GNU Linux-libre 4.14.324-gnu1
[releases.git] / drivers / media / platform / qcom / camss-8x16 / camss-video.c
1 /*
2  * camss-video.c
3  *
4  * Qualcomm MSM Camera Subsystem - V4L2 device node
5  *
6  * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
7  * Copyright (C) 2015-2017 Linaro Ltd.
8  *
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.
12  *
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.
17  */
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>
26
27 #include "camss-video.h"
28 #include "camss.h"
29
30 struct fract {
31         u8 numerator;
32         u8 denominator;
33 };
34
35 /*
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)
43  */
44 struct camss_format_info {
45         u32 code;
46         u32 pixelformat;
47         u8 planes;
48         struct fract hsub[3];
49         struct fract vsub[3];
50         unsigned int bpp[3];
51 };
52
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 } },
86 };
87
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 } },
121 };
122
123 /* -----------------------------------------------------------------------------
124  * Helper functions
125  */
126
127 static int video_find_format(u32 code, u32 pixelformat,
128                              const struct camss_format_info *formats,
129                              unsigned int nformats)
130 {
131         int i;
132
133         for (i = 0; i < nformats; i++) {
134                 if (formats[i].code == code &&
135                     formats[i].pixelformat == pixelformat)
136                         return i;
137         }
138
139         for (i = 0; i < nformats; i++)
140                 if (formats[i].code == code)
141                         return i;
142
143         WARN_ON(1);
144
145         return -EINVAL;
146 }
147
148 /*
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
154  *
155  * Fill the output pix structure with information from the input mbus format.
156  *
157  * Return 0 on success or a negative error code otherwise
158  */
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)
163 {
164         unsigned int i;
165         u32 bytesperline;
166
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 *
178                                 bytesperline;
179         }
180
181         return 0;
182 }
183
184 static struct v4l2_subdev *video_remote_subdev(struct camss_video *video,
185                                                u32 *pad)
186 {
187         struct media_pad *remote;
188
189         remote = media_entity_remote_pad(&video->pad);
190
191         if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
192                 return NULL;
193
194         if (pad)
195                 *pad = remote->index;
196
197         return media_entity_to_v4l2_subdev(remote->entity);
198 }
199
200 static int video_get_subdev_format(struct camss_video *video,
201                                    struct v4l2_format *format)
202 {
203         struct v4l2_subdev_format fmt;
204         struct v4l2_subdev *subdev;
205         u32 pad;
206         int ret;
207
208         subdev = video_remote_subdev(video, &pad);
209         if (subdev == NULL)
210                 return -EPIPE;
211
212         fmt.pad = pad;
213         fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
214
215         ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
216         if (ret)
217                 return ret;
218
219         ret = video_find_format(fmt.format.code,
220                                 format->fmt.pix_mp.pixelformat,
221                                 video->formats, video->nformats);
222         if (ret < 0)
223                 return ret;
224
225         format->type = video->type;
226
227         return video_mbus_to_pix_mp(&fmt.format, &format->fmt.pix_mp,
228                                     &video->formats[ret], video->bpl_alignment);
229 }
230
231 /* -----------------------------------------------------------------------------
232  * Video queue operations
233  */
234
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[])
238 {
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;
242         unsigned int i;
243
244         if (*num_planes) {
245                 if (*num_planes != format->num_planes)
246                         return -EINVAL;
247
248                 for (i = 0; i < *num_planes; i++)
249                         if (sizes[i] < format->plane_fmt[i].sizeimage)
250                                 return -EINVAL;
251
252                 return 0;
253         }
254
255         *num_planes = format->num_planes;
256
257         for (i = 0; i < *num_planes; i++)
258                 sizes[i] = format->plane_fmt[i].sizeimage;
259
260         return 0;
261 }
262
263 static int video_buf_init(struct vb2_buffer *vb)
264 {
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,
268                                                    vb);
269         const struct v4l2_pix_format_mplane *format =
270                                                 &video->active_fmt.fmt.pix_mp;
271         struct sg_table *sgt;
272         unsigned int i;
273
274         for (i = 0; i < format->num_planes; i++) {
275                 sgt = vb2_dma_sg_plane_desc(vb, i);
276                 if (!sgt)
277                         return -EFAULT;
278
279                 buffer->addr[i] = sg_dma_address(sgt->sgl);
280         }
281
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 *
288                                 format->height;
289
290         return 0;
291 }
292
293 static int video_buf_prepare(struct vb2_buffer *vb)
294 {
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;
299         unsigned int i;
300
301         for (i = 0; i < format->num_planes; i++) {
302                 if (format->plane_fmt[i].sizeimage > vb2_plane_size(vb, i))
303                         return -EINVAL;
304
305                 vb2_set_plane_payload(vb, i, format->plane_fmt[i].sizeimage);
306         }
307
308         vbuf->field = V4L2_FIELD_NONE;
309
310         return 0;
311 }
312
313 static void video_buf_queue(struct vb2_buffer *vb)
314 {
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,
318                                                    vb);
319
320         video->ops->queue_buffer(video, buffer);
321 }
322
323 static int video_check_format(struct camss_video *video)
324 {
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;
328         int ret;
329
330         sd_pix->pixelformat = pix->pixelformat;
331         ret = video_get_subdev_format(video, &format);
332         if (ret < 0)
333                 return ret;
334
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)
340                 return -EPIPE;
341
342         return 0;
343 }
344
345 static int video_start_streaming(struct vb2_queue *q, unsigned int count)
346 {
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;
352         int ret;
353
354         ret = media_pipeline_start(&vdev->entity, &video->pipe);
355         if (ret < 0)
356                 goto flush_buffers;
357
358         ret = video_check_format(video);
359         if (ret < 0)
360                 goto error;
361
362         entity = &vdev->entity;
363         while (1) {
364                 pad = &entity->pads[0];
365                 if (!(pad->flags & MEDIA_PAD_FL_SINK))
366                         break;
367
368                 pad = media_entity_remote_pad(pad);
369                 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
370                         break;
371
372                 entity = pad->entity;
373                 subdev = media_entity_to_v4l2_subdev(entity);
374
375                 ret = v4l2_subdev_call(subdev, video, s_stream, 1);
376                 if (ret < 0 && ret != -ENOIOCTLCMD)
377                         goto error;
378         }
379
380         return 0;
381
382 error:
383         media_pipeline_stop(&vdev->entity);
384
385 flush_buffers:
386         video->ops->flush_buffers(video, VB2_BUF_STATE_QUEUED);
387
388         return ret;
389 }
390
391 static void video_stop_streaming(struct vb2_queue *q)
392 {
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;
398
399         entity = &vdev->entity;
400         while (1) {
401                 pad = &entity->pads[0];
402                 if (!(pad->flags & MEDIA_PAD_FL_SINK))
403                         break;
404
405                 pad = media_entity_remote_pad(pad);
406                 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
407                         break;
408
409                 entity = pad->entity;
410                 subdev = media_entity_to_v4l2_subdev(entity);
411
412                 v4l2_subdev_call(subdev, video, s_stream, 0);
413         }
414
415         media_pipeline_stop(&vdev->entity);
416
417         video->ops->flush_buffers(video, VB2_BUF_STATE_ERROR);
418 }
419
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,
429 };
430
431 /* -----------------------------------------------------------------------------
432  * V4L2 ioctls
433  */
434
435 static int video_querycap(struct file *file, void *fh,
436                           struct v4l2_capability *cap)
437 {
438         struct camss_video *video = video_drvdata(file);
439
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));
444
445         return 0;
446 }
447
448 static int video_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
449 {
450         struct camss_video *video = video_drvdata(file);
451         int i, j, k;
452
453         if (f->type != video->type)
454                 return -EINVAL;
455
456         if (f->index >= video->nformats)
457                 return -EINVAL;
458
459         /* find index "i" of "k"th unique pixelformat in formats array */
460         k = -1;
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)
465                                 break;
466                 }
467
468                 if (j == i)
469                         k++;
470
471                 if (k == f->index)
472                         break;
473         }
474
475         if (k < f->index)
476                 return -EINVAL;
477
478         f->pixelformat = video->formats[i].pixelformat;
479
480         return 0;
481 }
482
483 static int video_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
484 {
485         struct camss_video *video = video_drvdata(file);
486
487         *f = video->active_fmt;
488
489         return 0;
490 }
491
492 static int __video_try_fmt(struct camss_video *video, struct v4l2_format *f)
493 {
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 };
499         u32 width, height;
500         u32 bpl, lines;
501         int i, j;
502
503         pix_mp = &f->fmt.pix_mp;
504
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,
509                                                   1, 65528);
510                         sizeimage[i] = clamp_t(u32, p->sizeimage,
511                                                bytesperline[i],
512                                                bytesperline[i] * 4096);
513                 }
514
515         for (j = 0; j < video->nformats; j++)
516                 if (pix_mp->pixelformat == video->formats[j].pixelformat)
517                         break;
518
519         if (j == video->nformats)
520                 j = 0; /* default format */
521
522         fi = &video->formats[j];
523         width = pix_mp->width;
524         height = pix_mp->height;
525
526         memset(pix_mp, 0, sizeof(*pix_mp));
527
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;
539         }
540
541         pix_mp->field = V4L2_FIELD_NONE;
542         pix_mp->colorspace = V4L2_COLORSPACE_SRGB;
543         pix_mp->flags = 0;
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);
548
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,
553                                                   1, 65528);
554                         p->sizeimage = clamp_t(u32, p->sizeimage,
555                                                p->bytesperline,
556                                                p->bytesperline * 4096);
557                         lines = p->sizeimage / p->bytesperline;
558
559                         if (p->bytesperline < bytesperline[i])
560                                 p->bytesperline = ALIGN(bytesperline[i], 8);
561
562                         if (p->sizeimage < p->bytesperline * lines)
563                                 p->sizeimage = p->bytesperline * lines;
564
565                         if (p->sizeimage < sizeimage[i])
566                                 p->sizeimage = sizeimage[i];
567                 }
568
569         return 0;
570 }
571
572 static int video_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
573 {
574         struct camss_video *video = video_drvdata(file);
575
576         return __video_try_fmt(video, f);
577 }
578
579 static int video_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
580 {
581         struct camss_video *video = video_drvdata(file);
582         int ret;
583
584         if (vb2_is_busy(&video->vb2_q))
585                 return -EBUSY;
586
587         ret = __video_try_fmt(video, f);
588         if (ret < 0)
589                 return ret;
590
591         video->active_fmt = *f;
592
593         return 0;
594 }
595
596 static int video_enum_input(struct file *file, void *fh,
597                             struct v4l2_input *input)
598 {
599         if (input->index > 0)
600                 return -EINVAL;
601
602         strlcpy(input->name, "camera", sizeof(input->name));
603         input->type = V4L2_INPUT_TYPE_CAMERA;
604
605         return 0;
606 }
607
608 static int video_g_input(struct file *file, void *fh, unsigned int *input)
609 {
610         *input = 0;
611
612         return 0;
613 }
614
615 static int video_s_input(struct file *file, void *fh, unsigned int input)
616 {
617         return input == 0 ? 0 : -EINVAL;
618 }
619
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,
638 };
639
640 /* -----------------------------------------------------------------------------
641  * V4L2 file operations
642  */
643
644 static int video_open(struct file *file)
645 {
646         struct video_device *vdev = video_devdata(file);
647         struct camss_video *video = video_drvdata(file);
648         struct v4l2_fh *vfh;
649         int ret;
650
651         mutex_lock(&video->lock);
652
653         vfh = kzalloc(sizeof(*vfh), GFP_KERNEL);
654         if (vfh == NULL) {
655                 ret = -ENOMEM;
656                 goto error_alloc;
657         }
658
659         v4l2_fh_init(vfh, vdev);
660         v4l2_fh_add(vfh);
661
662         file->private_data = vfh;
663
664         ret = v4l2_pipeline_pm_use(&vdev->entity, 1);
665         if (ret < 0) {
666                 dev_err(video->camss->dev, "Failed to power up pipeline: %d\n",
667                         ret);
668                 goto error_pm_use;
669         }
670
671         mutex_unlock(&video->lock);
672
673         return 0;
674
675 error_pm_use:
676         v4l2_fh_release(file);
677
678 error_alloc:
679         mutex_unlock(&video->lock);
680
681         return ret;
682 }
683
684 static int video_release(struct file *file)
685 {
686         struct video_device *vdev = video_devdata(file);
687
688         vb2_fop_release(file);
689
690         v4l2_pipeline_pm_use(&vdev->entity, 0);
691
692         file->private_data = NULL;
693
694         return 0;
695 }
696
697 static const struct v4l2_file_operations msm_vid_fops = {
698         .owner          = THIS_MODULE,
699         .unlocked_ioctl = video_ioctl2,
700         .open           = video_open,
701         .release        = video_release,
702         .poll           = vb2_fop_poll,
703         .mmap           = vb2_fop_mmap,
704         .read           = vb2_fop_read,
705 };
706
707 /* -----------------------------------------------------------------------------
708  * CAMSS video core
709  */
710
711 static void msm_video_release(struct video_device *vdev)
712 {
713         struct camss_video *video = video_get_drvdata(vdev);
714
715         media_entity_cleanup(&vdev->entity);
716
717         mutex_destroy(&video->q_lock);
718         mutex_destroy(&video->lock);
719
720         if (atomic_dec_and_test(&video->camss->ref_count))
721                 camss_delete(video->camss);
722 }
723
724 /*
725  * msm_video_init_format - Helper function to initialize format
726  * @video: struct camss_video
727  *
728  * Initialize pad format with default value.
729  *
730  * Return 0 on success or a negative error code otherwise
731  */
732 static int msm_video_init_format(struct camss_video *video)
733 {
734         int ret;
735         struct v4l2_format format = {
736                 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
737                 .fmt.pix_mp = {
738                         .width = 1920,
739                         .height = 1080,
740                         .pixelformat = video->formats[0].pixelformat,
741                 },
742         };
743
744         ret = __video_try_fmt(video, &format);
745         if (ret < 0)
746                 return ret;
747
748         video->active_fmt = format;
749
750         return 0;
751 }
752
753 /*
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
758  *
759  * Initialize and register a video device node to a V4L2 device. Also
760  * initialize the vb2 queue.
761  *
762  * Return 0 on success or a negative error code otherwise
763  */
764
765 int msm_video_register(struct camss_video *video, struct v4l2_device *v4l2_dev,
766                        const char *name, int is_pix)
767 {
768         struct media_pad *pad = &video->pad;
769         struct video_device *vdev;
770         struct vb2_queue *q;
771         int ret;
772
773         vdev = &video->vdev;
774
775         mutex_init(&video->q_lock);
776
777         q = &video->vb2_q;
778         q->drv_priv = video;
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);
788         if (ret < 0) {
789                 dev_err(v4l2_dev->dev, "Failed to init vb2 queue: %d\n", ret);
790                 goto error_vb2_init;
791         }
792
793         pad->flags = MEDIA_PAD_FL_SINK;
794         ret = media_entity_pads_init(&vdev->entity, 1, pad);
795         if (ret < 0) {
796                 dev_err(v4l2_dev->dev, "Failed to init video entity: %d\n",
797                         ret);
798                 goto error_media_init;
799         }
800
801         mutex_init(&video->lock);
802
803         video->formats = formats_rdi;
804         video->nformats = ARRAY_SIZE(formats_rdi);
805         if (is_pix) {
806                 video->formats = formats_pix;
807                 video->nformats = ARRAY_SIZE(formats_pix);
808         }
809
810         ret = msm_video_init_format(video);
811         if (ret < 0) {
812                 dev_err(v4l2_dev->dev, "Failed to init format: %d\n", ret);
813                 goto error_video_register;
814         }
815
816         vdev->fops = &msm_vid_fops;
817         vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_STREAMING |
818                                                         V4L2_CAP_READWRITE;
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));
826
827         ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
828         if (ret < 0) {
829                 dev_err(v4l2_dev->dev, "Failed to register video device: %d\n",
830                         ret);
831                 goto error_video_register;
832         }
833
834         video_set_drvdata(vdev, video);
835         atomic_inc(&video->camss->ref_count);
836
837         return 0;
838
839 error_video_register:
840         media_entity_cleanup(&vdev->entity);
841         mutex_destroy(&video->lock);
842 error_media_init:
843         vb2_queue_release(&video->vb2_q);
844 error_vb2_init:
845         mutex_destroy(&video->q_lock);
846
847         return ret;
848 }
849
850 void msm_video_stop_streaming(struct camss_video *video)
851 {
852         if (vb2_is_streaming(&video->vb2_q))
853                 vb2_queue_release(&video->vb2_q);
854 }
855
856 void msm_video_unregister(struct camss_video *video)
857 {
858         atomic_inc(&video->camss->ref_count);
859         video_unregister_device(&video->vdev);
860         atomic_dec(&video->camss->ref_count);
861 }