[media] s5p-fimc: Convert to the new control framework
[linux-2.6/libata-dev.git] / drivers / media / video / s5p-fimc / fimc-capture.c
blob0c237a776f8db5fd57c9b382c692c780e07049c1
1 /*
2 * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver
4 * Copyright (C) 2010 - 2011 Samsung Electronics Co., Ltd.
5 * Author: Sylwester Nawrocki, <s.nawrocki@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/types.h>
15 #include <linux/errno.h>
16 #include <linux/bug.h>
17 #include <linux/interrupt.h>
18 #include <linux/device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/list.h>
21 #include <linux/slab.h>
23 #include <linux/videodev2.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-ioctl.h>
26 #include <media/v4l2-mem2mem.h>
27 #include <media/videobuf2-core.h>
28 #include <media/videobuf2-dma-contig.h>
30 #include "fimc-mdevice.h"
31 #include "fimc-core.h"
33 static void fimc_capture_state_cleanup(struct fimc_dev *fimc)
35 struct fimc_vid_cap *cap = &fimc->vid_cap;
36 struct fimc_vid_buffer *buf;
37 unsigned long flags;
39 spin_lock_irqsave(&fimc->slock, flags);
40 fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_PEND |
41 1 << ST_CAPT_SHUT | 1 << ST_CAPT_STREAM);
43 fimc->vid_cap.active_buf_cnt = 0;
45 /* Release buffers that were enqueued in the driver by videobuf2. */
46 while (!list_empty(&cap->pending_buf_q)) {
47 buf = pending_queue_pop(cap);
48 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
51 while (!list_empty(&cap->active_buf_q)) {
52 buf = active_queue_pop(cap);
53 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
56 spin_unlock_irqrestore(&fimc->slock, flags);
59 static int fimc_stop_capture(struct fimc_dev *fimc)
61 struct fimc_vid_cap *cap = &fimc->vid_cap;
62 unsigned long flags;
64 if (!fimc_capture_active(fimc))
65 return 0;
67 spin_lock_irqsave(&fimc->slock, flags);
68 set_bit(ST_CAPT_SHUT, &fimc->state);
69 fimc_deactivate_capture(fimc);
70 spin_unlock_irqrestore(&fimc->slock, flags);
72 wait_event_timeout(fimc->irq_queue,
73 !test_bit(ST_CAPT_SHUT, &fimc->state),
74 FIMC_SHUTDOWN_TIMEOUT);
76 v4l2_subdev_call(cap->sd, video, s_stream, 0);
78 fimc_capture_state_cleanup(fimc);
79 dbg("state: 0x%lx", fimc->state);
80 return 0;
84 static int start_streaming(struct vb2_queue *q, unsigned int count)
86 struct fimc_ctx *ctx = q->drv_priv;
87 struct fimc_dev *fimc = ctx->fimc_dev;
88 struct s5p_fimc_isp_info *isp_info;
89 int min_bufs;
90 int ret;
92 fimc_hw_reset(fimc);
94 ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_stream, 1);
95 if (ret && ret != -ENOIOCTLCMD)
96 goto error;
98 ret = fimc_prepare_config(ctx, ctx->state);
99 if (ret)
100 goto error;
102 isp_info = &fimc->pdata->isp_info[fimc->vid_cap.input_index];
103 fimc_hw_set_camera_type(fimc, isp_info);
104 fimc_hw_set_camera_source(fimc, isp_info);
105 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
107 if (ctx->state & FIMC_PARAMS) {
108 ret = fimc_set_scaler_info(ctx);
109 if (ret) {
110 err("Scaler setup error");
111 goto error;
113 fimc_hw_set_input_path(ctx);
114 fimc_hw_set_prescaler(ctx);
115 fimc_hw_set_mainscaler(ctx);
116 fimc_hw_set_target_format(ctx);
117 fimc_hw_set_rotation(ctx);
118 fimc_hw_set_effect(ctx);
121 fimc_hw_set_output_path(ctx);
122 fimc_hw_set_out_dma(ctx);
124 INIT_LIST_HEAD(&fimc->vid_cap.pending_buf_q);
125 INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
126 fimc->vid_cap.frame_count = 0;
127 fimc->vid_cap.buf_index = 0;
129 set_bit(ST_CAPT_PEND, &fimc->state);
131 min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
133 if (fimc->vid_cap.active_buf_cnt >= min_bufs)
134 fimc_activate_capture(ctx);
136 return 0;
137 error:
138 fimc_capture_state_cleanup(fimc);
139 return ret;
142 static int stop_streaming(struct vb2_queue *q)
144 struct fimc_ctx *ctx = q->drv_priv;
145 struct fimc_dev *fimc = ctx->fimc_dev;
147 if (!fimc_capture_active(fimc))
148 return -EINVAL;
150 return fimc_stop_capture(fimc);
153 int fimc_capture_suspend(struct fimc_dev *fimc)
155 return -EBUSY;
158 int fimc_capture_resume(struct fimc_dev *fimc)
160 return 0;
163 static unsigned int get_plane_size(struct fimc_frame *fr, unsigned int plane)
165 if (!fr || plane >= fr->fmt->memplanes)
166 return 0;
167 return fr->f_width * fr->f_height * fr->fmt->depth[plane] / 8;
170 static int queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
171 unsigned int *num_planes, unsigned int sizes[],
172 void *allocators[])
174 struct fimc_ctx *ctx = vq->drv_priv;
175 struct fimc_fmt *fmt = ctx->d_frame.fmt;
176 int i;
178 if (!fmt)
179 return -EINVAL;
181 *num_planes = fmt->memplanes;
183 for (i = 0; i < fmt->memplanes; i++) {
184 sizes[i] = get_plane_size(&ctx->d_frame, i);
185 allocators[i] = ctx->fimc_dev->alloc_ctx;
188 return 0;
191 static int buffer_prepare(struct vb2_buffer *vb)
193 struct vb2_queue *vq = vb->vb2_queue;
194 struct fimc_ctx *ctx = vq->drv_priv;
195 int i;
197 if (!ctx->d_frame.fmt || vq->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
198 return -EINVAL;
200 for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
201 unsigned long size = get_plane_size(&ctx->d_frame, i);
203 if (vb2_plane_size(vb, i) < size) {
204 v4l2_err(ctx->fimc_dev->vid_cap.vfd,
205 "User buffer too small (%ld < %ld)\n",
206 vb2_plane_size(vb, i), size);
207 return -EINVAL;
210 vb2_set_plane_payload(vb, i, size);
213 return 0;
216 static void buffer_queue(struct vb2_buffer *vb)
218 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
219 struct fimc_dev *fimc = ctx->fimc_dev;
220 struct fimc_vid_buffer *buf
221 = container_of(vb, struct fimc_vid_buffer, vb);
222 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
223 unsigned long flags;
224 int min_bufs;
226 spin_lock_irqsave(&fimc->slock, flags);
227 fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
229 if (!test_bit(ST_CAPT_STREAM, &fimc->state)
230 && vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
231 /* Setup the buffer directly for processing. */
232 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
233 vid_cap->buf_index;
235 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
236 buf->index = vid_cap->buf_index;
237 active_queue_add(vid_cap, buf);
239 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
240 vid_cap->buf_index = 0;
241 } else {
242 fimc_pending_queue_add(vid_cap, buf);
245 min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
247 if (vb2_is_streaming(&vid_cap->vbq) &&
248 vid_cap->active_buf_cnt >= min_bufs &&
249 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state))
250 fimc_activate_capture(ctx);
252 spin_unlock_irqrestore(&fimc->slock, flags);
255 static void fimc_lock(struct vb2_queue *vq)
257 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
258 mutex_lock(&ctx->fimc_dev->lock);
261 static void fimc_unlock(struct vb2_queue *vq)
263 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
264 mutex_unlock(&ctx->fimc_dev->lock);
267 static struct vb2_ops fimc_capture_qops = {
268 .queue_setup = queue_setup,
269 .buf_prepare = buffer_prepare,
270 .buf_queue = buffer_queue,
271 .wait_prepare = fimc_unlock,
272 .wait_finish = fimc_lock,
273 .start_streaming = start_streaming,
274 .stop_streaming = stop_streaming,
278 * fimc_capture_ctrls_create - initialize the control handler
279 * Initialize the capture video node control handler and fill it
280 * with the FIMC controls. Inherit any sensor's controls if the
281 * 'user_subdev_api' flag is false (default behaviour).
282 * This function need to be called with the graph mutex held.
284 int fimc_capture_ctrls_create(struct fimc_dev *fimc)
286 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
287 int ret;
289 if (WARN_ON(vid_cap->ctx == NULL))
290 return -ENXIO;
291 if (vid_cap->ctx->ctrls_rdy)
292 return 0;
294 ret = fimc_ctrls_create(vid_cap->ctx);
295 if (ret || vid_cap->user_subdev_api)
296 return ret;
298 return v4l2_ctrl_add_handler(&vid_cap->ctx->ctrl_handler,
299 fimc->pipeline.sensor->ctrl_handler);
302 static int fimc_capture_open(struct file *file)
304 struct fimc_dev *fimc = video_drvdata(file);
305 int ret = v4l2_fh_open(file);
307 if (ret)
308 return ret;
310 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
312 /* Return if the corresponding video mem2mem node is already opened. */
313 if (fimc_m2m_active(fimc))
314 return -EBUSY;
316 ret = pm_runtime_get_sync(&fimc->pdev->dev);
317 if (ret < 0) {
318 v4l2_fh_release(file);
319 return ret;
322 if (++fimc->vid_cap.refcnt == 1)
323 ret = fimc_capture_ctrls_create(fimc);
325 return ret;
328 static int fimc_capture_close(struct file *file)
330 struct fimc_dev *fimc = video_drvdata(file);
332 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
334 if (--fimc->vid_cap.refcnt == 0) {
335 fimc_stop_capture(fimc);
336 fimc_ctrls_delete(fimc->vid_cap.ctx);
337 vb2_queue_release(&fimc->vid_cap.vbq);
340 pm_runtime_put(&fimc->pdev->dev);
342 return v4l2_fh_release(file);
345 static unsigned int fimc_capture_poll(struct file *file,
346 struct poll_table_struct *wait)
348 struct fimc_dev *fimc = video_drvdata(file);
350 return vb2_poll(&fimc->vid_cap.vbq, file, wait);
353 static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
355 struct fimc_dev *fimc = video_drvdata(file);
357 return vb2_mmap(&fimc->vid_cap.vbq, vma);
360 /* video device file operations */
361 static const struct v4l2_file_operations fimc_capture_fops = {
362 .owner = THIS_MODULE,
363 .open = fimc_capture_open,
364 .release = fimc_capture_close,
365 .poll = fimc_capture_poll,
366 .unlocked_ioctl = video_ioctl2,
367 .mmap = fimc_capture_mmap,
370 static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
371 struct v4l2_capability *cap)
373 struct fimc_dev *fimc = video_drvdata(file);
375 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
376 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
377 cap->bus_info[0] = 0;
378 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
379 V4L2_CAP_VIDEO_CAPTURE_MPLANE;
381 return 0;
384 /* Synchronize formats of the camera interface input and attached sensor. */
385 static int sync_capture_fmt(struct fimc_ctx *ctx)
387 struct fimc_frame *frame = &ctx->s_frame;
388 struct fimc_dev *fimc = ctx->fimc_dev;
389 struct v4l2_mbus_framefmt *fmt = &fimc->vid_cap.fmt;
390 int ret;
392 fmt->width = ctx->d_frame.o_width;
393 fmt->height = ctx->d_frame.o_height;
395 ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_mbus_fmt, fmt);
396 if (ret == -ENOIOCTLCMD) {
397 err("s_mbus_fmt failed");
398 return ret;
400 dbg("w: %d, h: %d, code= %d", fmt->width, fmt->height, fmt->code);
402 frame->fmt = find_mbus_format(fmt, FMT_FLAGS_CAM);
403 if (!frame->fmt) {
404 err("fimc source format not found\n");
405 return -EINVAL;
408 frame->f_width = fmt->width;
409 frame->f_height = fmt->height;
410 frame->width = fmt->width;
411 frame->height = fmt->height;
412 frame->o_width = fmt->width;
413 frame->o_height = fmt->height;
414 frame->offs_h = 0;
415 frame->offs_v = 0;
417 return 0;
420 static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
421 struct v4l2_format *f)
423 struct fimc_dev *fimc = video_drvdata(file);
424 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
426 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
427 return -EINVAL;
429 return fimc_fill_format(&ctx->d_frame, f);
432 static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
433 struct v4l2_format *f)
435 struct fimc_dev *fimc = video_drvdata(file);
436 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
438 return fimc_try_fmt_mplane(ctx, f);
441 static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
442 struct v4l2_format *f)
444 struct fimc_dev *fimc = video_drvdata(file);
445 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
446 struct v4l2_pix_format_mplane *pix;
447 struct fimc_frame *frame;
448 int ret;
449 int i;
451 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
452 return -EINVAL;
454 ret = fimc_try_fmt_mplane(ctx, f);
455 if (ret)
456 return ret;
458 if (vb2_is_busy(&fimc->vid_cap.vbq) || fimc_capture_active(fimc))
459 return -EBUSY;
461 frame = &ctx->d_frame;
463 pix = &f->fmt.pix_mp;
464 frame->fmt = find_format(f, FMT_FLAGS_M2M | FMT_FLAGS_CAM);
465 if (!frame->fmt) {
466 v4l2_err(fimc->vid_cap.vfd,
467 "Not supported capture (FIMC target) color format\n");
468 return -EINVAL;
471 for (i = 0; i < frame->fmt->colplanes; i++) {
472 frame->payload[i] =
473 (pix->width * pix->height * frame->fmt->depth[i]) >> 3;
476 /* Output DMA frame pixel size and offsets. */
477 frame->f_width = pix->plane_fmt[0].bytesperline * 8
478 / frame->fmt->depth[0];
479 frame->f_height = pix->height;
480 frame->width = pix->width;
481 frame->height = pix->height;
482 frame->o_width = pix->width;
483 frame->o_height = pix->height;
484 frame->offs_h = 0;
485 frame->offs_v = 0;
487 ctx->state |= (FIMC_PARAMS | FIMC_DST_FMT);
489 ret = sync_capture_fmt(ctx);
490 return ret;
493 static int fimc_cap_enum_input(struct file *file, void *priv,
494 struct v4l2_input *i)
496 struct fimc_dev *fimc = video_drvdata(file);
498 if (i->index != 0)
499 return -EINVAL;
502 i->type = V4L2_INPUT_TYPE_CAMERA;
503 return 0;
506 static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
508 return i == 0 ? i : -EINVAL;
511 static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
513 *i = 0;
514 return 0;
517 static int fimc_cap_streamon(struct file *file, void *priv,
518 enum v4l2_buf_type type)
520 struct fimc_dev *fimc = video_drvdata(file);
521 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
523 if (fimc_capture_active(fimc) || !fimc->vid_cap.sd)
524 return -EBUSY;
526 if (!(ctx->state & FIMC_DST_FMT)) {
527 v4l2_err(fimc->vid_cap.vfd, "Format is not set\n");
528 return -EINVAL;
531 return vb2_streamon(&fimc->vid_cap.vbq, type);
534 static int fimc_cap_streamoff(struct file *file, void *priv,
535 enum v4l2_buf_type type)
537 struct fimc_dev *fimc = video_drvdata(file);
539 return vb2_streamoff(&fimc->vid_cap.vbq, type);
542 static int fimc_cap_reqbufs(struct file *file, void *priv,
543 struct v4l2_requestbuffers *reqbufs)
545 struct fimc_dev *fimc = video_drvdata(file);
546 int ret = vb2_reqbufs(&fimc->vid_cap.vbq, reqbufs);
548 if (!ret)
549 fimc->vid_cap.reqbufs_count = reqbufs->count;
550 return ret;
553 static int fimc_cap_querybuf(struct file *file, void *priv,
554 struct v4l2_buffer *buf)
556 struct fimc_dev *fimc = video_drvdata(file);
558 return vb2_querybuf(&fimc->vid_cap.vbq, buf);
561 static int fimc_cap_qbuf(struct file *file, void *priv,
562 struct v4l2_buffer *buf)
564 struct fimc_dev *fimc = video_drvdata(file);
566 return vb2_qbuf(&fimc->vid_cap.vbq, buf);
569 static int fimc_cap_dqbuf(struct file *file, void *priv,
570 struct v4l2_buffer *buf)
572 struct fimc_dev *fimc = video_drvdata(file);
574 return vb2_dqbuf(&fimc->vid_cap.vbq, buf, file->f_flags & O_NONBLOCK);
577 static int fimc_cap_cropcap(struct file *file, void *fh,
578 struct v4l2_cropcap *cr)
580 struct fimc_dev *fimc = video_drvdata(file);
581 struct fimc_frame *f = &fimc->vid_cap.ctx->s_frame;
583 if (cr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
584 return -EINVAL;
586 cr->bounds.left = 0;
587 cr->bounds.top = 0;
588 cr->bounds.width = f->o_width;
589 cr->bounds.height = f->o_height;
590 cr->defrect = cr->bounds;
592 return 0;
595 static int fimc_cap_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
597 struct fimc_dev *fimc = video_drvdata(file);
598 struct fimc_frame *f = &fimc->vid_cap.ctx->s_frame;
600 cr->c.left = f->offs_h;
601 cr->c.top = f->offs_v;
602 cr->c.width = f->width;
603 cr->c.height = f->height;
605 return 0;
608 static int fimc_cap_s_crop(struct file *file, void *fh, struct v4l2_crop *cr)
610 struct fimc_dev *fimc = video_drvdata(file);
611 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
612 struct fimc_frame *f;
613 int ret = -EINVAL;
615 if (fimc_capture_active(fimc))
616 return -EBUSY;
618 ret = fimc_try_crop(ctx, cr);
619 if (ret)
620 return ret;
622 if (!(ctx->state & FIMC_DST_FMT)) {
623 v4l2_err(fimc->vid_cap.vfd, "Capture format is not set\n");
624 return -EINVAL;
627 f = &ctx->s_frame;
628 /* Check for the pixel scaling ratio when cropping input image. */
629 ret = fimc_check_scaler_ratio(cr->c.width, cr->c.height,
630 ctx->d_frame.width, ctx->d_frame.height,
631 ctx->rotation);
632 if (ret) {
633 v4l2_err(fimc->vid_cap.vfd, "Out of the scaler range\n");
634 return ret;
637 f->offs_h = cr->c.left;
638 f->offs_v = cr->c.top;
639 f->width = cr->c.width;
640 f->height = cr->c.height;
642 return 0;
646 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
647 .vidioc_querycap = fimc_vidioc_querycap_capture,
649 .vidioc_enum_fmt_vid_cap_mplane = fimc_vidioc_enum_fmt_mplane,
650 .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
651 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
652 .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
654 .vidioc_reqbufs = fimc_cap_reqbufs,
655 .vidioc_querybuf = fimc_cap_querybuf,
657 .vidioc_qbuf = fimc_cap_qbuf,
658 .vidioc_dqbuf = fimc_cap_dqbuf,
660 .vidioc_streamon = fimc_cap_streamon,
661 .vidioc_streamoff = fimc_cap_streamoff,
663 .vidioc_g_crop = fimc_cap_g_crop,
664 .vidioc_s_crop = fimc_cap_s_crop,
665 .vidioc_cropcap = fimc_cap_cropcap,
667 .vidioc_enum_input = fimc_cap_enum_input,
668 .vidioc_s_input = fimc_cap_s_input,
669 .vidioc_g_input = fimc_cap_g_input,
672 /* fimc->lock must be already initialized */
673 int fimc_register_capture_device(struct fimc_dev *fimc,
674 struct v4l2_device *v4l2_dev)
676 struct video_device *vfd;
677 struct fimc_vid_cap *vid_cap;
678 struct fimc_ctx *ctx;
679 struct v4l2_format f;
680 struct fimc_frame *fr;
681 struct vb2_queue *q;
682 int ret = -ENOMEM;
684 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
685 if (!ctx)
686 return -ENOMEM;
688 ctx->fimc_dev = fimc;
689 ctx->in_path = FIMC_CAMERA;
690 ctx->out_path = FIMC_DMA;
691 ctx->state = FIMC_CTX_CAP;
693 /* Default format of the output frames */
694 f.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB32;
695 fr = &ctx->d_frame;
696 fr->fmt = find_format(&f, FMT_FLAGS_M2M);
697 fr->width = fr->f_width = fr->o_width = 640;
698 fr->height = fr->f_height = fr->o_height = 480;
700 vfd = video_device_alloc();
701 if (!vfd) {
702 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
703 goto err_vd_alloc;
706 snprintf(vfd->name, sizeof(vfd->name), "%s.capture",
707 dev_name(&fimc->pdev->dev));
709 vfd->fops = &fimc_capture_fops;
710 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
711 vfd->v4l2_dev = v4l2_dev;
712 vfd->minor = -1;
713 vfd->release = video_device_release;
714 vfd->lock = &fimc->lock;
715 video_set_drvdata(vfd, fimc);
717 vid_cap = &fimc->vid_cap;
718 vid_cap->vfd = vfd;
719 vid_cap->active_buf_cnt = 0;
720 vid_cap->reqbufs_count = 0;
721 vid_cap->refcnt = 0;
722 /* Default color format for image sensor */
723 vid_cap->fmt.code = V4L2_MBUS_FMT_YUYV8_2X8;
725 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
726 INIT_LIST_HEAD(&vid_cap->active_buf_q);
727 spin_lock_init(&ctx->slock);
728 vid_cap->ctx = ctx;
730 q = &fimc->vid_cap.vbq;
731 memset(q, 0, sizeof(*q));
732 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
733 q->io_modes = VB2_MMAP | VB2_USERPTR;
734 q->drv_priv = fimc->vid_cap.ctx;
735 q->ops = &fimc_capture_qops;
736 q->mem_ops = &vb2_dma_contig_memops;
737 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
739 vb2_queue_init(q);
741 fimc->vid_cap.vd_pad.flags = MEDIA_PAD_FL_SINK;
742 ret = media_entity_init(&vfd->entity, 1, &fimc->vid_cap.vd_pad, 0);
743 if (ret)
744 goto err_ent;
746 vfd->ctrl_handler = &ctx->ctrl_handler;
747 return 0;
749 err_ent:
750 video_device_release(vfd);
751 err_vd_alloc:
752 kfree(ctx);
753 return ret;
756 void fimc_unregister_capture_device(struct fimc_dev *fimc)
758 struct video_device *vfd = fimc->vid_cap.vfd;
760 if (vfd) {
761 media_entity_cleanup(&vfd->entity);
762 /* Can also be called if video device was
763 not registered */
764 video_unregister_device(vfd);
766 kfree(fimc->vid_cap.ctx);
767 fimc->vid_cap.ctx = NULL;