xref: /linux/drivers/media/platform/renesas/rcar-isp/core-io.c (revision 67f8bc848ee31831336bd478e57d2f993551902e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2026 Renesas Electronics Corp.
4  * Copyright (C) 2026 Ideas on Board Oy
5  * Copyright (C) 2026 Ragnatech AB
6  */
7 
8 #include <media/v4l2-ctrls.h>
9 #include <media/v4l2-event.h>
10 #include <media/v4l2-ioctl.h>
11 #include <media/v4l2-isp.h>
12 #include <media/v4l2-mc.h>
13 
14 #include <linux/media/dreamchip/rppx1-config.h>
15 
16 #include "risp-core.h"
17 
18 #define risp_io_err(d, fmt, arg...)         dev_err((d)->core->dev, fmt, ##arg)
19 
20 static struct risp_buffer *risp_io_vb2buf(struct vb2_v4l2_buffer *vb)
21 {
22 	return container_of(vb, struct risp_buffer, vb);
23 }
24 
25 static int risp_io_open(struct file *file)
26 {
27 	struct rcar_isp_core_io *io = video_drvdata(file);
28 	int ret;
29 
30 	ret = mutex_lock_interruptible(&io->lock);
31 	if (ret)
32 		return ret;
33 
34 	file->private_data = io;
35 
36 	ret = v4l2_fh_open(file);
37 	if (ret)
38 		goto err_unlock;
39 
40 	ret = v4l2_pipeline_pm_get(&io->vdev.entity);
41 	if (ret < 0)
42 		goto err_open;
43 
44 	mutex_unlock(&io->lock);
45 
46 	return 0;
47 err_open:
48 	v4l2_fh_release(file);
49 err_unlock:
50 	mutex_unlock(&io->lock);
51 
52 	return ret;
53 }
54 
55 static int risp_io_release(struct file *file)
56 {
57 	struct rcar_isp_core_io *io = video_drvdata(file);
58 	int ret;
59 
60 	mutex_lock(&io->lock);
61 
62 	ret = _vb2_fop_release(file, NULL);
63 
64 	v4l2_pipeline_pm_put(&io->vdev.entity);
65 
66 	mutex_unlock(&io->lock);
67 
68 	return ret;
69 }
70 
71 static const struct v4l2_file_operations risp_io_fops = {
72 	.owner		= THIS_MODULE,
73 	.unlocked_ioctl	= video_ioctl2,
74 	.open		= risp_io_open,
75 	.release	= risp_io_release,
76 	.poll		= vb2_fop_poll,
77 	.mmap		= vb2_fop_mmap,
78 	.read		= vb2_fop_read,
79 };
80 
81 /* -----------------------------------------------------------------------------
82  * Common queue
83  */
84 
85 static int risp_io_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
86 			       unsigned int *nplanes, unsigned int sizes[],
87 			       struct device *alloc_devs[])
88 
89 {
90 	struct rcar_isp_core_io *io = vb2_get_drv_priv(vq);
91 
92 	if (V4L2_TYPE_IS_MULTIPLANAR(vq->type)) {
93 		const struct v4l2_pix_format_mplane *pix = &io->format.fmt.pix_mp;
94 
95 		if (*nplanes) {
96 			if (*nplanes > pix->num_planes)
97 				return -EINVAL;
98 
99 			for (unsigned int i = 0; i < pix->num_planes; i++)
100 				if (sizes[i] < pix->plane_fmt[i].sizeimage)
101 					return -EINVAL;
102 
103 			return 0;
104 		}
105 
106 		*nplanes = pix->num_planes;
107 		for (unsigned int i = 0; i < pix->num_planes; i++)
108 			sizes[i] = pix->plane_fmt[i].sizeimage;
109 	} else {
110 		if (*nplanes) {
111 			if (sizes[0] < io->format.fmt.meta.buffersize)
112 				return -EINVAL;
113 
114 			return 0;
115 		}
116 
117 		*nplanes = 1;
118 		sizes[0] = io->format.fmt.meta.buffersize;
119 	}
120 
121 	/* Initialize buffer queue */
122 	INIT_LIST_HEAD(&io->buffers);
123 
124 	return 0;
125 };
126 
127 static int risp_io_buffer_prepare_set(struct rcar_isp_core_io *io,
128 				      struct vb2_buffer *vb, unsigned int plane,
129 				      unsigned long size)
130 {
131 	if (vb2_plane_size(vb, plane) < size) {
132 		risp_io_err(io, "Buffer too small (%lu < %lu)\n",
133 			    vb2_plane_size(vb, plane), size);
134 		return -EINVAL;
135 	}
136 
137 	vb2_set_plane_payload(vb, plane, size);
138 
139 	return 0;
140 }
141 
142 static int risp_io_buffer_prepare(struct vb2_buffer *vb)
143 {
144 	struct rcar_isp_core_io *io = vb2_get_drv_priv(vb->vb2_queue);
145 
146 	if (V4L2_TYPE_IS_MULTIPLANAR(vb->vb2_queue->type)) {
147 		const struct v4l2_pix_format_mplane *pix = &io->format.fmt.pix_mp;
148 		int ret = 0;
149 
150 		for (unsigned int i = 0; i < pix->num_planes; i++) {
151 			ret = risp_io_buffer_prepare_set(io, vb, i,
152 							 pix->plane_fmt[i].sizeimage);
153 			if (ret)
154 				break;
155 		}
156 
157 		return ret;
158 	}
159 
160 	return risp_io_buffer_prepare_set(io, vb, 0,
161 					  io->format.fmt.meta.buffersize);
162 }
163 
164 static void risp_io_buffer_queue(struct vb2_buffer *vb)
165 {
166 	struct rcar_isp_core_io *io = vb2_get_drv_priv(vb->vb2_queue);
167 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
168 	struct risp_buffer *buf = risp_io_vb2buf(vbuf);
169 
170 	guard(mutex)(&io->core->io_lock);
171 
172 	list_add_tail(&buf->list, &io->buffers);
173 
174 	if (risp_core_job_prepare(io->core))
175 		risp_io_err(io, "Failed to prepare job\n");
176 }
177 
178 static void risp_io_return_buffers(struct rcar_isp_core_io *io,
179 				   enum vb2_buffer_state state)
180 {
181 	struct risp_buffer *buf, *node;
182 
183 	lockdep_assert_held(&io->core->io_lock);
184 
185 	list_for_each_entry_safe(buf, node, &io->buffers, list) {
186 		vb2_buffer_done(&buf->vb.vb2_buf, state);
187 		list_del(&buf->list);
188 	}
189 }
190 
191 static int risp_io_start_streaming(struct vb2_queue *vq, unsigned int count)
192 {
193 	struct rcar_isp_core_io *io = vb2_get_drv_priv(vq);
194 	int ret;
195 
196 	scoped_guard(mutex, &io->core->io_lock) {
197 		if (io->core->io[RISP_CORE_INPUT1].format.fmt.pix_mp.width !=
198 		    io->core->io[RISP_CORE_OUTPUT1].format.fmt.pix_mp.width ||
199 		    io->core->io[RISP_CORE_INPUT1].format.fmt.pix_mp.height !=
200 		    io->core->io[RISP_CORE_OUTPUT1].format.fmt.pix_mp.height) {
201 			risp_io_return_buffers(io, VB2_BUF_STATE_QUEUED);
202 			return -EPIPE;
203 		}
204 
205 		io->streaming = true;
206 	}
207 
208 	ret = risp_core_start_streaming(io->core);
209 	if (ret) {
210 		guard(mutex)(&io->core->io_lock);
211 
212 		risp_io_return_buffers(io, VB2_BUF_STATE_QUEUED);
213 		return ret;
214 	}
215 
216 	return 0;
217 }
218 
219 static void risp_io_stop_streaming(struct vb2_queue *vq)
220 {
221 	struct rcar_isp_core_io *io = vb2_get_drv_priv(vq);
222 
223 	scoped_guard(mutex, &io->core->io_lock) {
224 		io->streaming = false;
225 		risp_core_stop_streaming(io->core);
226 		risp_io_return_buffers(io, VB2_BUF_STATE_ERROR);
227 	}
228 
229 	/*
230 	 * Wait for buffers part of the jobs not yet processed. Note that this
231 	 * might complete buffers out of order.
232 	 */
233 	vb2_wait_for_all_buffers(&io->queue);
234 }
235 
236 /* -----------------------------------------------------------------------------
237  * Common V4L2 IOCTLs
238  */
239 
240 static int risp_io_querycap(struct file *file, void *priv,
241 			    struct v4l2_capability *cap)
242 {
243 	struct video_device *vdev = video_devdata(file);
244 
245 	strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
246 	strscpy(cap->card, vdev->name, sizeof(cap->card));
247 
248 	return 0;
249 }
250 
251 /* -----------------------------------------------------------------------------
252  * Input Exposure
253  */
254 
255 static int risp_io_input_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
256 				     unsigned int *nplanes, unsigned int sizes[],
257 				     struct device *alloc_devs[])
258 
259 {
260 	struct rcar_isp_core_io *io = vb2_get_drv_priv(vq);
261 	struct rcar_isp_core *core = io->core;
262 	struct device *bus_master;
263 	int ret;
264 
265 	ret = risp_io_queue_setup(vq, nbuffers, nplanes, sizes, alloc_devs);
266 	if (ret)
267 		return ret;
268 
269 	bus_master = vsp1_isp_get_bus_master(core->vspx.dev);
270 	if (IS_ERR_OR_NULL(bus_master)) {
271 		risp_io_err(io, "Missing reference to bus-master device\n");
272 		return -EINVAL;
273 	}
274 
275 	/*
276 	 * Allocate buffers using the bus_master device associated with the
277 	 * VSPX associated to this ISP instance.
278 	 */
279 	alloc_devs[0] = bus_master;
280 
281 	return 0;
282 };
283 
284 static const struct vb2_ops risp_io_input_qops = {
285 	.queue_setup		= risp_io_input_queue_setup,
286 	.buf_prepare		= risp_io_buffer_prepare,
287 	.buf_queue		= risp_io_buffer_queue,
288 	.start_streaming	= risp_io_start_streaming,
289 	.stop_streaming		= risp_io_stop_streaming,
290 };
291 
292 static const struct v4l2_pix_format_mplane risp_io_input_default_format = {
293 	.width = 1920,
294 	.height = 1080,
295 	.field = V4L2_FIELD_NONE,
296 	.pixelformat = V4L2_PIX_FMT_SGRBG8,
297 	.colorspace = V4L2_COLORSPACE_RAW,
298 	.xfer_func = V4L2_XFER_FUNC_NONE,
299 	.ycbcr_enc = V4L2_YCBCR_ENC_601,
300 	.quantization = V4L2_QUANTIZATION_FULL_RANGE,
301 	.num_planes = 1,
302 	.plane_fmt = {
303 		[0] = {
304 			.sizeimage = 1920 * 1080,
305 			.bytesperline = 1920,
306 		},
307 	},
308 };
309 
310 static const struct risp_io_input_format {
311 	unsigned int fourcc;
312 	unsigned int bpp;
313 } risp_io_input_formats[] = {
314 	{ .fourcc = V4L2_PIX_FMT_SBGGR8,	.bpp = 1 },
315 	{ .fourcc = V4L2_PIX_FMT_SGBRG8,	.bpp = 1 },
316 	{ .fourcc = V4L2_PIX_FMT_SGRBG8,	.bpp = 1 },
317 	{ .fourcc = V4L2_PIX_FMT_SRGGB8,	.bpp = 1 },
318 	{ .fourcc = V4L2_PIX_FMT_SBGGR10,	.bpp = 2 },
319 	{ .fourcc = V4L2_PIX_FMT_SGBRG10,	.bpp = 2 },
320 	{ .fourcc = V4L2_PIX_FMT_SGRBG10,	.bpp = 2 },
321 	{ .fourcc = V4L2_PIX_FMT_SRGGB10,	.bpp = 2 },
322 	{ .fourcc = V4L2_PIX_FMT_SBGGR12,	.bpp = 2 },
323 	{ .fourcc = V4L2_PIX_FMT_SGBRG12,	.bpp = 2 },
324 	{ .fourcc = V4L2_PIX_FMT_SGRBG12,	.bpp = 2 },
325 	{ .fourcc = V4L2_PIX_FMT_SRGGB12,	.bpp = 2 },
326 };
327 
328 static void risp_io_input_try_format(struct rcar_isp_core_io *io,
329 				     struct v4l2_pix_format_mplane *pix)
330 {
331 	unsigned int bpp = 0;
332 
333 	v4l_bound_align_image(&pix->width, 128, 5120, 2,
334 			      &pix->height, 128, 4096, 2, 0);
335 
336 	for (unsigned int i = 0; i < ARRAY_SIZE(risp_io_input_formats); i++) {
337 		if (risp_io_input_formats[i].fourcc == pix->pixelformat) {
338 			bpp = risp_io_input_formats[i].bpp;
339 			break;
340 		}
341 	}
342 
343 	if (!bpp) {
344 		pix->pixelformat = risp_io_input_formats[0].fourcc;
345 		bpp = risp_io_input_formats[0].bpp;
346 	}
347 
348 	pix->field = V4L2_FIELD_NONE;
349 	pix->colorspace = V4L2_COLORSPACE_RAW;
350 
351 	pix->num_planes = 1;
352 	pix->plane_fmt[0].bytesperline = pix->width * bpp;
353 	pix->plane_fmt[0].sizeimage = pix->plane_fmt[0].bytesperline * pix->height;
354 }
355 
356 static int risp_io_input_enum_fmt(struct file *file, void *priv,
357 				  struct v4l2_fmtdesc *f)
358 {
359 	if (f->index >= ARRAY_SIZE(risp_io_input_formats))
360 		return -EINVAL;
361 
362 	f->pixelformat = risp_io_input_formats[f->index].fourcc;
363 
364 	return 0;
365 }
366 
367 static int risp_io_input_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
368 {
369 	struct rcar_isp_core_io *io = video_drvdata(file);
370 
371 	if (f->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
372 		return -EINVAL;
373 
374 	f->fmt.pix_mp = io->format.fmt.pix_mp;
375 
376 	return 0;
377 }
378 
379 static int risp_io_input_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
380 {
381 	struct rcar_isp_core_io *io = video_drvdata(file);
382 
383 	if (vb2_is_busy(&io->queue))
384 		return -EBUSY;
385 
386 	if (f->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
387 		return -EINVAL;
388 
389 	risp_io_input_try_format(io, &f->fmt.pix_mp);
390 
391 	io->format.fmt.pix_mp = f->fmt.pix_mp;
392 
393 	return 0;
394 }
395 
396 static int risp_io_input_try_fmt(struct file *file, void *fh,
397 				 struct v4l2_format *f)
398 {
399 	struct rcar_isp_core_io *io = video_drvdata(file);
400 
401 	risp_io_input_try_format(io, &f->fmt.pix_mp);
402 
403 	return 0;
404 }
405 
406 static int risp_io_input_enum_framesizes(struct file *file, void *fh,
407 					 struct v4l2_frmsizeenum *fsize)
408 {
409 	bool found = false;
410 
411 	if (fsize->index != 0)
412 		return -EINVAL;
413 
414 	for (unsigned int i = 0; i < ARRAY_SIZE(risp_io_input_formats); i++) {
415 		if (risp_io_input_formats[i].fourcc == fsize->pixel_format) {
416 			found = true;
417 			break;
418 		}
419 	}
420 
421 	if (!found)
422 		return -EINVAL;
423 
424 	fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
425 
426 	fsize->stepwise.min_width = 128;
427 	fsize->stepwise.max_width = 5120;
428 	fsize->stepwise.step_width = 2;
429 
430 	fsize->stepwise.min_height = 128;
431 	fsize->stepwise.max_height = 4096;
432 	fsize->stepwise.step_height = 2;
433 
434 	return 0;
435 }
436 
437 static const struct v4l2_ioctl_ops risp_io_input_ioctl_ops = {
438 	.vidioc_querycap		= risp_io_querycap,
439 
440 	.vidioc_enum_fmt_vid_out	= risp_io_input_enum_fmt,
441 	.vidioc_g_fmt_vid_out_mplane	= risp_io_input_g_fmt,
442 	.vidioc_s_fmt_vid_out_mplane	= risp_io_input_s_fmt,
443 	.vidioc_try_fmt_vid_out_mplane	= risp_io_input_try_fmt,
444 	.vidioc_enum_framesizes		= risp_io_input_enum_framesizes,
445 
446 	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
447 	.vidioc_querybuf		= vb2_ioctl_querybuf,
448 	.vidioc_qbuf			= vb2_ioctl_qbuf,
449 	.vidioc_expbuf			= vb2_ioctl_expbuf,
450 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
451 	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
452 	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
453 	.vidioc_streamon		= vb2_ioctl_streamon,
454 	.vidioc_streamoff		= vb2_ioctl_streamoff,
455 };
456 
457 /* -----------------------------------------------------------------------------
458  * Parameters
459  *
460  */
461 
462 static int risp_io_params_buf_init(struct vb2_buffer *vb)
463 {
464 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
465 	struct risp_buffer *buf = risp_io_vb2buf(vbuf);
466 	struct rcar_isp_core_io *io = vb2_get_drv_priv(vb->vb2_queue);
467 	struct rcar_isp_core *core = io->core;
468 	size_t size;
469 	int ret;
470 
471 	memset(&buf->vsp_buffer, 0, sizeof(buf->vsp_buffer));
472 
473 	size = RISP_IO_PARAMS_BUF_SIZE;
474 	ret = vsp1_isp_alloc_buffer(core->vspx.dev, size, &buf->vsp_buffer);
475 	if (ret)
476 		return -EINVAL;
477 
478 	memset(buf->vsp_buffer.cpu_addr, 0, RISP_IO_PARAMS_BUF_SIZE);
479 
480 	return 0;
481 }
482 
483 static void risp_io_params_buf_cleanup(struct vb2_buffer *vb)
484 {
485 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
486 	struct risp_buffer *buf = risp_io_vb2buf(vbuf);
487 	struct rcar_isp_core_io *io = vb2_get_drv_priv(vb->vb2_queue);
488 	struct rcar_isp_core *core = io->core;
489 
490 	vsp1_isp_free_buffer(core->vspx.dev, &buf->vsp_buffer);
491 }
492 
493 struct risp_conf_dma_write_desc {
494 	u32 *buf;
495 	u32 base;
496 	unsigned int count;
497 };
498 
499 static int risp_conf_dma_prepare(void *priv, u32 offset, u32 value)
500 {
501 	struct risp_conf_dma_write_desc *desc = priv;
502 
503 	/* Bounds check, 8 bytes = address (4)+ value (4). */
504 	if ((desc->count + 1) * 8 > RISP_IO_PARAMS_BUF_SIZE)
505 		return -ENOMEM;
506 
507 	(*desc->buf++) = desc->base | offset;
508 	(*desc->buf++) = value;
509 
510 	desc->count++;
511 
512 	return 0;
513 }
514 
515 static int risp_io_params_buffer_prepare(struct vb2_buffer *vb)
516 {
517 	struct rcar_isp_core_io *io = vb2_get_drv_priv(vb->vb2_queue);
518 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
519 	struct risp_buffer *buf = risp_io_vb2buf(vbuf);
520 	struct risp_conf_dma_write_desc desc;
521 	u32 *cpu_addr;
522 	int ret;
523 
524 	/* Prepare params. */
525 	cpu_addr = (u32 *)buf->vsp_buffer.cpu_addr;
526 
527 	desc.buf = cpu_addr + 2;
528 	desc.base = io->core->rppaddr;
529 	desc.count = 0;
530 
531 	/* Fill params body. */
532 	ret = rppx1_params(io->core->rpp, vb, io->format.fmt.meta.buffersize,
533 			   risp_conf_dma_prepare, &desc);
534 	if (ret)
535 		return ret;
536 
537 	/* Fill params header. */
538 	cpu_addr[0] = desc.count;
539 	cpu_addr[1] = 0x0;
540 
541 	return 0;
542 }
543 
544 static const struct vb2_ops risp_io_params_qops = {
545 	.queue_setup		= risp_io_queue_setup,
546 	.buf_init		= risp_io_params_buf_init,
547 	.buf_cleanup		= risp_io_params_buf_cleanup,
548 	.buf_prepare		= risp_io_params_buffer_prepare,
549 	.buf_queue		= risp_io_buffer_queue,
550 	.start_streaming	= risp_io_start_streaming,
551 	.stop_streaming		= risp_io_stop_streaming,
552 };
553 
554 static const struct v4l2_meta_format risp_io_params_default_format = {
555 	.dataformat = V4L2_META_FMT_RPPX1_PARAMS,
556 	.buffersize = v4l2_isp_buffer_size(RPPX1_PARAMS_MAX_SIZE),
557 };
558 
559 static int risp_io_params_enum_fmt(struct file *file, void *priv,
560 				   struct v4l2_fmtdesc *f)
561 {
562 	struct rcar_isp_core_io *io = video_drvdata(file);
563 
564 	if (f->type != V4L2_BUF_TYPE_META_OUTPUT || f->index)
565 		return -EINVAL;
566 
567 	f->pixelformat = io->format.fmt.meta.dataformat;
568 
569 	return 0;
570 }
571 
572 static int risp_io_params_g_fmt(struct file *file, void *priv,
573 				struct v4l2_format *f)
574 {
575 	struct rcar_isp_core_io *io = video_drvdata(file);
576 	struct v4l2_meta_format *meta = &f->fmt.meta;
577 
578 	if (f->type != V4L2_BUF_TYPE_META_OUTPUT)
579 		return -EINVAL;
580 
581 	*meta = io->format.fmt.meta;
582 
583 	return 0;
584 }
585 
586 static int risp_io_params_s_fmt(struct file *file, void *priv,
587 				struct v4l2_format *f)
588 {
589 	struct rcar_isp_core_io *io = video_drvdata(file);
590 
591 	if (vb2_is_busy(&io->queue))
592 		return -EBUSY;
593 
594 	return risp_io_params_g_fmt(file, priv, f);
595 }
596 
597 static const struct v4l2_ioctl_ops risp_io_params_ioctl_ops = {
598 	.vidioc_querycap		= risp_io_querycap,
599 
600 	.vidioc_enum_fmt_meta_out	= risp_io_params_enum_fmt,
601 	.vidioc_g_fmt_meta_out		= risp_io_params_g_fmt,
602 	.vidioc_s_fmt_meta_out		= risp_io_params_s_fmt,
603 	.vidioc_try_fmt_meta_out	= risp_io_params_g_fmt,
604 
605 	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
606 	.vidioc_querybuf		= vb2_ioctl_querybuf,
607 	.vidioc_qbuf			= vb2_ioctl_qbuf,
608 	.vidioc_expbuf			= vb2_ioctl_expbuf,
609 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
610 	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
611 	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
612 	.vidioc_streamon		= vb2_ioctl_streamon,
613 	.vidioc_streamoff		= vb2_ioctl_streamoff,
614 };
615 
616 /* -----------------------------------------------------------------------------
617  * Statistics
618  */
619 
620 static const struct vb2_ops risp_io_stats_qops = {
621 	.queue_setup		= risp_io_queue_setup,
622 	.buf_prepare		= risp_io_buffer_prepare,
623 	.buf_queue		= risp_io_buffer_queue,
624 	.start_streaming	= risp_io_start_streaming,
625 	.stop_streaming		= risp_io_stop_streaming,
626 };
627 
628 static const struct v4l2_meta_format risp_io_stats_default_format = {
629 	.dataformat = V4L2_META_FMT_RPPX1_STATS,
630 	.buffersize = v4l2_isp_buffer_size(RPPX1_STATS_MAX_SIZE),
631 };
632 
633 static int risp_io_stats_enum_fmt(struct file *file, void *priv,
634 				  struct v4l2_fmtdesc *f)
635 {
636 	struct rcar_isp_core_io *io = video_drvdata(file);
637 
638 	if (f->type != V4L2_BUF_TYPE_META_CAPTURE || f->index)
639 		return -EINVAL;
640 
641 	f->pixelformat = io->format.fmt.meta.dataformat;
642 
643 	return 0;
644 }
645 
646 static int risp_io_stats_g_fmt(struct file *file, void *priv,
647 			       struct v4l2_format *f)
648 {
649 	struct rcar_isp_core_io *io = video_drvdata(file);
650 	struct v4l2_meta_format *meta = &f->fmt.meta;
651 
652 	if (f->type != V4L2_BUF_TYPE_META_CAPTURE)
653 		return -EINVAL;
654 
655 	*meta = io->format.fmt.meta;
656 
657 	return 0;
658 }
659 
660 static int risp_io_stats_s_fmt(struct file *file, void *priv,
661 			       struct v4l2_format *f)
662 {
663 	struct rcar_isp_core_io *io = video_drvdata(file);
664 
665 	if (vb2_is_busy(&io->queue))
666 		return -EBUSY;
667 
668 	return risp_io_stats_g_fmt(file, priv, f);
669 }
670 
671 static const struct v4l2_ioctl_ops risp_io_stats_ioctl_ops = {
672 	.vidioc_querycap		= risp_io_querycap,
673 
674 	.vidioc_enum_fmt_meta_cap	= risp_io_stats_enum_fmt,
675 	.vidioc_g_fmt_meta_cap		= risp_io_stats_g_fmt,
676 	.vidioc_s_fmt_meta_cap		= risp_io_stats_s_fmt,
677 	.vidioc_try_fmt_meta_cap	= risp_io_stats_g_fmt,
678 
679 	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
680 	.vidioc_querybuf		= vb2_ioctl_querybuf,
681 	.vidioc_qbuf			= vb2_ioctl_qbuf,
682 	.vidioc_expbuf			= vb2_ioctl_expbuf,
683 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
684 	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
685 	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
686 	.vidioc_streamon		= vb2_ioctl_streamon,
687 	.vidioc_streamoff		= vb2_ioctl_streamoff,
688 };
689 
690 /* -----------------------------------------------------------------------------
691  * Video capture
692  */
693 
694 static const struct vb2_ops risp_io_capture_qops = {
695 	.queue_setup		= risp_io_queue_setup,
696 	.buf_prepare		= risp_io_buffer_prepare,
697 	.buf_queue		= risp_io_buffer_queue,
698 	.start_streaming	= risp_io_start_streaming,
699 	.stop_streaming		= risp_io_stop_streaming,
700 };
701 
702 static const struct v4l2_pix_format_mplane risp_io_capture_default_format = {
703 	.width = 1920,
704 	.height = 1080,
705 	.pixelformat = V4L2_PIX_FMT_XBGR32,
706 	.field = V4L2_FIELD_NONE,
707 	.colorspace = V4L2_COLORSPACE_SRGB,
708 	.ycbcr_enc = V4L2_YCBCR_ENC_601,
709 	.quantization = V4L2_QUANTIZATION_FULL_RANGE,
710 	.xfer_func = V4L2_XFER_FUNC_SRGB,
711 	.num_planes = 1,
712 	.plane_fmt = {
713 		[0] = {
714 			.bytesperline = ALIGN(1920 * 4, 256),
715 			.sizeimage = ALIGN(1920 * 4, 256) * 1080,
716 		},
717 	},
718 };
719 
720 static void risp_io_capture_try_format(struct rcar_isp_core_io *io,
721 				       struct v4l2_pix_format_mplane *pix)
722 {
723 	v4l_bound_align_image(&pix->width, 128, 5120, 2,
724 			      &pix->height, 128, 4096, 2, 0);
725 
726 	pix->field = V4L2_FIELD_NONE;
727 	pix->colorspace = V4L2_COLORSPACE_SRGB;
728 	pix->ycbcr_enc = V4L2_YCBCR_ENC_601;
729 	pix->xfer_func = V4L2_XFER_FUNC_SRGB;
730 
731 	switch (pix->pixelformat) {
732 	case V4L2_PIX_FMT_NV16M:
733 		pix->quantization = V4L2_QUANTIZATION_LIM_RANGE;
734 		pix->num_planes = 2;
735 		pix->plane_fmt[0].bytesperline = ALIGN(pix->width, 256);
736 		pix->plane_fmt[0].sizeimage = pix->plane_fmt[0].bytesperline * pix->height;
737 		pix->plane_fmt[1].bytesperline = ALIGN(pix->width, 256);
738 		pix->plane_fmt[1].sizeimage = pix->plane_fmt[1].bytesperline * pix->height;
739 		break;
740 	default:
741 		pix->pixelformat = V4L2_PIX_FMT_XBGR32;
742 		pix->quantization = V4L2_QUANTIZATION_FULL_RANGE;
743 		pix->num_planes = 1;
744 		pix->plane_fmt[0].bytesperline = ALIGN(pix->width * 4, 256);
745 		pix->plane_fmt[0].sizeimage = pix->plane_fmt[0].bytesperline * pix->height;
746 		break;
747 	}
748 }
749 
750 static int risp_io_capture_enum_fmt(struct file *file, void *priv,
751 				    struct v4l2_fmtdesc *f)
752 {
753 	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
754 		return -EINVAL;
755 
756 	switch (f->index) {
757 	case 0:
758 		f->pixelformat = V4L2_PIX_FMT_NV16M;
759 		break;
760 	case 1:
761 		f->pixelformat = V4L2_PIX_FMT_XBGR32;
762 		break;
763 	default:
764 		return -EINVAL;
765 	}
766 
767 	return 0;
768 }
769 
770 static int risp_io_capture_g_fmt(struct file *file, void *priv,
771 				 struct v4l2_format *f)
772 {
773 	struct rcar_isp_core_io *io = video_drvdata(file);
774 
775 	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
776 		return -EINVAL;
777 
778 	f->fmt.pix_mp = io->format.fmt.pix_mp;
779 
780 	return 0;
781 }
782 
783 static int risp_io_capture_s_fmt(struct file *file, void *priv,
784 				 struct v4l2_format *f)
785 {
786 	struct rcar_isp_core_io *io = video_drvdata(file);
787 
788 	if (vb2_is_busy(&io->queue))
789 		return -EBUSY;
790 
791 	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
792 		return -EINVAL;
793 
794 	risp_io_capture_try_format(io, &f->fmt.pix_mp);
795 
796 	io->format.fmt.pix_mp = f->fmt.pix_mp;
797 
798 	return 0;
799 }
800 
801 static int risp_io_capture_try_fmt(struct file *file, void *fh,
802 				   struct v4l2_format *f)
803 {
804 	struct rcar_isp_core_io *io = video_drvdata(file);
805 
806 	risp_io_capture_try_format(io, &f->fmt.pix_mp);
807 
808 	return 0;
809 }
810 
811 static int risp_io_capture_enum_framesizes(struct file *file, void *fh,
812 					   struct v4l2_frmsizeenum *fsize)
813 {
814 	if (fsize->index != 0)
815 		return -EINVAL;
816 
817 	switch (fsize->pixel_format) {
818 	case V4L2_PIX_FMT_NV16M:
819 	case V4L2_PIX_FMT_XBGR32:
820 		break;
821 	default:
822 		return -EINVAL;
823 	}
824 
825 	fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
826 
827 	fsize->stepwise.min_width = 128;
828 	fsize->stepwise.max_width = 5120;
829 	fsize->stepwise.step_width = 2;
830 
831 	fsize->stepwise.min_height = 128;
832 	fsize->stepwise.max_height = 4096;
833 	fsize->stepwise.step_height = 2;
834 
835 	return 0;
836 }
837 
838 static const struct v4l2_ioctl_ops risp_io_capture_ioctl_ops = {
839 	.vidioc_querycap		= risp_io_querycap,
840 
841 	.vidioc_enum_fmt_vid_cap	= risp_io_capture_enum_fmt,
842 	.vidioc_g_fmt_vid_cap_mplane	= risp_io_capture_g_fmt,
843 	.vidioc_s_fmt_vid_cap_mplane	= risp_io_capture_s_fmt,
844 	.vidioc_try_fmt_vid_cap_mplane	= risp_io_capture_try_fmt,
845 	.vidioc_enum_framesizes		= risp_io_capture_enum_framesizes,
846 
847 	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
848 	.vidioc_querybuf		= vb2_ioctl_querybuf,
849 	.vidioc_qbuf			= vb2_ioctl_qbuf,
850 	.vidioc_expbuf			= vb2_ioctl_expbuf,
851 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
852 	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
853 	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
854 	.vidioc_streamon		= vb2_ioctl_streamon,
855 	.vidioc_streamoff		= vb2_ioctl_streamoff,
856 };
857 
858 /* -----------------------------------------------------------------------------
859  * Create and remove IO video devices
860  */
861 
862 int risp_core_io_create(struct device *dev, struct rcar_isp_core *core,
863 			struct rcar_isp_core_io *io, unsigned int pad)
864 {
865 	struct video_device *vdev = &io->vdev;
866 	struct vb2_queue *q = &io->queue;
867 	int ret;
868 
869 	switch (pad) {
870 	case RISP_CORE_INPUT1:
871 		snprintf(vdev->name, sizeof(vdev->name), "%s %s input1",
872 			 KBUILD_MODNAME, dev_name(dev));
873 		vdev->vfl_dir = VFL_DIR_TX;
874 		vdev->device_caps = V4L2_CAP_VIDEO_OUTPUT_MPLANE;
875 		vdev->ioctl_ops = &risp_io_input_ioctl_ops;
876 
877 		q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
878 		q->ops = &risp_io_input_qops;
879 
880 		io->pad.flags = MEDIA_PAD_FL_SOURCE;
881 		io->format.fmt.pix_mp = risp_io_input_default_format;
882 		break;
883 
884 	case RISP_CORE_PARAMS:
885 		snprintf(vdev->name, sizeof(vdev->name), "%s %s params",
886 			 KBUILD_MODNAME, dev_name(dev));
887 		vdev->vfl_dir = VFL_DIR_TX;
888 		vdev->device_caps = V4L2_CAP_META_OUTPUT;
889 		vdev->ioctl_ops = &risp_io_params_ioctl_ops;
890 
891 		q->type = V4L2_BUF_TYPE_META_OUTPUT;
892 		q->ops = &risp_io_params_qops;
893 
894 		io->pad.flags = MEDIA_PAD_FL_SOURCE;
895 		io->format.fmt.meta = risp_io_params_default_format;
896 		break;
897 
898 	case RISP_CORE_STATS:
899 		snprintf(vdev->name, sizeof(vdev->name), "%s %s stats",
900 			 KBUILD_MODNAME, dev_name(dev));
901 		vdev->vfl_dir = VFL_DIR_RX;
902 		vdev->device_caps = V4L2_CAP_META_CAPTURE;
903 		vdev->ioctl_ops = &risp_io_stats_ioctl_ops;
904 
905 		q->type = V4L2_BUF_TYPE_META_CAPTURE;
906 		q->ops = &risp_io_stats_qops;
907 
908 		io->pad.flags = MEDIA_PAD_FL_SINK;
909 		io->format.fmt.meta = risp_io_stats_default_format;
910 		break;
911 
912 	case RISP_CORE_OUTPUT1:
913 		snprintf(vdev->name, sizeof(vdev->name), "%s %s output1",
914 			 KBUILD_MODNAME, dev_name(dev));
915 		vdev->vfl_dir = VFL_DIR_RX;
916 		vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE;
917 		vdev->ioctl_ops = &risp_io_capture_ioctl_ops;
918 
919 		q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
920 		q->ops = &risp_io_capture_qops;
921 
922 		io->pad.flags = MEDIA_PAD_FL_SINK;
923 		io->format.fmt.pix_mp = risp_io_capture_default_format;
924 		break;
925 	}
926 
927 	io->core = core;
928 
929 	mutex_init(&io->lock);
930 	INIT_LIST_HEAD(&io->buffers);
931 
932 	/* Create media graph pad. */
933 	ret = media_entity_pads_init(&io->vdev.entity, 1, &io->pad);
934 	if (ret)
935 		return ret;
936 
937 	/* Create queue */
938 	q->io_modes = VB2_MMAP | VB2_DMABUF;
939 	q->lock = &io->lock;
940 	q->drv_priv = io;
941 	q->mem_ops = &vb2_dma_contig_memops;
942 	q->buf_struct_size = sizeof(struct risp_buffer);
943 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
944 	q->dev = dev;
945 
946 	ret = vb2_queue_init(q);
947 	if (ret < 0) {
948 		risp_io_err(io, "Failed to initialize VB2 queue\n");
949 		return ret;
950 	}
951 
952 	/* Create video device */
953 	vdev->v4l2_dev = &core->v4l2_dev;
954 	vdev->queue = &io->queue;
955 
956 	vdev->release = video_device_release_empty;
957 	vdev->lock = &io->lock;
958 	vdev->fops = &risp_io_fops;
959 
960 	vdev->device_caps |= V4L2_CAP_STREAMING | V4L2_CAP_IO_MC;
961 
962 	ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
963 	if (ret) {
964 		risp_io_err(io, "Failed to register video device\n");
965 		return ret;
966 	}
967 
968 	video_set_drvdata(&io->vdev, io);
969 
970 	v4l2_info(&core->v4l2_dev, "Device registered as %s\n",
971 		  video_device_node_name(vdev));
972 
973 	switch (pad) {
974 	case RISP_CORE_INPUT1:
975 	case RISP_CORE_PARAMS:
976 		ret = media_create_pad_link(&io->vdev.entity, 0,
977 					    &core->subdev.entity, pad,
978 					    MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
979 		break;
980 	case RISP_CORE_STATS:
981 	case RISP_CORE_OUTPUT1:
982 		ret = media_create_pad_link(&core->subdev.entity, pad,
983 					    &io->vdev.entity, 0,
984 					    MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
985 		break;
986 	}
987 
988 	return ret;
989 }
990 
991 void risp_core_io_destroy(struct rcar_isp_core_io *io)
992 {
993 	if (!video_is_registered(&io->vdev))
994 		return;
995 
996 	vb2_video_unregister_device(&io->vdev);
997 }
998