4 * Qualcomm MSM Camera Subsystem - V4L2 device node
6 * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
7 * Copyright (C) 2015-2017 Linaro Ltd.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 and
11 * only version 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 #include <linux/slab.h>
19 #include <media/media-entity.h>
20 #include <media/v4l2-dev.h>
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-ioctl.h>
23 #include <media/v4l2-mc.h>
24 #include <media/videobuf-core.h>
25 #include <media/videobuf2-dma-sg.h>
27 #include "camss-video.h"
36 * struct camss_format_info - ISP media bus format information
37 * @code: V4L2 media bus format code
38 * @pixelformat: V4L2 pixel format FCC identifier
39 * @planes: Number of planes
40 * @hsub: Horizontal subsampling (for each plane)
41 * @vsub: Vertical subsampling (for each plane)
42 * @bpp: Bits per pixel when stored in memory (for each plane)
44 struct camss_format_info {
53 static const struct camss_format_info formats_rdi[] = {
54 { MEDIA_BUS_FMT_UYVY8_2X8, V4L2_PIX_FMT_UYVY, 1,
55 { { 1, 1 } }, { { 1, 1 } }, { 16 } },
56 { MEDIA_BUS_FMT_VYUY8_2X8, V4L2_PIX_FMT_VYUY, 1,
57 { { 1, 1 } }, { { 1, 1 } }, { 16 } },
58 { MEDIA_BUS_FMT_YUYV8_2X8, V4L2_PIX_FMT_YUYV, 1,
59 { { 1, 1 } }, { { 1, 1 } }, { 16 } },
60 { MEDIA_BUS_FMT_YVYU8_2X8, V4L2_PIX_FMT_YVYU, 1,
61 { { 1, 1 } }, { { 1, 1 } }, { 16 } },
62 { MEDIA_BUS_FMT_SBGGR8_1X8, V4L2_PIX_FMT_SBGGR8, 1,
63 { { 1, 1 } }, { { 1, 1 } }, { 8 } },
64 { MEDIA_BUS_FMT_SGBRG8_1X8, V4L2_PIX_FMT_SGBRG8, 1,
65 { { 1, 1 } }, { { 1, 1 } }, { 8 } },
66 { MEDIA_BUS_FMT_SGRBG8_1X8, V4L2_PIX_FMT_SGRBG8, 1,
67 { { 1, 1 } }, { { 1, 1 } }, { 8 } },
68 { MEDIA_BUS_FMT_SRGGB8_1X8, V4L2_PIX_FMT_SRGGB8, 1,
69 { { 1, 1 } }, { { 1, 1 } }, { 8 } },
70 { MEDIA_BUS_FMT_SBGGR10_1X10, V4L2_PIX_FMT_SBGGR10P, 1,
71 { { 1, 1 } }, { { 1, 1 } }, { 10 } },
72 { MEDIA_BUS_FMT_SGBRG10_1X10, V4L2_PIX_FMT_SGBRG10P, 1,
73 { { 1, 1 } }, { { 1, 1 } }, { 10 } },
74 { MEDIA_BUS_FMT_SGRBG10_1X10, V4L2_PIX_FMT_SGRBG10P, 1,
75 { { 1, 1 } }, { { 1, 1 } }, { 10 } },
76 { MEDIA_BUS_FMT_SRGGB10_1X10, V4L2_PIX_FMT_SRGGB10P, 1,
77 { { 1, 1 } }, { { 1, 1 } }, { 10 } },
78 { MEDIA_BUS_FMT_SBGGR12_1X12, V4L2_PIX_FMT_SBGGR12P, 1,
79 { { 1, 1 } }, { { 1, 1 } }, { 12 } },
80 { MEDIA_BUS_FMT_SGBRG12_1X12, V4L2_PIX_FMT_SGBRG12P, 1,
81 { { 1, 1 } }, { { 1, 1 } }, { 12 } },
82 { MEDIA_BUS_FMT_SGRBG12_1X12, V4L2_PIX_FMT_SGRBG12P, 1,
83 { { 1, 1 } }, { { 1, 1 } }, { 12 } },
84 { MEDIA_BUS_FMT_SRGGB12_1X12, V4L2_PIX_FMT_SRGGB12P, 1,
85 { { 1, 1 } }, { { 1, 1 } }, { 12 } },
88 static const struct camss_format_info formats_pix[] = {
89 { MEDIA_BUS_FMT_YUYV8_1_5X8, V4L2_PIX_FMT_NV12, 1,
90 { { 1, 1 } }, { { 2, 3 } }, { 8 } },
91 { MEDIA_BUS_FMT_YVYU8_1_5X8, V4L2_PIX_FMT_NV12, 1,
92 { { 1, 1 } }, { { 2, 3 } }, { 8 } },
93 { MEDIA_BUS_FMT_UYVY8_1_5X8, V4L2_PIX_FMT_NV12, 1,
94 { { 1, 1 } }, { { 2, 3 } }, { 8 } },
95 { MEDIA_BUS_FMT_VYUY8_1_5X8, V4L2_PIX_FMT_NV12, 1,
96 { { 1, 1 } }, { { 2, 3 } }, { 8 } },
97 { MEDIA_BUS_FMT_YUYV8_1_5X8, V4L2_PIX_FMT_NV21, 1,
98 { { 1, 1 } }, { { 2, 3 } }, { 8 } },
99 { MEDIA_BUS_FMT_YVYU8_1_5X8, V4L2_PIX_FMT_NV21, 1,
100 { { 1, 1 } }, { { 2, 3 } }, { 8 } },
101 { MEDIA_BUS_FMT_UYVY8_1_5X8, V4L2_PIX_FMT_NV21, 1,
102 { { 1, 1 } }, { { 2, 3 } }, { 8 } },
103 { MEDIA_BUS_FMT_VYUY8_1_5X8, V4L2_PIX_FMT_NV21, 1,
104 { { 1, 1 } }, { { 2, 3 } }, { 8 } },
105 { MEDIA_BUS_FMT_YUYV8_2X8, V4L2_PIX_FMT_NV16, 1,
106 { { 1, 1 } }, { { 1, 2 } }, { 8 } },
107 { MEDIA_BUS_FMT_YVYU8_2X8, V4L2_PIX_FMT_NV16, 1,
108 { { 1, 1 } }, { { 1, 2 } }, { 8 } },
109 { MEDIA_BUS_FMT_UYVY8_2X8, V4L2_PIX_FMT_NV16, 1,
110 { { 1, 1 } }, { { 1, 2 } }, { 8 } },
111 { MEDIA_BUS_FMT_VYUY8_2X8, V4L2_PIX_FMT_NV16, 1,
112 { { 1, 1 } }, { { 1, 2 } }, { 8 } },
113 { MEDIA_BUS_FMT_YUYV8_2X8, V4L2_PIX_FMT_NV61, 1,
114 { { 1, 1 } }, { { 1, 2 } }, { 8 } },
115 { MEDIA_BUS_FMT_YVYU8_2X8, V4L2_PIX_FMT_NV61, 1,
116 { { 1, 1 } }, { { 1, 2 } }, { 8 } },
117 { MEDIA_BUS_FMT_UYVY8_2X8, V4L2_PIX_FMT_NV61, 1,
118 { { 1, 1 } }, { { 1, 2 } }, { 8 } },
119 { MEDIA_BUS_FMT_VYUY8_2X8, V4L2_PIX_FMT_NV61, 1,
120 { { 1, 1 } }, { { 1, 2 } }, { 8 } },
123 /* -----------------------------------------------------------------------------
127 static int video_find_format(u32 code, u32 pixelformat,
128 const struct camss_format_info *formats,
129 unsigned int nformats)
133 for (i = 0; i < nformats; i++) {
134 if (formats[i].code == code &&
135 formats[i].pixelformat == pixelformat)
139 for (i = 0; i < nformats; i++)
140 if (formats[i].code == code)
149 * video_mbus_to_pix_mp - Convert v4l2_mbus_framefmt to v4l2_pix_format_mplane
150 * @mbus: v4l2_mbus_framefmt format (input)
151 * @pix: v4l2_pix_format_mplane format (output)
152 * @f: a pointer to formats array element to be used for the conversion
153 * @alignment: bytesperline alignment value
155 * Fill the output pix structure with information from the input mbus format.
157 * Return 0 on success or a negative error code otherwise
159 static int video_mbus_to_pix_mp(const struct v4l2_mbus_framefmt *mbus,
160 struct v4l2_pix_format_mplane *pix,
161 const struct camss_format_info *f,
162 unsigned int alignment)
167 memset(pix, 0, sizeof(*pix));
168 v4l2_fill_pix_format_mplane(pix, mbus);
169 pix->pixelformat = f->pixelformat;
170 pix->num_planes = f->planes;
171 for (i = 0; i < pix->num_planes; i++) {
172 bytesperline = pix->width / f->hsub[i].numerator *
173 f->hsub[i].denominator * f->bpp[i] / 8;
174 bytesperline = ALIGN(bytesperline, alignment);
175 pix->plane_fmt[i].bytesperline = bytesperline;
176 pix->plane_fmt[i].sizeimage = pix->height /
177 f->vsub[i].numerator * f->vsub[i].denominator *
184 static struct v4l2_subdev *video_remote_subdev(struct camss_video *video,
187 struct media_pad *remote;
189 remote = media_entity_remote_pad(&video->pad);
191 if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
195 *pad = remote->index;
197 return media_entity_to_v4l2_subdev(remote->entity);
200 static int video_get_subdev_format(struct camss_video *video,
201 struct v4l2_format *format)
203 struct v4l2_subdev_format fmt;
204 struct v4l2_subdev *subdev;
208 subdev = video_remote_subdev(video, &pad);
213 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
215 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
219 ret = video_find_format(fmt.format.code,
220 format->fmt.pix_mp.pixelformat,
221 video->formats, video->nformats);
225 format->type = video->type;
227 return video_mbus_to_pix_mp(&fmt.format, &format->fmt.pix_mp,
228 &video->formats[ret], video->bpl_alignment);
231 /* -----------------------------------------------------------------------------
232 * Video queue operations
235 static int video_queue_setup(struct vb2_queue *q,
236 unsigned int *num_buffers, unsigned int *num_planes,
237 unsigned int sizes[], struct device *alloc_devs[])
239 struct camss_video *video = vb2_get_drv_priv(q);
240 const struct v4l2_pix_format_mplane *format =
241 &video->active_fmt.fmt.pix_mp;
245 if (*num_planes != format->num_planes)
248 for (i = 0; i < *num_planes; i++)
249 if (sizes[i] < format->plane_fmt[i].sizeimage)
255 *num_planes = format->num_planes;
257 for (i = 0; i < *num_planes; i++)
258 sizes[i] = format->plane_fmt[i].sizeimage;
263 static int video_buf_init(struct vb2_buffer *vb)
265 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
266 struct camss_video *video = vb2_get_drv_priv(vb->vb2_queue);
267 struct camss_buffer *buffer = container_of(vbuf, struct camss_buffer,
269 const struct v4l2_pix_format_mplane *format =
270 &video->active_fmt.fmt.pix_mp;
271 struct sg_table *sgt;
274 for (i = 0; i < format->num_planes; i++) {
275 sgt = vb2_dma_sg_plane_desc(vb, i);
279 buffer->addr[i] = sg_dma_address(sgt->sgl);
282 if (format->pixelformat == V4L2_PIX_FMT_NV12 ||
283 format->pixelformat == V4L2_PIX_FMT_NV21 ||
284 format->pixelformat == V4L2_PIX_FMT_NV16 ||
285 format->pixelformat == V4L2_PIX_FMT_NV61)
286 buffer->addr[1] = buffer->addr[0] +
287 format->plane_fmt[0].bytesperline *
293 static int video_buf_prepare(struct vb2_buffer *vb)
295 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
296 struct camss_video *video = vb2_get_drv_priv(vb->vb2_queue);
297 const struct v4l2_pix_format_mplane *format =
298 &video->active_fmt.fmt.pix_mp;
301 for (i = 0; i < format->num_planes; i++) {
302 if (format->plane_fmt[i].sizeimage > vb2_plane_size(vb, i))
305 vb2_set_plane_payload(vb, i, format->plane_fmt[i].sizeimage);
308 vbuf->field = V4L2_FIELD_NONE;
313 static void video_buf_queue(struct vb2_buffer *vb)
315 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
316 struct camss_video *video = vb2_get_drv_priv(vb->vb2_queue);
317 struct camss_buffer *buffer = container_of(vbuf, struct camss_buffer,
320 video->ops->queue_buffer(video, buffer);
323 static int video_check_format(struct camss_video *video)
325 struct v4l2_pix_format_mplane *pix = &video->active_fmt.fmt.pix_mp;
326 struct v4l2_format format;
327 struct v4l2_pix_format_mplane *sd_pix = &format.fmt.pix_mp;
330 sd_pix->pixelformat = pix->pixelformat;
331 ret = video_get_subdev_format(video, &format);
335 if (pix->pixelformat != sd_pix->pixelformat ||
336 pix->height != sd_pix->height ||
337 pix->width != sd_pix->width ||
338 pix->num_planes != sd_pix->num_planes ||
339 pix->field != format.fmt.pix_mp.field)
345 static int video_start_streaming(struct vb2_queue *q, unsigned int count)
347 struct camss_video *video = vb2_get_drv_priv(q);
348 struct video_device *vdev = &video->vdev;
349 struct media_entity *entity;
350 struct media_pad *pad;
351 struct v4l2_subdev *subdev;
354 ret = media_pipeline_start(&vdev->entity, &video->pipe);
358 ret = video_check_format(video);
362 entity = &vdev->entity;
364 pad = &entity->pads[0];
365 if (!(pad->flags & MEDIA_PAD_FL_SINK))
368 pad = media_entity_remote_pad(pad);
369 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
372 entity = pad->entity;
373 subdev = media_entity_to_v4l2_subdev(entity);
375 ret = v4l2_subdev_call(subdev, video, s_stream, 1);
376 if (ret < 0 && ret != -ENOIOCTLCMD)
383 media_pipeline_stop(&vdev->entity);
385 video->ops->flush_buffers(video, VB2_BUF_STATE_QUEUED);
390 static void video_stop_streaming(struct vb2_queue *q)
392 struct camss_video *video = vb2_get_drv_priv(q);
393 struct video_device *vdev = &video->vdev;
394 struct media_entity *entity;
395 struct media_pad *pad;
396 struct v4l2_subdev *subdev;
398 entity = &vdev->entity;
400 pad = &entity->pads[0];
401 if (!(pad->flags & MEDIA_PAD_FL_SINK))
404 pad = media_entity_remote_pad(pad);
405 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
408 entity = pad->entity;
409 subdev = media_entity_to_v4l2_subdev(entity);
411 v4l2_subdev_call(subdev, video, s_stream, 0);
414 media_pipeline_stop(&vdev->entity);
416 video->ops->flush_buffers(video, VB2_BUF_STATE_ERROR);
419 static const struct vb2_ops msm_video_vb2_q_ops = {
420 .queue_setup = video_queue_setup,
421 .wait_prepare = vb2_ops_wait_prepare,
422 .wait_finish = vb2_ops_wait_finish,
423 .buf_init = video_buf_init,
424 .buf_prepare = video_buf_prepare,
425 .buf_queue = video_buf_queue,
426 .start_streaming = video_start_streaming,
427 .stop_streaming = video_stop_streaming,
430 /* -----------------------------------------------------------------------------
434 static int video_querycap(struct file *file, void *fh,
435 struct v4l2_capability *cap)
437 struct camss_video *video = video_drvdata(file);
439 strlcpy(cap->driver, "qcom-camss", sizeof(cap->driver));
440 strlcpy(cap->card, "Qualcomm Camera Subsystem", sizeof(cap->card));
441 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
442 dev_name(video->camss->dev));
447 static int video_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
449 struct camss_video *video = video_drvdata(file);
452 if (f->type != video->type)
455 if (f->index >= video->nformats)
458 /* find index "i" of "k"th unique pixelformat in formats array */
460 for (i = 0; i < video->nformats; i++) {
461 for (j = 0; j < i; j++) {
462 if (video->formats[i].pixelformat ==
463 video->formats[j].pixelformat)
477 f->pixelformat = video->formats[i].pixelformat;
482 static int video_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
484 struct camss_video *video = video_drvdata(file);
486 *f = video->active_fmt;
491 static int __video_try_fmt(struct camss_video *video, struct v4l2_format *f)
493 struct v4l2_pix_format_mplane *pix_mp;
494 const struct camss_format_info *fi;
495 struct v4l2_plane_pix_format *p;
496 u32 bytesperline[3] = { 0 };
497 u32 sizeimage[3] = { 0 };
502 pix_mp = &f->fmt.pix_mp;
504 if (video->line_based)
505 for (i = 0; i < pix_mp->num_planes && i < 3; i++) {
506 p = &pix_mp->plane_fmt[i];
507 bytesperline[i] = clamp_t(u32, p->bytesperline,
509 sizeimage[i] = clamp_t(u32, p->sizeimage,
511 bytesperline[i] * 4096);
514 for (j = 0; j < video->nformats; j++)
515 if (pix_mp->pixelformat == video->formats[j].pixelformat)
518 if (j == video->nformats)
519 j = 0; /* default format */
521 fi = &video->formats[j];
522 width = pix_mp->width;
523 height = pix_mp->height;
525 memset(pix_mp, 0, sizeof(*pix_mp));
527 pix_mp->pixelformat = fi->pixelformat;
528 pix_mp->width = clamp_t(u32, width, 1, 8191);
529 pix_mp->height = clamp_t(u32, height, 1, 8191);
530 pix_mp->num_planes = fi->planes;
531 for (i = 0; i < pix_mp->num_planes; i++) {
532 bpl = pix_mp->width / fi->hsub[i].numerator *
533 fi->hsub[i].denominator * fi->bpp[i] / 8;
534 bpl = ALIGN(bpl, video->bpl_alignment);
535 pix_mp->plane_fmt[i].bytesperline = bpl;
536 pix_mp->plane_fmt[i].sizeimage = pix_mp->height /
537 fi->vsub[i].numerator * fi->vsub[i].denominator * bpl;
540 pix_mp->field = V4L2_FIELD_NONE;
541 pix_mp->colorspace = V4L2_COLORSPACE_SRGB;
543 pix_mp->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(pix_mp->colorspace);
544 pix_mp->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
545 pix_mp->colorspace, pix_mp->ycbcr_enc);
546 pix_mp->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(pix_mp->colorspace);
548 if (video->line_based)
549 for (i = 0; i < pix_mp->num_planes; i++) {
550 p = &pix_mp->plane_fmt[i];
551 p->bytesperline = clamp_t(u32, p->bytesperline,
553 p->sizeimage = clamp_t(u32, p->sizeimage,
555 p->bytesperline * 4096);
556 lines = p->sizeimage / p->bytesperline;
558 if (p->bytesperline < bytesperline[i])
559 p->bytesperline = ALIGN(bytesperline[i], 8);
561 if (p->sizeimage < p->bytesperline * lines)
562 p->sizeimage = p->bytesperline * lines;
564 if (p->sizeimage < sizeimage[i])
565 p->sizeimage = sizeimage[i];
571 static int video_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
573 struct camss_video *video = video_drvdata(file);
575 return __video_try_fmt(video, f);
578 static int video_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
580 struct camss_video *video = video_drvdata(file);
583 if (vb2_is_busy(&video->vb2_q))
586 ret = __video_try_fmt(video, f);
590 video->active_fmt = *f;
595 static int video_enum_input(struct file *file, void *fh,
596 struct v4l2_input *input)
598 if (input->index > 0)
601 strlcpy(input->name, "camera", sizeof(input->name));
602 input->type = V4L2_INPUT_TYPE_CAMERA;
607 static int video_g_input(struct file *file, void *fh, unsigned int *input)
614 static int video_s_input(struct file *file, void *fh, unsigned int input)
616 return input == 0 ? 0 : -EINVAL;
619 static const struct v4l2_ioctl_ops msm_vid_ioctl_ops = {
620 .vidioc_querycap = video_querycap,
621 .vidioc_enum_fmt_vid_cap_mplane = video_enum_fmt,
622 .vidioc_g_fmt_vid_cap_mplane = video_g_fmt,
623 .vidioc_s_fmt_vid_cap_mplane = video_s_fmt,
624 .vidioc_try_fmt_vid_cap_mplane = video_try_fmt,
625 .vidioc_reqbufs = vb2_ioctl_reqbufs,
626 .vidioc_querybuf = vb2_ioctl_querybuf,
627 .vidioc_qbuf = vb2_ioctl_qbuf,
628 .vidioc_expbuf = vb2_ioctl_expbuf,
629 .vidioc_dqbuf = vb2_ioctl_dqbuf,
630 .vidioc_create_bufs = vb2_ioctl_create_bufs,
631 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
632 .vidioc_streamon = vb2_ioctl_streamon,
633 .vidioc_streamoff = vb2_ioctl_streamoff,
634 .vidioc_enum_input = video_enum_input,
635 .vidioc_g_input = video_g_input,
636 .vidioc_s_input = video_s_input,
639 /* -----------------------------------------------------------------------------
640 * V4L2 file operations
643 static int video_open(struct file *file)
645 struct video_device *vdev = video_devdata(file);
646 struct camss_video *video = video_drvdata(file);
650 mutex_lock(&video->lock);
652 vfh = kzalloc(sizeof(*vfh), GFP_KERNEL);
658 v4l2_fh_init(vfh, vdev);
661 file->private_data = vfh;
663 ret = v4l2_pipeline_pm_use(&vdev->entity, 1);
665 dev_err(video->camss->dev, "Failed to power up pipeline: %d\n",
670 mutex_unlock(&video->lock);
675 v4l2_fh_release(file);
678 mutex_unlock(&video->lock);
683 static int video_release(struct file *file)
685 struct video_device *vdev = video_devdata(file);
687 vb2_fop_release(file);
689 v4l2_pipeline_pm_use(&vdev->entity, 0);
691 file->private_data = NULL;
696 static const struct v4l2_file_operations msm_vid_fops = {
697 .owner = THIS_MODULE,
698 .unlocked_ioctl = video_ioctl2,
700 .release = video_release,
701 .poll = vb2_fop_poll,
702 .mmap = vb2_fop_mmap,
703 .read = vb2_fop_read,
706 /* -----------------------------------------------------------------------------
710 static void msm_video_release(struct video_device *vdev)
712 struct camss_video *video = video_get_drvdata(vdev);
714 media_entity_cleanup(&vdev->entity);
716 mutex_destroy(&video->q_lock);
717 mutex_destroy(&video->lock);
719 if (atomic_dec_and_test(&video->camss->ref_count))
720 camss_delete(video->camss);
724 * msm_video_init_format - Helper function to initialize format
725 * @video: struct camss_video
727 * Initialize pad format with default value.
729 * Return 0 on success or a negative error code otherwise
731 static int msm_video_init_format(struct camss_video *video)
734 struct v4l2_format format = {
735 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
739 .pixelformat = video->formats[0].pixelformat,
743 ret = __video_try_fmt(video, &format);
747 video->active_fmt = format;
753 * msm_video_register - Register a video device node
754 * @video: struct camss_video
755 * @v4l2_dev: V4L2 device
756 * @name: name to be used for the video device node
758 * Initialize and register a video device node to a V4L2 device. Also
759 * initialize the vb2 queue.
761 * Return 0 on success or a negative error code otherwise
764 int msm_video_register(struct camss_video *video, struct v4l2_device *v4l2_dev,
765 const char *name, int is_pix)
767 struct media_pad *pad = &video->pad;
768 struct video_device *vdev;
774 mutex_init(&video->q_lock);
778 q->mem_ops = &vb2_dma_sg_memops;
779 q->ops = &msm_video_vb2_q_ops;
780 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
781 q->io_modes = VB2_DMABUF | VB2_MMAP | VB2_READ;
782 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
783 q->buf_struct_size = sizeof(struct camss_buffer);
784 q->dev = video->camss->dev;
785 q->lock = &video->q_lock;
786 ret = vb2_queue_init(q);
788 dev_err(v4l2_dev->dev, "Failed to init vb2 queue: %d\n", ret);
792 pad->flags = MEDIA_PAD_FL_SINK;
793 ret = media_entity_pads_init(&vdev->entity, 1, pad);
795 dev_err(v4l2_dev->dev, "Failed to init video entity: %d\n",
797 goto error_media_init;
800 mutex_init(&video->lock);
802 video->formats = formats_rdi;
803 video->nformats = ARRAY_SIZE(formats_rdi);
805 video->formats = formats_pix;
806 video->nformats = ARRAY_SIZE(formats_pix);
809 ret = msm_video_init_format(video);
811 dev_err(v4l2_dev->dev, "Failed to init format: %d\n", ret);
812 goto error_video_register;
815 vdev->fops = &msm_vid_fops;
816 vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_STREAMING |
818 vdev->ioctl_ops = &msm_vid_ioctl_ops;
819 vdev->release = msm_video_release;
820 vdev->v4l2_dev = v4l2_dev;
821 vdev->vfl_dir = VFL_DIR_RX;
822 vdev->queue = &video->vb2_q;
823 vdev->lock = &video->lock;
824 strlcpy(vdev->name, name, sizeof(vdev->name));
826 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
828 dev_err(v4l2_dev->dev, "Failed to register video device: %d\n",
830 goto error_video_register;
833 video_set_drvdata(vdev, video);
834 atomic_inc(&video->camss->ref_count);
838 error_video_register:
839 media_entity_cleanup(&vdev->entity);
840 mutex_destroy(&video->lock);
842 vb2_queue_release(&video->vb2_q);
844 mutex_destroy(&video->q_lock);
849 void msm_video_stop_streaming(struct camss_video *video)
851 if (vb2_is_streaming(&video->vb2_q))
852 vb2_queue_release(&video->vb2_q);
855 void msm_video_unregister(struct camss_video *video)
857 atomic_inc(&video->camss->ref_count);
858 video_unregister_device(&video->vdev);
859 atomic_dec(&video->camss->ref_count);