Merge tag 'media/v5.2-1' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[linux-2.6-block.git] / drivers / staging / media / imx / imx-media-capture.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Video Capture Subdev for Freescale i.MX5/6 SOC
4  *
5  * Copyright (c) 2012-2016 Mentor Graphics Inc.
6  */
7 #include <linux/delay.h>
8 #include <linux/fs.h>
9 #include <linux/module.h>
10 #include <linux/of_platform.h>
11 #include <linux/pinctrl/consumer.h>
12 #include <linux/platform_device.h>
13 #include <linux/sched.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16 #include <linux/timer.h>
17 #include <media/v4l2-ctrls.h>
18 #include <media/v4l2-device.h>
19 #include <media/v4l2-event.h>
20 #include <media/v4l2-fwnode.h>
21 #include <media/v4l2-ioctl.h>
22 #include <media/v4l2-mc.h>
23 #include <media/v4l2-subdev.h>
24 #include <media/videobuf2-dma-contig.h>
25 #include <video/imx-ipu-v3.h>
26 #include <media/imx.h>
27 #include "imx-media.h"
28
29 struct capture_priv {
30         struct imx_media_video_dev vdev;
31
32         struct v4l2_subdev    *src_sd;
33         int                   src_sd_pad;
34         struct device         *dev;
35
36         struct imx_media_dev  *md;
37
38         struct media_pad      vdev_pad;
39
40         struct mutex          mutex;       /* capture device mutex */
41
42         /* the videobuf2 queue */
43         struct vb2_queue       q;
44         /* list of ready imx_media_buffer's from q */
45         struct list_head       ready_q;
46         /* protect ready_q */
47         spinlock_t             q_lock;
48
49         /* controls inherited from subdevs */
50         struct v4l2_ctrl_handler ctrl_hdlr;
51
52         /* misc status */
53         bool                  stop;          /* streaming is stopping */
54 };
55
56 #define to_capture_priv(v) container_of(v, struct capture_priv, vdev)
57
58 /* In bytes, per queue */
59 #define VID_MEM_LIMIT   SZ_64M
60
61 static const struct vb2_ops capture_qops;
62
63 /*
64  * Video ioctls follow
65  */
66
67 static int vidioc_querycap(struct file *file, void *fh,
68                            struct v4l2_capability *cap)
69 {
70         struct capture_priv *priv = video_drvdata(file);
71
72         strscpy(cap->driver, "imx-media-capture", sizeof(cap->driver));
73         strscpy(cap->card, "imx-media-capture", sizeof(cap->card));
74         snprintf(cap->bus_info, sizeof(cap->bus_info),
75                  "platform:%s", priv->src_sd->name);
76
77         return 0;
78 }
79
80 static int capture_enum_framesizes(struct file *file, void *fh,
81                                    struct v4l2_frmsizeenum *fsize)
82 {
83         struct capture_priv *priv = video_drvdata(file);
84         const struct imx_media_pixfmt *cc;
85         struct v4l2_subdev_frame_size_enum fse = {
86                 .index = fsize->index,
87                 .pad = priv->src_sd_pad,
88                 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
89         };
90         int ret;
91
92         cc = imx_media_find_format(fsize->pixel_format, CS_SEL_ANY, true);
93         if (!cc)
94                 return -EINVAL;
95
96         fse.code = cc->codes[0];
97
98         ret = v4l2_subdev_call(priv->src_sd, pad, enum_frame_size, NULL, &fse);
99         if (ret)
100                 return ret;
101
102         if (fse.min_width == fse.max_width &&
103             fse.min_height == fse.max_height) {
104                 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
105                 fsize->discrete.width = fse.min_width;
106                 fsize->discrete.height = fse.min_height;
107         } else {
108                 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
109                 fsize->stepwise.min_width = fse.min_width;
110                 fsize->stepwise.max_width = fse.max_width;
111                 fsize->stepwise.min_height = fse.min_height;
112                 fsize->stepwise.max_height = fse.max_height;
113                 fsize->stepwise.step_width = 1;
114                 fsize->stepwise.step_height = 1;
115         }
116
117         return 0;
118 }
119
120 static int capture_enum_frameintervals(struct file *file, void *fh,
121                                        struct v4l2_frmivalenum *fival)
122 {
123         struct capture_priv *priv = video_drvdata(file);
124         const struct imx_media_pixfmt *cc;
125         struct v4l2_subdev_frame_interval_enum fie = {
126                 .index = fival->index,
127                 .pad = priv->src_sd_pad,
128                 .width = fival->width,
129                 .height = fival->height,
130                 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
131         };
132         int ret;
133
134         cc = imx_media_find_format(fival->pixel_format, CS_SEL_ANY, true);
135         if (!cc)
136                 return -EINVAL;
137
138         fie.code = cc->codes[0];
139
140         ret = v4l2_subdev_call(priv->src_sd, pad, enum_frame_interval,
141                                NULL, &fie);
142         if (ret)
143                 return ret;
144
145         fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
146         fival->discrete = fie.interval;
147
148         return 0;
149 }
150
151 static int capture_enum_fmt_vid_cap(struct file *file, void *fh,
152                                     struct v4l2_fmtdesc *f)
153 {
154         struct capture_priv *priv = video_drvdata(file);
155         const struct imx_media_pixfmt *cc_src;
156         struct v4l2_subdev_format fmt_src;
157         u32 fourcc;
158         int ret;
159
160         fmt_src.pad = priv->src_sd_pad;
161         fmt_src.which = V4L2_SUBDEV_FORMAT_ACTIVE;
162         ret = v4l2_subdev_call(priv->src_sd, pad, get_fmt, NULL, &fmt_src);
163         if (ret) {
164                 v4l2_err(priv->src_sd, "failed to get src_sd format\n");
165                 return ret;
166         }
167
168         cc_src = imx_media_find_ipu_format(fmt_src.format.code, CS_SEL_ANY);
169         if (cc_src) {
170                 u32 cs_sel = (cc_src->cs == IPUV3_COLORSPACE_YUV) ?
171                         CS_SEL_YUV : CS_SEL_RGB;
172
173                 ret = imx_media_enum_format(&fourcc, f->index, cs_sel);
174                 if (ret)
175                         return ret;
176         } else {
177                 cc_src = imx_media_find_mbus_format(fmt_src.format.code,
178                                                     CS_SEL_ANY, true);
179                 if (WARN_ON(!cc_src))
180                         return -EINVAL;
181
182                 if (f->index != 0)
183                         return -EINVAL;
184                 fourcc = cc_src->fourcc;
185         }
186
187         f->pixelformat = fourcc;
188
189         return 0;
190 }
191
192 static int capture_g_fmt_vid_cap(struct file *file, void *fh,
193                                  struct v4l2_format *f)
194 {
195         struct capture_priv *priv = video_drvdata(file);
196
197         *f = priv->vdev.fmt;
198
199         return 0;
200 }
201
202 static int __capture_try_fmt_vid_cap(struct capture_priv *priv,
203                                      struct v4l2_subdev_format *fmt_src,
204                                      struct v4l2_format *f,
205                                      struct v4l2_rect *compose)
206 {
207         const struct imx_media_pixfmt *cc, *cc_src;
208
209         cc_src = imx_media_find_ipu_format(fmt_src->format.code, CS_SEL_ANY);
210         if (cc_src) {
211                 u32 fourcc, cs_sel;
212
213                 cs_sel = (cc_src->cs == IPUV3_COLORSPACE_YUV) ?
214                         CS_SEL_YUV : CS_SEL_RGB;
215                 fourcc = f->fmt.pix.pixelformat;
216
217                 cc = imx_media_find_format(fourcc, cs_sel, false);
218                 if (!cc) {
219                         imx_media_enum_format(&fourcc, 0, cs_sel);
220                         cc = imx_media_find_format(fourcc, cs_sel, false);
221                 }
222         } else {
223                 cc_src = imx_media_find_mbus_format(fmt_src->format.code,
224                                                     CS_SEL_ANY, true);
225                 if (WARN_ON(!cc_src))
226                         return -EINVAL;
227
228                 cc = cc_src;
229         }
230
231         /* allow IDMAC interweave but enforce field order from source */
232         if (V4L2_FIELD_IS_INTERLACED(f->fmt.pix.field)) {
233                 switch (fmt_src->format.field) {
234                 case V4L2_FIELD_SEQ_TB:
235                         fmt_src->format.field = V4L2_FIELD_INTERLACED_TB;
236                         break;
237                 case V4L2_FIELD_SEQ_BT:
238                         fmt_src->format.field = V4L2_FIELD_INTERLACED_BT;
239                         break;
240                 default:
241                         break;
242                 }
243         }
244
245         imx_media_mbus_fmt_to_pix_fmt(&f->fmt.pix, compose,
246                                       &fmt_src->format, cc);
247
248         return 0;
249 }
250
251 static int capture_try_fmt_vid_cap(struct file *file, void *fh,
252                                    struct v4l2_format *f)
253 {
254         struct capture_priv *priv = video_drvdata(file);
255         struct v4l2_subdev_format fmt_src;
256         int ret;
257
258         fmt_src.pad = priv->src_sd_pad;
259         fmt_src.which = V4L2_SUBDEV_FORMAT_ACTIVE;
260         ret = v4l2_subdev_call(priv->src_sd, pad, get_fmt, NULL, &fmt_src);
261         if (ret)
262                 return ret;
263
264         return __capture_try_fmt_vid_cap(priv, &fmt_src, f, NULL);
265 }
266
267 static int capture_s_fmt_vid_cap(struct file *file, void *fh,
268                                  struct v4l2_format *f)
269 {
270         struct capture_priv *priv = video_drvdata(file);
271         struct v4l2_subdev_format fmt_src;
272         struct v4l2_rect compose;
273         int ret;
274
275         if (vb2_is_busy(&priv->q)) {
276                 v4l2_err(priv->src_sd, "%s queue busy\n", __func__);
277                 return -EBUSY;
278         }
279
280         fmt_src.pad = priv->src_sd_pad;
281         fmt_src.which = V4L2_SUBDEV_FORMAT_ACTIVE;
282         ret = v4l2_subdev_call(priv->src_sd, pad, get_fmt, NULL, &fmt_src);
283         if (ret)
284                 return ret;
285
286         ret = __capture_try_fmt_vid_cap(priv, &fmt_src, f, &compose);
287         if (ret)
288                 return ret;
289
290         priv->vdev.fmt.fmt.pix = f->fmt.pix;
291         priv->vdev.cc = imx_media_find_format(f->fmt.pix.pixelformat,
292                                               CS_SEL_ANY, true);
293         priv->vdev.compose = compose;
294
295         return 0;
296 }
297
298 static int capture_querystd(struct file *file, void *fh, v4l2_std_id *std)
299 {
300         struct capture_priv *priv = video_drvdata(file);
301
302         return v4l2_subdev_call(priv->src_sd, video, querystd, std);
303 }
304
305 static int capture_g_std(struct file *file, void *fh, v4l2_std_id *std)
306 {
307         struct capture_priv *priv = video_drvdata(file);
308
309         return v4l2_subdev_call(priv->src_sd, video, g_std, std);
310 }
311
312 static int capture_s_std(struct file *file, void *fh, v4l2_std_id std)
313 {
314         struct capture_priv *priv = video_drvdata(file);
315
316         if (vb2_is_busy(&priv->q))
317                 return -EBUSY;
318
319         return v4l2_subdev_call(priv->src_sd, video, s_std, std);
320 }
321
322 static int capture_g_selection(struct file *file, void *fh,
323                                struct v4l2_selection *s)
324 {
325         struct capture_priv *priv = video_drvdata(file);
326
327         switch (s->target) {
328         case V4L2_SEL_TGT_COMPOSE:
329         case V4L2_SEL_TGT_COMPOSE_DEFAULT:
330         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
331                 /* The compose rectangle is fixed to the source format. */
332                 s->r = priv->vdev.compose;
333                 break;
334         case V4L2_SEL_TGT_COMPOSE_PADDED:
335                 /*
336                  * The hardware writes with a configurable but fixed DMA burst
337                  * size. If the source format width is not burst size aligned,
338                  * the written frame contains padding to the right.
339                  */
340                 s->r.left = 0;
341                 s->r.top = 0;
342                 s->r.width = priv->vdev.fmt.fmt.pix.width;
343                 s->r.height = priv->vdev.fmt.fmt.pix.height;
344                 break;
345         default:
346                 return -EINVAL;
347         }
348
349         return 0;
350 }
351
352 static int capture_g_parm(struct file *file, void *fh,
353                           struct v4l2_streamparm *a)
354 {
355         struct capture_priv *priv = video_drvdata(file);
356         struct v4l2_subdev_frame_interval fi;
357         int ret;
358
359         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
360                 return -EINVAL;
361
362         memset(&fi, 0, sizeof(fi));
363         fi.pad = priv->src_sd_pad;
364         ret = v4l2_subdev_call(priv->src_sd, video, g_frame_interval, &fi);
365         if (ret < 0)
366                 return ret;
367
368         a->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
369         a->parm.capture.timeperframe = fi.interval;
370
371         return 0;
372 }
373
374 static int capture_s_parm(struct file *file, void *fh,
375                           struct v4l2_streamparm *a)
376 {
377         struct capture_priv *priv = video_drvdata(file);
378         struct v4l2_subdev_frame_interval fi;
379         int ret;
380
381         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
382                 return -EINVAL;
383
384         memset(&fi, 0, sizeof(fi));
385         fi.pad = priv->src_sd_pad;
386         fi.interval = a->parm.capture.timeperframe;
387         ret = v4l2_subdev_call(priv->src_sd, video, s_frame_interval, &fi);
388         if (ret < 0)
389                 return ret;
390
391         a->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
392         a->parm.capture.timeperframe = fi.interval;
393
394         return 0;
395 }
396
397 static int capture_subscribe_event(struct v4l2_fh *fh,
398                                    const struct v4l2_event_subscription *sub)
399 {
400         switch (sub->type) {
401         case V4L2_EVENT_IMX_FRAME_INTERVAL_ERROR:
402                 return v4l2_event_subscribe(fh, sub, 0, NULL);
403         case V4L2_EVENT_SOURCE_CHANGE:
404                 return v4l2_src_change_event_subscribe(fh, sub);
405         case V4L2_EVENT_CTRL:
406                 return v4l2_ctrl_subscribe_event(fh, sub);
407         default:
408                 return -EINVAL;
409         }
410 }
411
412 static const struct v4l2_ioctl_ops capture_ioctl_ops = {
413         .vidioc_querycap        = vidioc_querycap,
414
415         .vidioc_enum_framesizes = capture_enum_framesizes,
416         .vidioc_enum_frameintervals = capture_enum_frameintervals,
417
418         .vidioc_enum_fmt_vid_cap        = capture_enum_fmt_vid_cap,
419         .vidioc_g_fmt_vid_cap           = capture_g_fmt_vid_cap,
420         .vidioc_try_fmt_vid_cap         = capture_try_fmt_vid_cap,
421         .vidioc_s_fmt_vid_cap           = capture_s_fmt_vid_cap,
422
423         .vidioc_querystd        = capture_querystd,
424         .vidioc_g_std           = capture_g_std,
425         .vidioc_s_std           = capture_s_std,
426
427         .vidioc_g_selection     = capture_g_selection,
428
429         .vidioc_g_parm          = capture_g_parm,
430         .vidioc_s_parm          = capture_s_parm,
431
432         .vidioc_reqbufs         = vb2_ioctl_reqbufs,
433         .vidioc_create_bufs     = vb2_ioctl_create_bufs,
434         .vidioc_prepare_buf     = vb2_ioctl_prepare_buf,
435         .vidioc_querybuf        = vb2_ioctl_querybuf,
436         .vidioc_qbuf            = vb2_ioctl_qbuf,
437         .vidioc_dqbuf           = vb2_ioctl_dqbuf,
438         .vidioc_expbuf          = vb2_ioctl_expbuf,
439         .vidioc_streamon        = vb2_ioctl_streamon,
440         .vidioc_streamoff       = vb2_ioctl_streamoff,
441
442         .vidioc_subscribe_event = capture_subscribe_event,
443         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
444 };
445
446 /*
447  * Queue operations
448  */
449
450 static int capture_queue_setup(struct vb2_queue *vq,
451                                unsigned int *nbuffers,
452                                unsigned int *nplanes,
453                                unsigned int sizes[],
454                                struct device *alloc_devs[])
455 {
456         struct capture_priv *priv = vb2_get_drv_priv(vq);
457         struct v4l2_pix_format *pix = &priv->vdev.fmt.fmt.pix;
458         unsigned int count = *nbuffers;
459
460         if (vq->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
461                 return -EINVAL;
462
463         if (*nplanes) {
464                 if (*nplanes != 1 || sizes[0] < pix->sizeimage)
465                         return -EINVAL;
466                 count += vq->num_buffers;
467         }
468
469         count = min_t(__u32, VID_MEM_LIMIT / pix->sizeimage, count);
470
471         if (*nplanes)
472                 *nbuffers = (count < vq->num_buffers) ? 0 :
473                         count - vq->num_buffers;
474         else
475                 *nbuffers = count;
476
477         *nplanes = 1;
478         sizes[0] = pix->sizeimage;
479
480         return 0;
481 }
482
483 static int capture_buf_init(struct vb2_buffer *vb)
484 {
485         struct imx_media_buffer *buf = to_imx_media_vb(vb);
486
487         INIT_LIST_HEAD(&buf->list);
488
489         return 0;
490 }
491
492 static int capture_buf_prepare(struct vb2_buffer *vb)
493 {
494         struct vb2_queue *vq = vb->vb2_queue;
495         struct capture_priv *priv = vb2_get_drv_priv(vq);
496         struct v4l2_pix_format *pix = &priv->vdev.fmt.fmt.pix;
497
498         if (vb2_plane_size(vb, 0) < pix->sizeimage) {
499                 v4l2_err(priv->src_sd,
500                          "data will not fit into plane (%lu < %lu)\n",
501                          vb2_plane_size(vb, 0), (long)pix->sizeimage);
502                 return -EINVAL;
503         }
504
505         vb2_set_plane_payload(vb, 0, pix->sizeimage);
506
507         return 0;
508 }
509
510 static void capture_buf_queue(struct vb2_buffer *vb)
511 {
512         struct capture_priv *priv = vb2_get_drv_priv(vb->vb2_queue);
513         struct imx_media_buffer *buf = to_imx_media_vb(vb);
514         unsigned long flags;
515
516         spin_lock_irqsave(&priv->q_lock, flags);
517
518         list_add_tail(&buf->list, &priv->ready_q);
519
520         spin_unlock_irqrestore(&priv->q_lock, flags);
521 }
522
523 static int capture_start_streaming(struct vb2_queue *vq, unsigned int count)
524 {
525         struct capture_priv *priv = vb2_get_drv_priv(vq);
526         struct imx_media_buffer *buf, *tmp;
527         unsigned long flags;
528         int ret;
529
530         ret = imx_media_pipeline_set_stream(priv->md, &priv->src_sd->entity,
531                                             true);
532         if (ret) {
533                 v4l2_err(priv->src_sd, "pipeline start failed with %d\n", ret);
534                 goto return_bufs;
535         }
536
537         priv->stop = false;
538
539         return 0;
540
541 return_bufs:
542         spin_lock_irqsave(&priv->q_lock, flags);
543         list_for_each_entry_safe(buf, tmp, &priv->ready_q, list) {
544                 list_del(&buf->list);
545                 vb2_buffer_done(&buf->vbuf.vb2_buf, VB2_BUF_STATE_QUEUED);
546         }
547         spin_unlock_irqrestore(&priv->q_lock, flags);
548         return ret;
549 }
550
551 static void capture_stop_streaming(struct vb2_queue *vq)
552 {
553         struct capture_priv *priv = vb2_get_drv_priv(vq);
554         struct imx_media_buffer *frame;
555         struct imx_media_buffer *tmp;
556         unsigned long flags;
557         int ret;
558
559         spin_lock_irqsave(&priv->q_lock, flags);
560         priv->stop = true;
561         spin_unlock_irqrestore(&priv->q_lock, flags);
562
563         ret = imx_media_pipeline_set_stream(priv->md, &priv->src_sd->entity,
564                                             false);
565         if (ret)
566                 v4l2_warn(priv->src_sd, "pipeline stop failed with %d\n", ret);
567
568         /* release all active buffers */
569         spin_lock_irqsave(&priv->q_lock, flags);
570         list_for_each_entry_safe(frame, tmp, &priv->ready_q, list) {
571                 list_del(&frame->list);
572                 vb2_buffer_done(&frame->vbuf.vb2_buf, VB2_BUF_STATE_ERROR);
573         }
574         spin_unlock_irqrestore(&priv->q_lock, flags);
575 }
576
577 static const struct vb2_ops capture_qops = {
578         .queue_setup     = capture_queue_setup,
579         .buf_init        = capture_buf_init,
580         .buf_prepare     = capture_buf_prepare,
581         .buf_queue       = capture_buf_queue,
582         .wait_prepare    = vb2_ops_wait_prepare,
583         .wait_finish     = vb2_ops_wait_finish,
584         .start_streaming = capture_start_streaming,
585         .stop_streaming  = capture_stop_streaming,
586 };
587
588 /*
589  * File operations
590  */
591 static int capture_open(struct file *file)
592 {
593         struct capture_priv *priv = video_drvdata(file);
594         struct video_device *vfd = priv->vdev.vfd;
595         int ret;
596
597         if (mutex_lock_interruptible(&priv->mutex))
598                 return -ERESTARTSYS;
599
600         ret = v4l2_fh_open(file);
601         if (ret)
602                 v4l2_err(priv->src_sd, "v4l2_fh_open failed\n");
603
604         ret = v4l2_pipeline_pm_use(&vfd->entity, 1);
605         if (ret)
606                 v4l2_fh_release(file);
607
608         mutex_unlock(&priv->mutex);
609         return ret;
610 }
611
612 static int capture_release(struct file *file)
613 {
614         struct capture_priv *priv = video_drvdata(file);
615         struct video_device *vfd = priv->vdev.vfd;
616         struct vb2_queue *vq = &priv->q;
617         int ret = 0;
618
619         mutex_lock(&priv->mutex);
620
621         if (file->private_data == vq->owner) {
622                 vb2_queue_release(vq);
623                 vq->owner = NULL;
624         }
625
626         v4l2_pipeline_pm_use(&vfd->entity, 0);
627
628         v4l2_fh_release(file);
629         mutex_unlock(&priv->mutex);
630         return ret;
631 }
632
633 static const struct v4l2_file_operations capture_fops = {
634         .owner          = THIS_MODULE,
635         .open           = capture_open,
636         .release        = capture_release,
637         .poll           = vb2_fop_poll,
638         .unlocked_ioctl = video_ioctl2,
639         .mmap           = vb2_fop_mmap,
640 };
641
642 static struct video_device capture_videodev = {
643         .fops           = &capture_fops,
644         .ioctl_ops      = &capture_ioctl_ops,
645         .minor          = -1,
646         .release        = video_device_release,
647         .vfl_dir        = VFL_DIR_RX,
648         .tvnorms        = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
649         .device_caps    = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING,
650 };
651
652 void imx_media_capture_device_set_format(struct imx_media_video_dev *vdev,
653                                          const struct v4l2_pix_format *pix,
654                                          const struct v4l2_rect *compose)
655 {
656         struct capture_priv *priv = to_capture_priv(vdev);
657
658         mutex_lock(&priv->mutex);
659         priv->vdev.fmt.fmt.pix = *pix;
660         priv->vdev.cc = imx_media_find_format(pix->pixelformat, CS_SEL_ANY,
661                                               true);
662         priv->vdev.compose = *compose;
663         mutex_unlock(&priv->mutex);
664 }
665 EXPORT_SYMBOL_GPL(imx_media_capture_device_set_format);
666
667 struct imx_media_buffer *
668 imx_media_capture_device_next_buf(struct imx_media_video_dev *vdev)
669 {
670         struct capture_priv *priv = to_capture_priv(vdev);
671         struct imx_media_buffer *buf = NULL;
672         unsigned long flags;
673
674         spin_lock_irqsave(&priv->q_lock, flags);
675
676         /* get next queued buffer */
677         if (!list_empty(&priv->ready_q)) {
678                 buf = list_entry(priv->ready_q.next, struct imx_media_buffer,
679                                  list);
680                 list_del(&buf->list);
681         }
682
683         spin_unlock_irqrestore(&priv->q_lock, flags);
684
685         return buf;
686 }
687 EXPORT_SYMBOL_GPL(imx_media_capture_device_next_buf);
688
689 void imx_media_capture_device_error(struct imx_media_video_dev *vdev)
690 {
691         struct capture_priv *priv = to_capture_priv(vdev);
692         struct vb2_queue *vq = &priv->q;
693         unsigned long flags;
694
695         if (!vb2_is_streaming(vq))
696                 return;
697
698         spin_lock_irqsave(&priv->q_lock, flags);
699         vb2_queue_error(vq);
700         spin_unlock_irqrestore(&priv->q_lock, flags);
701 }
702 EXPORT_SYMBOL_GPL(imx_media_capture_device_error);
703
704 int imx_media_capture_device_register(struct imx_media_dev *md,
705                                       struct imx_media_video_dev *vdev)
706 {
707         struct capture_priv *priv = to_capture_priv(vdev);
708         struct v4l2_subdev *sd = priv->src_sd;
709         struct video_device *vfd = vdev->vfd;
710         struct vb2_queue *vq = &priv->q;
711         struct v4l2_subdev_format fmt_src;
712         int ret;
713
714         priv->md = md;
715
716         vfd->v4l2_dev = sd->v4l2_dev;
717
718         ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
719         if (ret) {
720                 v4l2_err(sd, "Failed to register video device\n");
721                 return ret;
722         }
723
724         vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
725         vq->io_modes = VB2_MMAP | VB2_DMABUF;
726         vq->drv_priv = priv;
727         vq->buf_struct_size = sizeof(struct imx_media_buffer);
728         vq->ops = &capture_qops;
729         vq->mem_ops = &vb2_dma_contig_memops;
730         vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
731         vq->lock = &priv->mutex;
732         vq->min_buffers_needed = 2;
733         vq->dev = priv->dev;
734
735         ret = vb2_queue_init(vq);
736         if (ret) {
737                 v4l2_err(sd, "vb2_queue_init failed\n");
738                 goto unreg;
739         }
740
741         INIT_LIST_HEAD(&priv->ready_q);
742
743         priv->vdev_pad.flags = MEDIA_PAD_FL_SINK;
744         ret = media_entity_pads_init(&vfd->entity, 1, &priv->vdev_pad);
745         if (ret) {
746                 v4l2_err(sd, "failed to init dev pad\n");
747                 goto unreg;
748         }
749
750         /* create the link from the src_sd devnode pad to device node */
751         ret = media_create_pad_link(&sd->entity, priv->src_sd_pad,
752                                     &vfd->entity, 0, 0);
753         if (ret) {
754                 v4l2_err(sd, "failed to create link to device node\n");
755                 goto unreg;
756         }
757
758         /* setup default format */
759         fmt_src.pad = priv->src_sd_pad;
760         fmt_src.which = V4L2_SUBDEV_FORMAT_ACTIVE;
761         v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt_src);
762         if (ret) {
763                 v4l2_err(sd, "failed to get src_sd format\n");
764                 goto unreg;
765         }
766
767         vdev->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
768         imx_media_mbus_fmt_to_pix_fmt(&vdev->fmt.fmt.pix, &vdev->compose,
769                                       &fmt_src.format, NULL);
770         vdev->cc = imx_media_find_format(vdev->fmt.fmt.pix.pixelformat,
771                                          CS_SEL_ANY, false);
772
773         v4l2_info(sd, "Registered %s as /dev/%s\n", vfd->name,
774                   video_device_node_name(vfd));
775
776         vfd->ctrl_handler = &priv->ctrl_hdlr;
777
778         return 0;
779 unreg:
780         video_unregister_device(vfd);
781         return ret;
782 }
783 EXPORT_SYMBOL_GPL(imx_media_capture_device_register);
784
785 void imx_media_capture_device_unregister(struct imx_media_video_dev *vdev)
786 {
787         struct capture_priv *priv = to_capture_priv(vdev);
788         struct video_device *vfd = priv->vdev.vfd;
789
790         mutex_lock(&priv->mutex);
791
792         if (video_is_registered(vfd)) {
793                 video_unregister_device(vfd);
794                 media_entity_cleanup(&vfd->entity);
795         }
796
797         mutex_unlock(&priv->mutex);
798 }
799 EXPORT_SYMBOL_GPL(imx_media_capture_device_unregister);
800
801 struct imx_media_video_dev *
802 imx_media_capture_device_init(struct v4l2_subdev *src_sd, int pad)
803 {
804         struct capture_priv *priv;
805         struct video_device *vfd;
806
807         priv = devm_kzalloc(src_sd->dev, sizeof(*priv), GFP_KERNEL);
808         if (!priv)
809                 return ERR_PTR(-ENOMEM);
810
811         priv->src_sd = src_sd;
812         priv->src_sd_pad = pad;
813         priv->dev = src_sd->dev;
814
815         mutex_init(&priv->mutex);
816         spin_lock_init(&priv->q_lock);
817
818         snprintf(capture_videodev.name, sizeof(capture_videodev.name),
819                  "%s capture", src_sd->name);
820
821         vfd = video_device_alloc();
822         if (!vfd)
823                 return ERR_PTR(-ENOMEM);
824
825         *vfd = capture_videodev;
826         vfd->lock = &priv->mutex;
827         vfd->queue = &priv->q;
828         priv->vdev.vfd = vfd;
829
830         INIT_LIST_HEAD(&priv->vdev.list);
831
832         video_set_drvdata(vfd, priv);
833
834         v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
835
836         return &priv->vdev;
837 }
838 EXPORT_SYMBOL_GPL(imx_media_capture_device_init);
839
840 void imx_media_capture_device_remove(struct imx_media_video_dev *vdev)
841 {
842         struct capture_priv *priv = to_capture_priv(vdev);
843
844         v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
845 }
846 EXPORT_SYMBOL_GPL(imx_media_capture_device_remove);
847
848 MODULE_DESCRIPTION("i.MX5/6 v4l2 video capture interface driver");
849 MODULE_AUTHOR("Steve Longerbeam <steve_longerbeam@mentor.com>");
850 MODULE_LICENSE("GPL");