1 From 70a588f7daefe34697ccd04fa6ac3a6d4b63be88 Mon Sep 17 00:00:00 2001
2 From: Jack Zhu <jack.zhu@starfivetech.com>
3 Date: Fri, 12 May 2023 18:28:42 +0800
4 Subject: [PATCH 084/122] media: starfive: Add video driver
6 Add video driver for StarFive Camera Subsystem.
8 Signed-off-by: Jack Zhu <jack.zhu@starfivetech.com>
10 drivers/media/platform/starfive/Makefile | 3 +-
11 drivers/media/platform/starfive/stf_video.c | 864 ++++++++++++++++++++
12 drivers/media/platform/starfive/stf_video.h | 95 +++
13 3 files changed, 961 insertions(+), 1 deletion(-)
14 create mode 100644 drivers/media/platform/starfive/stf_video.c
15 create mode 100644 drivers/media/platform/starfive/stf_video.h
17 --- a/drivers/media/platform/starfive/Makefile
18 +++ b/drivers/media/platform/starfive/Makefile
22 starfive-camss-objs += \
27 obj-$(CONFIG_VIDEO_STARFIVE_CAMSS) += starfive-camss.o \
29 +++ b/drivers/media/platform/starfive/stf_video.c
31 +// SPDX-License-Identifier: GPL-2.0
35 + * StarFive Camera Subsystem - V4L2 device node
37 + * Copyright (C) 2021-2023 StarFive Technology Co., Ltd.
40 +#include <media/media-entity.h>
41 +#include <media/v4l2-ctrls.h>
42 +#include <media/v4l2-event.h>
43 +#include <media/v4l2-mc.h>
44 +#include <media/videobuf2-dma-contig.h>
46 +#include "stf_camss.h"
47 +#include "stf_video.h"
49 +static const struct stfcamss_format_info formats_pix_wr[] = {
50 + { MEDIA_BUS_FMT_SRGGB10_1X10, V4L2_PIX_FMT_SRGGB10, 1,
51 + { { 1, 1 } }, { { 1, 1 } }, { 10 } },
52 + { MEDIA_BUS_FMT_SGRBG10_1X10, V4L2_PIX_FMT_SGRBG10, 1,
53 + { { 1, 1 } }, { { 1, 1 } }, { 10 } },
54 + { MEDIA_BUS_FMT_SGBRG10_1X10, V4L2_PIX_FMT_SGBRG10, 1,
55 + { { 1, 1 } }, { { 1, 1 } }, { 10 } },
56 + { MEDIA_BUS_FMT_SBGGR10_1X10, V4L2_PIX_FMT_SBGGR10, 1,
57 + { { 1, 1 } }, { { 1, 1 } }, { 10 } },
60 +static const struct stfcamss_format_info formats_pix_isp[] = {
61 + { MEDIA_BUS_FMT_Y12_1X12, V4L2_PIX_FMT_NV12, 1,
62 + { { 1, 1 } }, { { 2, 3 } }, { 8 } },
65 +/* -----------------------------------------------------------------------------
69 +static int video_find_format(u32 code, u32 pixelformat,
70 + const struct stfcamss_format_info *formats,
71 + unsigned int nformats)
75 + for (i = 0; i < nformats; i++) {
76 + if (formats[i].code == code &&
77 + formats[i].pixelformat == pixelformat)
81 + for (i = 0; i < nformats; i++)
82 + if (formats[i].code == code)
85 + for (i = 0; i < nformats; i++)
86 + if (formats[i].pixelformat == pixelformat)
92 +static int __video_try_fmt(struct stfcamss_video *video, struct v4l2_format *f)
94 + struct v4l2_pix_format *pix;
95 + const struct stfcamss_format_info *fi;
102 + for (i = 0; i < video->nformats; i++)
103 + if (pix->pixelformat == video->formats[i].pixelformat)
106 + if (i == video->nformats)
107 + i = 0; /* default format */
109 + fi = &video->formats[i];
110 + width = pix->width;
111 + height = pix->height;
113 + memset(pix, 0, sizeof(*pix));
115 + pix->pixelformat = fi->pixelformat;
116 + pix->width = clamp_t(u32, width, STFCAMSS_FRAME_MIN_WIDTH,
117 + STFCAMSS_FRAME_MAX_WIDTH);
118 + pix->height = clamp_t(u32, height, STFCAMSS_FRAME_MIN_HEIGHT,
119 + STFCAMSS_FRAME_MAX_HEIGHT);
120 + bpl = pix->width / fi->hsub[0].numerator *
121 + fi->hsub[0].denominator * fi->bpp[0] / 8;
122 + bpl = ALIGN(bpl, video->bpl_alignment);
123 + pix->bytesperline = bpl;
124 + pix->sizeimage = pix->height / fi->vsub[0].numerator *
125 + fi->vsub[0].denominator * bpl;
127 + pix->field = V4L2_FIELD_NONE;
128 + pix->colorspace = V4L2_COLORSPACE_SRGB;
131 + V4L2_MAP_YCBCR_ENC_DEFAULT(pix->colorspace);
132 + pix->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
135 + pix->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(pix->colorspace);
140 +static int stf_video_init_format(struct stfcamss_video *video)
143 + struct v4l2_format format = {
144 + .type = video->type,
148 + .pixelformat = V4L2_PIX_FMT_RGB565,
152 + ret = __video_try_fmt(video, &format);
157 + video->active_fmt = format;
162 +/* -----------------------------------------------------------------------------
163 + * Video queue operations
166 +static int video_queue_setup(struct vb2_queue *q,
167 + unsigned int *num_buffers,
168 + unsigned int *num_planes,
169 + unsigned int sizes[],
170 + struct device *alloc_devs[])
172 + struct stfcamss_video *video = vb2_get_drv_priv(q);
173 + const struct v4l2_pix_format *format = &video->active_fmt.fmt.pix;
176 + if (*num_planes != 1)
179 + if (sizes[0] < format->sizeimage)
184 + sizes[0] = format->sizeimage;
186 + dev_err(video->stfcamss->dev,
187 + "%s: error size is zero!!!\n", __func__);
189 + dev_dbg(video->stfcamss->dev, "planes = %d, size = %d\n",
190 + *num_planes, sizes[0]);
195 +static int video_buf_init(struct vb2_buffer *vb)
197 + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
198 + struct stfcamss_video *video = vb2_get_drv_priv(vb->vb2_queue);
199 + struct stfcamss_buffer *buffer =
200 + container_of(vbuf, struct stfcamss_buffer, vb);
201 + const struct v4l2_pix_format *fmt = &video->active_fmt.fmt.pix;
204 + buffer->sizeimage = 0;
206 + paddr = vb2_plane_cookie(vb, 0);
207 + buffer->sizeimage = vb2_plane_size(vb, 0);
208 + buffer->addr[0] = *paddr;
209 + if (fmt->pixelformat == V4L2_PIX_FMT_NV12 ||
210 + fmt->pixelformat == V4L2_PIX_FMT_NV21 ||
211 + fmt->pixelformat == V4L2_PIX_FMT_NV16 ||
212 + fmt->pixelformat == V4L2_PIX_FMT_NV61)
214 + buffer->addr[0] + fmt->bytesperline * fmt->height;
219 +static int video_buf_prepare(struct vb2_buffer *vb)
221 + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
222 + struct stfcamss_video *video = vb2_get_drv_priv(vb->vb2_queue);
223 + const struct v4l2_pix_format *fmt = &video->active_fmt.fmt.pix;
225 + if (fmt->sizeimage > vb2_plane_size(vb, 0)) {
226 + dev_err(video->stfcamss->dev,
227 + "sizeimage = %d, plane size = %d\n",
228 + fmt->sizeimage, (unsigned int)vb2_plane_size(vb, 0));
231 + vb2_set_plane_payload(vb, 0, fmt->sizeimage);
233 + vbuf->field = V4L2_FIELD_NONE;
238 +static void video_buf_queue(struct vb2_buffer *vb)
240 + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
241 + struct stfcamss_video *video = vb2_get_drv_priv(vb->vb2_queue);
242 + struct stfcamss_buffer *buffer =
243 + container_of(vbuf, struct stfcamss_buffer, vb);
245 + video->ops->queue_buffer(video, buffer);
249 + * video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
250 + * @mbus: v4l2_mbus_framefmt format (input)
251 + * @pix: v4l2_pix_format_mplane format (output)
252 + * @f: a pointer to formats array element to be used for the conversion
253 + * @alignment: bytesperline alignment value
255 + * Fill the output pix structure with information from the input mbus format.
257 + * Return 0 on success or a negative error code otherwise
259 +static int video_mbus_to_pix(const struct v4l2_mbus_framefmt *mbus,
260 + struct v4l2_pix_format *pix,
261 + const struct stfcamss_format_info *f,
262 + unsigned int alignment)
266 + memset(pix, 0, sizeof(*pix));
267 + v4l2_fill_pix_format(pix, mbus);
268 + pix->pixelformat = f->pixelformat;
269 + bytesperline = pix->width / f->hsub[0].numerator *
270 + f->hsub[0].denominator * f->bpp[0] / 8;
271 + bytesperline = ALIGN(bytesperline, alignment);
272 + pix->bytesperline = bytesperline;
273 + pix->sizeimage = pix->height / f->vsub[0].numerator *
274 + f->vsub[0].denominator * bytesperline;
278 +static struct v4l2_subdev *video_remote_subdev(struct stfcamss_video *video,
281 + struct media_pad *remote;
283 + remote = media_pad_remote_pad_first(&video->pad);
285 + if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
289 + *pad = remote->index;
291 + return media_entity_to_v4l2_subdev(remote->entity);
294 +static int video_get_subdev_format(struct stfcamss_video *video,
295 + struct v4l2_format *format)
297 + struct v4l2_pix_format *pix = &video->active_fmt.fmt.pix;
298 + struct v4l2_subdev_format fmt;
299 + struct v4l2_subdev *subdev;
304 + subdev = video_remote_subdev(video, &pad);
309 + fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
311 + ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
315 + pixelformat = pix->pixelformat;
316 + ret = video_find_format(fmt.format.code, pixelformat,
317 + video->formats, video->nformats);
321 + format->type = video->type;
323 + return video_mbus_to_pix(&fmt.format, &format->fmt.pix,
324 + &video->formats[ret], video->bpl_alignment);
327 +static int video_check_format(struct stfcamss_video *video)
329 + struct v4l2_pix_format *pix = &video->active_fmt.fmt.pix;
330 + struct v4l2_format format;
331 + struct v4l2_pix_format *sd_pix = &format.fmt.pix;
334 + sd_pix->pixelformat = pix->pixelformat;
335 + ret = video_get_subdev_format(video, &format);
339 + if (pix->pixelformat != sd_pix->pixelformat ||
340 + pix->height > sd_pix->height ||
341 + pix->width > sd_pix->width ||
342 + pix->field != format.fmt.pix.field) {
343 + dev_err(video->stfcamss->dev,
345 + "0x%x 0x%x\n0x%x 0x%x\n0x%x 0x%x\n",
347 + pix->pixelformat, sd_pix->pixelformat,
348 + pix->height, sd_pix->height,
349 + pix->field, format.fmt.pix.field);
356 +static int video_start_streaming(struct vb2_queue *q, unsigned int count)
358 + struct stfcamss_video *video = vb2_get_drv_priv(q);
359 + struct video_device *vdev = &video->vdev;
360 + struct media_entity *entity;
361 + struct media_pad *pad;
362 + struct v4l2_subdev *subdev;
365 + ret = video_device_pipeline_start(vdev, &video->stfcamss->pipe);
367 + dev_err(video->stfcamss->dev,
368 + "Failed to media_pipeline_start: %d\n", ret);
372 + ret = video_check_format(video);
375 + entity = &vdev->entity;
377 + pad = &entity->pads[0];
378 + if (!(pad->flags & MEDIA_PAD_FL_SINK))
381 + pad = media_pad_remote_pad_first(pad);
382 + if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
385 + entity = pad->entity;
386 + subdev = media_entity_to_v4l2_subdev(entity);
388 + ret = v4l2_subdev_call(subdev, video, s_stream, 1);
389 + if (ret < 0 && ret != -ENOIOCTLCMD)
395 + video_device_pipeline_stop(vdev);
396 + video->ops->flush_buffers(video, VB2_BUF_STATE_QUEUED);
400 +static void video_stop_streaming(struct vb2_queue *q)
402 + struct stfcamss_video *video = vb2_get_drv_priv(q);
403 + struct video_device *vdev = &video->vdev;
404 + struct media_entity *entity;
405 + struct media_pad *pad;
406 + struct v4l2_subdev *subdev;
408 + entity = &vdev->entity;
410 + pad = &entity->pads[0];
411 + if (!(pad->flags & MEDIA_PAD_FL_SINK))
414 + pad = media_pad_remote_pad_first(pad);
415 + if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
418 + entity = pad->entity;
419 + subdev = media_entity_to_v4l2_subdev(entity);
421 + v4l2_subdev_call(subdev, video, s_stream, 0);
424 + video_device_pipeline_stop(vdev);
425 + video->ops->flush_buffers(video, VB2_BUF_STATE_ERROR);
428 +static const struct vb2_ops stf_video_vb2_q_ops = {
429 + .queue_setup = video_queue_setup,
430 + .wait_prepare = vb2_ops_wait_prepare,
431 + .wait_finish = vb2_ops_wait_finish,
432 + .buf_init = video_buf_init,
433 + .buf_prepare = video_buf_prepare,
434 + .buf_queue = video_buf_queue,
435 + .start_streaming = video_start_streaming,
436 + .stop_streaming = video_stop_streaming,
439 +/* -----------------------------------------------------------------------------
443 +static int video_querycap(struct file *file, void *fh,
444 + struct v4l2_capability *cap)
446 + struct stfcamss_video *video = video_drvdata(file);
448 + strscpy(cap->driver, "stf camss", sizeof(cap->driver));
449 + strscpy(cap->card, "Starfive Camera Subsystem", sizeof(cap->card));
450 + snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
451 + dev_name(video->stfcamss->dev));
455 +static int video_get_pfmt_by_index(struct stfcamss_video *video, int ndx)
459 + /* find index "i" of "k"th unique pixelformat in formats array */
461 + for (i = 0; i < video->nformats; i++) {
462 + for (j = 0; j < i; j++) {
463 + if (video->formats[i].pixelformat ==
464 + video->formats[j].pixelformat)
478 +static int video_get_pfmt_by_mcode(struct stfcamss_video *video, u32 mcode)
482 + for (i = 0; i < video->nformats; i++) {
483 + if (video->formats[i].code == mcode)
490 +static int video_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
492 + struct stfcamss_video *video = video_drvdata(file);
495 + if (f->type != video->type)
497 + if (f->index >= video->nformats)
500 + if (f->mbus_code) {
501 + /* Each entry in formats[] table has unique mbus_code */
505 + i = video_get_pfmt_by_mcode(video, f->mbus_code);
507 + i = video_get_pfmt_by_index(video, f->index);
513 + f->pixelformat = video->formats[i].pixelformat;
518 +static int video_enum_framesizes(struct file *file, void *fh,
519 + struct v4l2_frmsizeenum *fsize)
521 + struct stfcamss_video *video = video_drvdata(file);
527 + for (i = 0; i < video->nformats; i++) {
528 + if (video->formats[i].pixelformat == fsize->pixel_format)
532 + if (i == video->nformats)
535 + fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
536 + fsize->stepwise.min_width = STFCAMSS_FRAME_MIN_WIDTH;
537 + fsize->stepwise.max_width = STFCAMSS_FRAME_MAX_WIDTH;
538 + fsize->stepwise.min_height = STFCAMSS_FRAME_MIN_HEIGHT;
539 + fsize->stepwise.max_height = STFCAMSS_FRAME_MAX_HEIGHT;
540 + fsize->stepwise.step_width = 1;
541 + fsize->stepwise.step_height = 1;
546 +static int video_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
548 + struct stfcamss_video *video = video_drvdata(file);
550 + *f = video->active_fmt;
555 +static int video_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
557 + struct stfcamss_video *video = video_drvdata(file);
560 + if (vb2_is_busy(&video->vb2_q))
563 + ret = __video_try_fmt(video, f);
567 + video->active_fmt = *f;
572 +static int video_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
574 + struct stfcamss_video *video = video_drvdata(file);
576 + return __video_try_fmt(video, f);
579 +static int video_enum_input(struct file *file, void *fh,
580 + struct v4l2_input *input)
582 + if (input->index > 0)
585 + strscpy(input->name, "camera", sizeof(input->name));
586 + input->type = V4L2_INPUT_TYPE_CAMERA;
591 +static int video_g_input(struct file *file, void *fh, unsigned int *input)
598 +static int video_s_input(struct file *file, void *fh, unsigned int input)
600 + return input == 0 ? 0 : -EINVAL;
603 +static int video_g_parm(struct file *file, void *priv,
604 + struct v4l2_streamparm *p)
606 + struct stfcamss_video *video = video_drvdata(file);
607 + struct video_device *vdev = &video->vdev;
608 + struct media_entity *entity;
609 + struct v4l2_subdev *subdev;
610 + struct media_pad *pad;
611 + int ret, is_support = 0;
613 + entity = &vdev->entity;
615 + pad = &entity->pads[0];
616 + if (!(pad->flags & MEDIA_PAD_FL_SINK))
619 + pad = media_pad_remote_pad_first(pad);
620 + if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
623 + entity = pad->entity;
624 + subdev = media_entity_to_v4l2_subdev(entity);
626 + ret = v4l2_g_parm_cap(vdev, subdev, p);
627 + if (ret < 0 && ret != -ENOIOCTLCMD)
633 + return is_support ? 0 : ret;
636 +static int video_s_parm(struct file *file, void *priv,
637 + struct v4l2_streamparm *p)
639 + struct stfcamss_video *video = video_drvdata(file);
640 + struct video_device *vdev = &video->vdev;
641 + struct media_entity *entity;
642 + struct v4l2_subdev *subdev;
643 + struct media_pad *pad;
644 + struct v4l2_streamparm tmp_p;
645 + int ret, is_support = 0;
647 + entity = &vdev->entity;
649 + pad = &entity->pads[0];
650 + if (!(pad->flags & MEDIA_PAD_FL_SINK))
653 + pad = media_pad_remote_pad_first(pad);
654 + if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
657 + entity = pad->entity;
658 + subdev = media_entity_to_v4l2_subdev(entity);
661 + ret = v4l2_s_parm_cap(vdev, subdev, &tmp_p);
662 + if (ret < 0 && ret != -ENOIOCTLCMD)
670 + return is_support ? 0 : ret;
673 +static const struct v4l2_ioctl_ops stf_vid_vin_ioctl_ops = {
674 + .vidioc_querycap = video_querycap,
675 + .vidioc_enum_fmt_vid_cap = video_enum_fmt,
676 + .vidioc_enum_fmt_vid_out = video_enum_fmt,
677 + .vidioc_enum_framesizes = video_enum_framesizes,
678 + .vidioc_g_fmt_vid_cap = video_g_fmt,
679 + .vidioc_s_fmt_vid_cap = video_s_fmt,
680 + .vidioc_try_fmt_vid_cap = video_try_fmt,
681 + .vidioc_g_fmt_vid_out = video_g_fmt,
682 + .vidioc_s_fmt_vid_out = video_s_fmt,
683 + .vidioc_try_fmt_vid_out = video_try_fmt,
684 + .vidioc_reqbufs = vb2_ioctl_reqbufs,
685 + .vidioc_querybuf = vb2_ioctl_querybuf,
686 + .vidioc_qbuf = vb2_ioctl_qbuf,
687 + .vidioc_expbuf = vb2_ioctl_expbuf,
688 + .vidioc_dqbuf = vb2_ioctl_dqbuf,
689 + .vidioc_create_bufs = vb2_ioctl_create_bufs,
690 + .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
691 + .vidioc_streamon = vb2_ioctl_streamon,
692 + .vidioc_streamoff = vb2_ioctl_streamoff,
693 + .vidioc_enum_input = video_enum_input,
694 + .vidioc_g_input = video_g_input,
695 + .vidioc_s_input = video_s_input,
696 + .vidioc_g_parm = video_g_parm,
697 + .vidioc_s_parm = video_s_parm,
700 +static const struct v4l2_ioctl_ops stf_vid_isp_ioctl_ops = {
701 + .vidioc_querycap = video_querycap,
702 + .vidioc_enum_fmt_vid_cap = video_enum_fmt,
703 + .vidioc_enum_fmt_vid_out = video_enum_fmt,
704 + .vidioc_enum_framesizes = video_enum_framesizes,
705 + .vidioc_g_fmt_vid_cap = video_g_fmt,
706 + .vidioc_s_fmt_vid_cap = video_s_fmt,
707 + .vidioc_try_fmt_vid_cap = video_try_fmt,
708 + .vidioc_g_fmt_vid_out = video_g_fmt,
709 + .vidioc_s_fmt_vid_out = video_s_fmt,
710 + .vidioc_try_fmt_vid_out = video_try_fmt,
711 + .vidioc_reqbufs = vb2_ioctl_reqbufs,
712 + .vidioc_querybuf = vb2_ioctl_querybuf,
713 + .vidioc_qbuf = vb2_ioctl_qbuf,
714 + .vidioc_expbuf = vb2_ioctl_expbuf,
715 + .vidioc_dqbuf = vb2_ioctl_dqbuf,
716 + .vidioc_create_bufs = vb2_ioctl_create_bufs,
717 + .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
718 + .vidioc_streamon = vb2_ioctl_streamon,
719 + .vidioc_streamoff = vb2_ioctl_streamoff,
720 + .vidioc_enum_input = video_enum_input,
721 + .vidioc_g_input = video_g_input,
722 + .vidioc_s_input = video_s_input,
723 + .vidioc_g_parm = video_g_parm,
724 + .vidioc_s_parm = video_s_parm,
727 +/* -----------------------------------------------------------------------------
728 + * V4L2 file operations
731 +static int video_open(struct file *file)
733 + struct video_device *vdev = video_devdata(file);
734 + struct stfcamss_video *video = video_drvdata(file);
735 + struct v4l2_fh *vfh;
738 + mutex_lock(&video->lock);
740 + vfh = kzalloc(sizeof(*vfh), GFP_KERNEL);
746 + v4l2_fh_init(vfh, vdev);
749 + file->private_data = vfh;
751 + ret = v4l2_pipeline_pm_get(&vdev->entity);
753 + dev_err(video->stfcamss->dev,
754 + "Failed to power up pipeline: %d\n", ret);
757 + mutex_unlock(&video->lock);
762 + v4l2_fh_release(file);
764 + mutex_unlock(&video->lock);
768 +static int video_release(struct file *file)
770 + struct video_device *vdev = video_devdata(file);
772 + vb2_fop_release(file);
773 + v4l2_pipeline_pm_put(&vdev->entity);
774 + file->private_data = NULL;
779 +static const struct v4l2_file_operations stf_vid_fops = {
780 + .owner = THIS_MODULE,
781 + .unlocked_ioctl = video_ioctl2,
782 + .open = video_open,
783 + .release = video_release,
784 + .poll = vb2_fop_poll,
785 + .mmap = vb2_fop_mmap,
786 + .read = vb2_fop_read,
789 +/* -----------------------------------------------------------------------------
790 + * STFCAMSS video core
793 +static void stf_video_release(struct video_device *vdev)
795 + struct stfcamss_video *video = video_get_drvdata(vdev);
797 + media_entity_cleanup(&vdev->entity);
799 + mutex_destroy(&video->q_lock);
800 + mutex_destroy(&video->lock);
803 +int stf_video_register(struct stfcamss_video *video,
804 + struct v4l2_device *v4l2_dev, const char *name)
806 + struct video_device *vdev;
807 + struct vb2_queue *q;
808 + struct media_pad *pad = &video->pad;
811 + vdev = &video->vdev;
813 + mutex_init(&video->q_lock);
816 + q->drv_priv = video;
817 + q->mem_ops = &vb2_dma_contig_memops;
818 + q->ops = &stf_video_vb2_q_ops;
819 + q->type = video->type;
820 + q->io_modes = VB2_DMABUF | VB2_MMAP | VB2_READ;
821 + q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
822 + q->buf_struct_size = sizeof(struct stfcamss_buffer);
823 + q->dev = video->stfcamss->dev;
824 + q->lock = &video->q_lock;
825 + q->min_buffers_needed = STFCAMSS_MIN_BUFFERS;
826 + ret = vb2_queue_init(q);
828 + dev_err(video->stfcamss->dev,
829 + "Failed to init vb2 queue: %d\n", ret);
833 + pad->flags = MEDIA_PAD_FL_SINK;
834 + ret = media_entity_pads_init(&vdev->entity, 1, pad);
836 + dev_err(video->stfcamss->dev,
837 + "Failed to init video entity: %d\n", ret);
841 + mutex_init(&video->lock);
843 + if (video->id == STF_V_LINE_WR) {
844 + video->formats = formats_pix_wr;
845 + video->nformats = ARRAY_SIZE(formats_pix_wr);
846 + video->bpl_alignment = STFCAMSS_FRAME_WIDTH_ALIGN_8;
847 + vdev->ioctl_ops = &stf_vid_vin_ioctl_ops;
849 + video->formats = formats_pix_isp;
850 + video->nformats = ARRAY_SIZE(formats_pix_isp);
851 + video->bpl_alignment = STFCAMSS_FRAME_WIDTH_ALIGN_8;
852 + vdev->ioctl_ops = &stf_vid_isp_ioctl_ops;
855 + ret = stf_video_init_format(video);
857 + dev_err(video->stfcamss->dev,
858 + "Failed to init format: %d\n", ret);
859 + goto err_vid_init_format;
862 + vdev->fops = &stf_vid_fops;
863 + vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE;
864 + vdev->vfl_dir = VFL_DIR_RX;
865 + vdev->device_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
866 + vdev->release = stf_video_release;
867 + vdev->v4l2_dev = v4l2_dev;
868 + vdev->queue = &video->vb2_q;
869 + vdev->lock = &video->lock;
870 + strscpy(vdev->name, name, sizeof(vdev->name));
872 + ret = video_register_device(vdev, VFL_TYPE_VIDEO, video->id);
874 + dev_err(video->stfcamss->dev,
875 + "Failed to register video device: %d\n", ret);
879 + video_set_drvdata(vdev, video);
883 +err_vid_init_format:
884 + media_entity_cleanup(&vdev->entity);
885 + mutex_destroy(&video->lock);
887 + mutex_destroy(&video->q_lock);
891 +void stf_video_unregister(struct stfcamss_video *video)
893 + vb2_video_unregister_device(&video->vdev);
896 +++ b/drivers/media/platform/starfive/stf_video.h
898 +/* SPDX-License-Identifier: GPL-2.0 */
902 + * StarFive Camera Subsystem - V4L2 device node
904 + * Copyright (C) 2021-2023 StarFive Technology Co., Ltd.
910 +#include <linux/mutex.h>
911 +#include <linux/videodev2.h>
912 +#include <media/v4l2-dev.h>
913 +#include <media/v4l2-fh.h>
914 +#include <media/v4l2-ioctl.h>
915 +#include <media/videobuf2-v4l2.h>
917 +#define STFCAMSS_FRAME_MIN_WIDTH 64
918 +#define STFCAMSS_FRAME_MAX_WIDTH 1920
919 +#define STFCAMSS_FRAME_MIN_HEIGHT 64
920 +#define STFCAMSS_FRAME_MAX_HEIGHT 1080
921 +#define STFCAMSS_FRAME_WIDTH_ALIGN_8 8
922 +#define STFCAMSS_FRAME_WIDTH_ALIGN_128 128
923 +#define STFCAMSS_MIN_BUFFERS 2
925 +#define STFCAMSS_MAX_ENTITY_NAME_LEN 27
927 +enum stf_v_line_id {
933 +struct stfcamss_buffer {
934 + struct vb2_v4l2_buffer vb;
935 + dma_addr_t addr[3];
936 + struct list_head queue;
946 + * struct stfcamss_format_info - ISP media bus format information
947 + * @code: V4L2 media bus format code
948 + * @pixelformat: V4L2 pixel format FCC identifier
949 + * @planes: Number of planes
950 + * @hsub: Horizontal subsampling (for each plane)
951 + * @vsub: Vertical subsampling (for each plane)
952 + * @bpp: Bits per pixel when stored in memory (for each plane)
954 +struct stfcamss_format_info {
958 + struct fract hsub[3];
959 + struct fract vsub[3];
963 +struct stfcamss_video {
964 + struct stfcamss *stfcamss;
966 + struct vb2_queue vb2_q;
967 + struct video_device vdev;
968 + struct media_pad pad;
969 + struct media_pipeline pipe;
970 + struct v4l2_format active_fmt;
971 + enum v4l2_buf_type type;
972 + const struct stfcamss_video_ops *ops;
973 + struct mutex lock; /* serialize device access */
974 + struct mutex q_lock; /* protects the queue */
975 + unsigned int bpl_alignment;
976 + const struct stfcamss_format_info *formats;
977 + unsigned int nformats;
980 +struct stfcamss_video_ops {
981 + int (*queue_buffer)(struct stfcamss_video *vid,
982 + struct stfcamss_buffer *buf);
983 + int (*flush_buffers)(struct stfcamss_video *vid,
984 + enum vb2_buffer_state state);
987 +int stf_video_register(struct stfcamss_video *video,
988 + struct v4l2_device *v4l2_dev, const char *name);
990 +void stf_video_unregister(struct stfcamss_video *video);
992 +#endif /* STF_VIDEO_H */