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);
386 video->ops->flush_buffers(video, VB2_BUF_STATE_QUEUED);
391 static void video_stop_streaming(struct vb2_queue *q)
393 struct camss_video *video = vb2_get_drv_priv(q);
394 struct video_device *vdev = &video->vdev;
395 struct media_entity *entity;
396 struct media_pad *pad;
397 struct v4l2_subdev *subdev;
399 entity = &vdev->entity;
401 pad = &entity->pads[0];
402 if (!(pad->flags & MEDIA_PAD_FL_SINK))
405 pad = media_entity_remote_pad(pad);
406 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
409 entity = pad->entity;
410 subdev = media_entity_to_v4l2_subdev(entity);
412 v4l2_subdev_call(subdev, video, s_stream, 0);
415 media_pipeline_stop(&vdev->entity);
417 video->ops->flush_buffers(video, VB2_BUF_STATE_ERROR);
420 static const struct vb2_ops msm_video_vb2_q_ops = {
421 .queue_setup = video_queue_setup,
422 .wait_prepare = vb2_ops_wait_prepare,
423 .wait_finish = vb2_ops_wait_finish,
424 .buf_init = video_buf_init,
425 .buf_prepare = video_buf_prepare,
426 .buf_queue = video_buf_queue,
427 .start_streaming = video_start_streaming,
428 .stop_streaming = video_stop_streaming,
431 /* -----------------------------------------------------------------------------
435 static int video_querycap(struct file *file, void *fh,
436 struct v4l2_capability *cap)
438 struct camss_video *video = video_drvdata(file);
440 strlcpy(cap->driver, "qcom-camss", sizeof(cap->driver));
441 strlcpy(cap->card, "Qualcomm Camera Subsystem", sizeof(cap->card));
442 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
443 dev_name(video->camss->dev));
448 static int video_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
450 struct camss_video *video = video_drvdata(file);
453 if (f->type != video->type)
456 if (f->index >= video->nformats)
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 f->pixelformat = video->formats[i].pixelformat;
483 static int video_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
485 struct camss_video *video = video_drvdata(file);
487 *f = video->active_fmt;
492 static int __video_try_fmt(struct camss_video *video, struct v4l2_format *f)
494 struct v4l2_pix_format_mplane *pix_mp;
495 const struct camss_format_info *fi;
496 struct v4l2_plane_pix_format *p;
497 u32 bytesperline[3] = { 0 };
498 u32 sizeimage[3] = { 0 };
503 pix_mp = &f->fmt.pix_mp;
505 if (video->line_based)
506 for (i = 0; i < pix_mp->num_planes && i < 3; i++) {
507 p = &pix_mp->plane_fmt[i];
508 bytesperline[i] = clamp_t(u32, p->bytesperline,
510 sizeimage[i] = clamp_t(u32, p->sizeimage,
512 bytesperline[i] * 4096);
515 for (j = 0; j < video->nformats; j++)
516 if (pix_mp->pixelformat == video->formats[j].pixelformat)
519 if (j == video->nformats)
520 j = 0; /* default format */
522 fi = &video->formats[j];
523 width = pix_mp->width;
524 height = pix_mp->height;
526 memset(pix_mp, 0, sizeof(*pix_mp));
528 pix_mp->pixelformat = fi->pixelformat;
529 pix_mp->width = clamp_t(u32, width, 1, 8191);
530 pix_mp->height = clamp_t(u32, height, 1, 8191);
531 pix_mp->num_planes = fi->planes;
532 for (i = 0; i < pix_mp->num_planes; i++) {
533 bpl = pix_mp->width / fi->hsub[i].numerator *
534 fi->hsub[i].denominator * fi->bpp[i] / 8;
535 bpl = ALIGN(bpl, video->bpl_alignment);
536 pix_mp->plane_fmt[i].bytesperline = bpl;
537 pix_mp->plane_fmt[i].sizeimage = pix_mp->height /
538 fi->vsub[i].numerator * fi->vsub[i].denominator * bpl;
541 pix_mp->field = V4L2_FIELD_NONE;
542 pix_mp->colorspace = V4L2_COLORSPACE_SRGB;
544 pix_mp->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(pix_mp->colorspace);
545 pix_mp->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
546 pix_mp->colorspace, pix_mp->ycbcr_enc);
547 pix_mp->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(pix_mp->colorspace);
549 if (video->line_based)
550 for (i = 0; i < pix_mp->num_planes; i++) {
551 p = &pix_mp->plane_fmt[i];
552 p->bytesperline = clamp_t(u32, p->bytesperline,
554 p->sizeimage = clamp_t(u32, p->sizeimage,
556 p->bytesperline * 4096);
557 lines = p->sizeimage / p->bytesperline;
559 if (p->bytesperline < bytesperline[i])
560 p->bytesperline = ALIGN(bytesperline[i], 8);
562 if (p->sizeimage < p->bytesperline * lines)
563 p->sizeimage = p->bytesperline * lines;
565 if (p->sizeimage < sizeimage[i])
566 p->sizeimage = sizeimage[i];
572 static int video_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
574 struct camss_video *video = video_drvdata(file);
576 return __video_try_fmt(video, f);
579 static int video_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
581 struct camss_video *video = video_drvdata(file);
584 if (vb2_is_busy(&video->vb2_q))
587 ret = __video_try_fmt(video, f);
591 video->active_fmt = *f;
596 static int video_enum_input(struct file *file, void *fh,
597 struct v4l2_input *input)
599 if (input->index > 0)
602 strlcpy(input->name, "camera", sizeof(input->name));
603 input->type = V4L2_INPUT_TYPE_CAMERA;
608 static int video_g_input(struct file *file, void *fh, unsigned int *input)
615 static int video_s_input(struct file *file, void *fh, unsigned int input)
617 return input == 0 ? 0 : -EINVAL;
620 static const struct v4l2_ioctl_ops msm_vid_ioctl_ops = {
621 .vidioc_querycap = video_querycap,
622 .vidioc_enum_fmt_vid_cap_mplane = video_enum_fmt,
623 .vidioc_g_fmt_vid_cap_mplane = video_g_fmt,
624 .vidioc_s_fmt_vid_cap_mplane = video_s_fmt,
625 .vidioc_try_fmt_vid_cap_mplane = video_try_fmt,
626 .vidioc_reqbufs = vb2_ioctl_reqbufs,
627 .vidioc_querybuf = vb2_ioctl_querybuf,
628 .vidioc_qbuf = vb2_ioctl_qbuf,
629 .vidioc_expbuf = vb2_ioctl_expbuf,
630 .vidioc_dqbuf = vb2_ioctl_dqbuf,
631 .vidioc_create_bufs = vb2_ioctl_create_bufs,
632 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
633 .vidioc_streamon = vb2_ioctl_streamon,
634 .vidioc_streamoff = vb2_ioctl_streamoff,
635 .vidioc_enum_input = video_enum_input,
636 .vidioc_g_input = video_g_input,
637 .vidioc_s_input = video_s_input,
640 /* -----------------------------------------------------------------------------
641 * V4L2 file operations
644 static int video_open(struct file *file)
646 struct video_device *vdev = video_devdata(file);
647 struct camss_video *video = video_drvdata(file);
651 mutex_lock(&video->lock);
653 vfh = kzalloc(sizeof(*vfh), GFP_KERNEL);
659 v4l2_fh_init(vfh, vdev);
662 file->private_data = vfh;
664 ret = v4l2_pipeline_pm_use(&vdev->entity, 1);
666 dev_err(video->camss->dev, "Failed to power up pipeline: %d\n",
671 mutex_unlock(&video->lock);
676 v4l2_fh_release(file);
679 mutex_unlock(&video->lock);
684 static int video_release(struct file *file)
686 struct video_device *vdev = video_devdata(file);
688 vb2_fop_release(file);
690 v4l2_pipeline_pm_use(&vdev->entity, 0);
692 file->private_data = NULL;
697 static const struct v4l2_file_operations msm_vid_fops = {
698 .owner = THIS_MODULE,
699 .unlocked_ioctl = video_ioctl2,
701 .release = video_release,
702 .poll = vb2_fop_poll,
703 .mmap = vb2_fop_mmap,
704 .read = vb2_fop_read,
707 /* -----------------------------------------------------------------------------
711 static void msm_video_release(struct video_device *vdev)
713 struct camss_video *video = video_get_drvdata(vdev);
715 media_entity_cleanup(&vdev->entity);
717 mutex_destroy(&video->q_lock);
718 mutex_destroy(&video->lock);
720 if (atomic_dec_and_test(&video->camss->ref_count))
721 camss_delete(video->camss);
725 * msm_video_init_format - Helper function to initialize format
726 * @video: struct camss_video
728 * Initialize pad format with default value.
730 * Return 0 on success or a negative error code otherwise
732 static int msm_video_init_format(struct camss_video *video)
735 struct v4l2_format format = {
736 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
740 .pixelformat = video->formats[0].pixelformat,
744 ret = __video_try_fmt(video, &format);
748 video->active_fmt = format;
754 * msm_video_register - Register a video device node
755 * @video: struct camss_video
756 * @v4l2_dev: V4L2 device
757 * @name: name to be used for the video device node
759 * Initialize and register a video device node to a V4L2 device. Also
760 * initialize the vb2 queue.
762 * Return 0 on success or a negative error code otherwise
765 int msm_video_register(struct camss_video *video, struct v4l2_device *v4l2_dev,
766 const char *name, int is_pix)
768 struct media_pad *pad = &video->pad;
769 struct video_device *vdev;
775 mutex_init(&video->q_lock);
779 q->mem_ops = &vb2_dma_sg_memops;
780 q->ops = &msm_video_vb2_q_ops;
781 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
782 q->io_modes = VB2_DMABUF | VB2_MMAP | VB2_READ;
783 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
784 q->buf_struct_size = sizeof(struct camss_buffer);
785 q->dev = video->camss->dev;
786 q->lock = &video->q_lock;
787 ret = vb2_queue_init(q);
789 dev_err(v4l2_dev->dev, "Failed to init vb2 queue: %d\n", ret);
793 pad->flags = MEDIA_PAD_FL_SINK;
794 ret = media_entity_pads_init(&vdev->entity, 1, pad);
796 dev_err(v4l2_dev->dev, "Failed to init video entity: %d\n",
798 goto error_media_init;
801 mutex_init(&video->lock);
803 video->formats = formats_rdi;
804 video->nformats = ARRAY_SIZE(formats_rdi);
806 video->formats = formats_pix;
807 video->nformats = ARRAY_SIZE(formats_pix);
810 ret = msm_video_init_format(video);
812 dev_err(v4l2_dev->dev, "Failed to init format: %d\n", ret);
813 goto error_video_register;
816 vdev->fops = &msm_vid_fops;
817 vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_STREAMING |
819 vdev->ioctl_ops = &msm_vid_ioctl_ops;
820 vdev->release = msm_video_release;
821 vdev->v4l2_dev = v4l2_dev;
822 vdev->vfl_dir = VFL_DIR_RX;
823 vdev->queue = &video->vb2_q;
824 vdev->lock = &video->lock;
825 strlcpy(vdev->name, name, sizeof(vdev->name));
827 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
829 dev_err(v4l2_dev->dev, "Failed to register video device: %d\n",
831 goto error_video_register;
834 video_set_drvdata(vdev, video);
835 atomic_inc(&video->camss->ref_count);
839 error_video_register:
840 media_entity_cleanup(&vdev->entity);
841 mutex_destroy(&video->lock);
843 vb2_queue_release(&video->vb2_q);
845 mutex_destroy(&video->q_lock);
850 void msm_video_stop_streaming(struct camss_video *video)
852 if (vb2_is_streaming(&video->vb2_q))
853 vb2_queue_release(&video->vb2_q);
856 void msm_video_unregister(struct camss_video *video)
858 atomic_inc(&video->camss->ref_count);
859 video_unregister_device(&video->vdev);
860 atomic_dec(&video->camss->ref_count);