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