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 int fimc_init_capture(struct fimc_dev *fimc)
35 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
36 struct fimc_sensor_info *sensor;
40 if (fimc->pipeline.sensor == NULL || ctx == NULL)
42 if (ctx->s_frame.fmt == NULL)
45 sensor = v4l2_get_subdev_hostdata(fimc->pipeline.sensor);
47 spin_lock_irqsave(&fimc->slock, flags);
48 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
49 fimc_set_yuv_order(ctx);
51 fimc_hw_set_camera_polarity(fimc, sensor->pdata);
52 fimc_hw_set_camera_type(fimc, sensor->pdata);
53 fimc_hw_set_camera_source(fimc, sensor->pdata);
54 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
56 ret = fimc_set_scaler_info(ctx);
58 fimc_hw_set_input_path(ctx);
59 fimc_hw_set_prescaler(ctx);
60 fimc_hw_set_mainscaler(ctx);
61 fimc_hw_set_target_format(ctx);
62 fimc_hw_set_rotation(ctx);
63 fimc_hw_set_effect(ctx, false);
64 fimc_hw_set_output_path(ctx);
65 fimc_hw_set_out_dma(ctx);
66 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
68 spin_unlock_irqrestore(&fimc->slock, flags);
72 static int fimc_capture_state_cleanup(struct fimc_dev *fimc)
74 struct fimc_vid_cap *cap = &fimc->vid_cap;
75 struct fimc_vid_buffer *buf;
78 spin_lock_irqsave(&fimc->slock, flags);
79 fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_PEND |
80 1 << ST_CAPT_SHUT | 1 << ST_CAPT_STREAM |
81 1 << ST_CAPT_ISP_STREAM);
83 fimc->vid_cap.active_buf_cnt = 0;
85 /* Release buffers that were enqueued in the driver by videobuf2. */
86 while (!list_empty(&cap->pending_buf_q)) {
87 buf = fimc_pending_queue_pop(cap);
88 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
91 while (!list_empty(&cap->active_buf_q)) {
92 buf = fimc_active_queue_pop(cap);
93 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
96 spin_unlock_irqrestore(&fimc->slock, flags);
98 if (test_bit(ST_CAPT_ISP_STREAM, &fimc->state))
99 return fimc_pipeline_s_stream(fimc, 0);
104 static int fimc_stop_capture(struct fimc_dev *fimc)
106 struct fimc_vid_cap *cap = &fimc->vid_cap;
109 if (!fimc_capture_active(fimc))
112 spin_lock_irqsave(&fimc->slock, flags);
113 set_bit(ST_CAPT_SHUT, &fimc->state);
114 fimc_deactivate_capture(fimc);
115 spin_unlock_irqrestore(&fimc->slock, flags);
117 wait_event_timeout(fimc->irq_queue,
118 !test_bit(ST_CAPT_SHUT, &fimc->state),
119 FIMC_SHUTDOWN_TIMEOUT);
121 return fimc_capture_state_cleanup(fimc);
125 * fimc_capture_config_update - apply the camera interface configuration
127 * To be called from within the interrupt handler with fimc.slock
128 * spinlock held. It updates the camera pixel crop, rotation and
131 int fimc_capture_config_update(struct fimc_ctx *ctx)
133 struct fimc_dev *fimc = ctx->fimc_dev;
136 if (test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
139 spin_lock(&ctx->slock);
140 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
141 ret = fimc_set_scaler_info(ctx);
143 fimc_hw_set_prescaler(ctx);
144 fimc_hw_set_mainscaler(ctx);
145 fimc_hw_set_target_format(ctx);
146 fimc_hw_set_rotation(ctx);
147 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
148 fimc_hw_set_out_dma(ctx);
149 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
151 spin_unlock(&ctx->slock);
155 static int start_streaming(struct vb2_queue *q, unsigned int count)
157 struct fimc_ctx *ctx = q->drv_priv;
158 struct fimc_dev *fimc = ctx->fimc_dev;
159 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
164 vid_cap->frame_count = 0;
166 ret = fimc_init_capture(fimc);
170 set_bit(ST_CAPT_PEND, &fimc->state);
172 min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
174 if (vid_cap->active_buf_cnt >= min_bufs &&
175 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
176 fimc_activate_capture(ctx);
178 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
179 fimc_pipeline_s_stream(fimc, 1);
184 fimc_capture_state_cleanup(fimc);
188 static int stop_streaming(struct vb2_queue *q)
190 struct fimc_ctx *ctx = q->drv_priv;
191 struct fimc_dev *fimc = ctx->fimc_dev;
193 if (!fimc_capture_active(fimc))
196 return fimc_stop_capture(fimc);
199 int fimc_capture_suspend(struct fimc_dev *fimc)
204 int fimc_capture_resume(struct fimc_dev *fimc)
209 static unsigned int get_plane_size(struct fimc_frame *fr, unsigned int plane)
211 if (!fr || plane >= fr->fmt->memplanes)
213 return fr->f_width * fr->f_height * fr->fmt->depth[plane] / 8;
216 static int queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
217 unsigned int *num_planes, unsigned int sizes[],
220 struct fimc_ctx *ctx = vq->drv_priv;
221 struct fimc_fmt *fmt = ctx->d_frame.fmt;
227 *num_planes = fmt->memplanes;
229 for (i = 0; i < fmt->memplanes; i++) {
230 sizes[i] = get_plane_size(&ctx->d_frame, i);
231 allocators[i] = ctx->fimc_dev->alloc_ctx;
237 static int buffer_prepare(struct vb2_buffer *vb)
239 struct vb2_queue *vq = vb->vb2_queue;
240 struct fimc_ctx *ctx = vq->drv_priv;
243 if (ctx->d_frame.fmt == NULL)
246 for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
247 unsigned long size = ctx->d_frame.payload[i];
249 if (vb2_plane_size(vb, i) < size) {
250 v4l2_err(ctx->fimc_dev->vid_cap.vfd,
251 "User buffer too small (%ld < %ld)\n",
252 vb2_plane_size(vb, i), size);
255 vb2_set_plane_payload(vb, i, size);
261 static void buffer_queue(struct vb2_buffer *vb)
263 struct fimc_vid_buffer *buf
264 = container_of(vb, struct fimc_vid_buffer, vb);
265 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
266 struct fimc_dev *fimc = ctx->fimc_dev;
267 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
271 spin_lock_irqsave(&fimc->slock, flags);
272 fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
274 if (!test_bit(ST_CAPT_STREAM, &fimc->state)
275 && vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
276 /* Setup the buffer directly for processing. */
277 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
280 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
281 buf->index = vid_cap->buf_index;
282 fimc_active_queue_add(vid_cap, buf);
284 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
285 vid_cap->buf_index = 0;
287 fimc_pending_queue_add(vid_cap, buf);
290 min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
293 if (vb2_is_streaming(&vid_cap->vbq) &&
294 vid_cap->active_buf_cnt >= min_bufs &&
295 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
296 fimc_activate_capture(ctx);
297 spin_unlock_irqrestore(&fimc->slock, flags);
299 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
300 fimc_pipeline_s_stream(fimc, 1);
303 spin_unlock_irqrestore(&fimc->slock, flags);
306 static void fimc_lock(struct vb2_queue *vq)
308 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
309 mutex_lock(&ctx->fimc_dev->lock);
312 static void fimc_unlock(struct vb2_queue *vq)
314 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
315 mutex_unlock(&ctx->fimc_dev->lock);
318 static struct vb2_ops fimc_capture_qops = {
319 .queue_setup = queue_setup,
320 .buf_prepare = buffer_prepare,
321 .buf_queue = buffer_queue,
322 .wait_prepare = fimc_unlock,
323 .wait_finish = fimc_lock,
324 .start_streaming = start_streaming,
325 .stop_streaming = stop_streaming,
329 * fimc_capture_ctrls_create - initialize the control handler
330 * Initialize the capture video node control handler and fill it
331 * with the FIMC controls. Inherit any sensor's controls if the
332 * 'user_subdev_api' flag is false (default behaviour).
333 * This function need to be called with the graph mutex held.
335 int fimc_capture_ctrls_create(struct fimc_dev *fimc)
337 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
340 if (WARN_ON(vid_cap->ctx == NULL))
342 if (vid_cap->ctx->ctrls_rdy)
345 ret = fimc_ctrls_create(vid_cap->ctx);
346 if (ret || vid_cap->user_subdev_api)
349 return v4l2_ctrl_add_handler(&vid_cap->ctx->ctrl_handler,
350 fimc->pipeline.sensor->ctrl_handler);
353 static int fimc_capture_set_default_format(struct fimc_dev *fimc);
355 static int fimc_capture_open(struct file *file)
357 struct fimc_dev *fimc = video_drvdata(file);
358 int ret = v4l2_fh_open(file);
363 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
365 /* Return if the corresponding video mem2mem node is already opened. */
366 if (fimc_m2m_active(fimc))
369 pm_runtime_get_sync(&fimc->pdev->dev);
371 if (++fimc->vid_cap.refcnt == 1) {
372 ret = fimc_pipeline_initialize(fimc,
373 &fimc->vid_cap.vfd->entity, true);
375 dev_err(&fimc->pdev->dev,
376 "Video pipeline initialization failed\n");
377 pm_runtime_put_sync(&fimc->pdev->dev);
378 fimc->vid_cap.refcnt--;
379 v4l2_fh_release(file);
382 ret = fimc_capture_ctrls_create(fimc);
384 if (!ret && !fimc->vid_cap.user_subdev_api)
385 ret = fimc_capture_set_default_format(fimc);
390 static int fimc_capture_close(struct file *file)
392 struct fimc_dev *fimc = video_drvdata(file);
394 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
396 if (--fimc->vid_cap.refcnt == 0) {
397 fimc_stop_capture(fimc);
398 fimc_pipeline_shutdown(fimc);
399 fimc_ctrls_delete(fimc->vid_cap.ctx);
400 vb2_queue_release(&fimc->vid_cap.vbq);
403 pm_runtime_put(&fimc->pdev->dev);
405 return v4l2_fh_release(file);
408 static unsigned int fimc_capture_poll(struct file *file,
409 struct poll_table_struct *wait)
411 struct fimc_dev *fimc = video_drvdata(file);
413 return vb2_poll(&fimc->vid_cap.vbq, file, wait);
416 static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
418 struct fimc_dev *fimc = video_drvdata(file);
420 return vb2_mmap(&fimc->vid_cap.vbq, vma);
423 static const struct v4l2_file_operations fimc_capture_fops = {
424 .owner = THIS_MODULE,
425 .open = fimc_capture_open,
426 .release = fimc_capture_close,
427 .poll = fimc_capture_poll,
428 .unlocked_ioctl = video_ioctl2,
429 .mmap = fimc_capture_mmap,
433 * Format and crop negotiation helpers
436 static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
437 u32 *width, u32 *height,
438 u32 *code, u32 *fourcc, int pad)
440 bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
441 struct fimc_dev *fimc = ctx->fimc_dev;
442 struct samsung_fimc_variant *var = fimc->variant;
443 struct fimc_pix_limit *pl = var->pix_limit;
444 struct fimc_frame *dst = &ctx->d_frame;
445 u32 depth, min_w, max_w, min_h, align_h = 3;
446 u32 mask = FMT_FLAGS_CAM;
447 struct fimc_fmt *ffmt;
449 /* Color conversion from/to JPEG is not supported */
450 if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
451 fimc_fmt_is_jpeg(ctx->s_frame.fmt->color))
452 *code = V4L2_MBUS_FMT_JPEG_1X8;
454 if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad != FIMC_SD_PAD_SINK)
455 mask |= FMT_FLAGS_M2M;
457 ffmt = fimc_find_format(fourcc, code, mask, 0);
461 *code = ffmt->mbus_code;
463 *fourcc = ffmt->fourcc;
465 if (pad == FIMC_SD_PAD_SINK) {
466 max_w = fimc_fmt_is_jpeg(ffmt->color) ?
467 pl->scaler_dis_w : pl->scaler_en_w;
468 /* Apply the camera input interface pixel constraints */
469 v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
470 height, max_t(u32, *height, 32),
471 FIMC_CAMIF_MAX_HEIGHT,
472 fimc_fmt_is_jpeg(ffmt->color) ? 3 : 1,
476 /* Can't scale or crop in transparent (JPEG) transfer mode */
477 if (fimc_fmt_is_jpeg(ffmt->color)) {
478 *width = ctx->s_frame.f_width;
479 *height = ctx->s_frame.f_height;
482 /* Apply the scaler and the output DMA constraints */
483 max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
484 min_w = ctx->state & FIMC_DST_CROP ? dst->width : var->min_out_pixsize;
485 min_h = ctx->state & FIMC_DST_CROP ? dst->height : var->min_out_pixsize;
486 if (fimc->id == 1 && var->pix_hoff)
487 align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
489 depth = fimc_get_format_depth(ffmt);
490 v4l_bound_align_image(width, min_w, max_w,
491 ffs(var->min_out_pixsize) - 1,
492 height, min_h, FIMC_CAMIF_MAX_HEIGHT,
494 64/(ALIGN(depth, 8)));
496 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
497 pad, code ? *code : 0, *width, *height,
498 dst->f_width, dst->f_height);
503 static void fimc_capture_try_crop(struct fimc_ctx *ctx, struct v4l2_rect *r,
506 bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
507 struct fimc_dev *fimc = ctx->fimc_dev;
508 struct samsung_fimc_variant *var = fimc->variant;
509 struct fimc_pix_limit *pl = var->pix_limit;
510 struct fimc_frame *sink = &ctx->s_frame;
511 u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
512 u32 align_sz = 0, align_h = 4;
513 u32 max_sc_h, max_sc_v;
515 /* In JPEG transparent transfer mode cropping is not supported */
516 if (fimc_fmt_is_jpeg(ctx->d_frame.fmt->color)) {
517 r->width = sink->f_width;
518 r->height = sink->f_height;
519 r->left = r->top = 0;
522 if (pad == FIMC_SD_PAD_SOURCE) {
523 if (ctx->rotation != 90 && ctx->rotation != 270)
525 max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3));
526 max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1));
527 min_sz = var->min_out_pixsize;
529 u32 depth = fimc_get_format_depth(sink->fmt);
530 align_sz = 64/ALIGN(depth, 8);
531 min_sz = var->min_inp_pixsize;
532 min_w = min_h = min_sz;
533 max_sc_h = max_sc_v = 1;
536 * For the crop rectangle at source pad the following constraints
538 * - it must fit in the sink pad format rectangle (f_width/f_height);
539 * - maximum downscaling ratio is 64;
540 * - maximum crop size depends if the rotator is used or not;
541 * - the sink pad format width/height must be 4 multiple of the
542 * prescaler ratios determined by sink pad size and source pad crop,
543 * the prescaler ratio is returned by fimc_get_scaler_factor().
546 rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
547 rotate ? sink->f_height : sink->f_width);
548 max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
549 if (pad == FIMC_SD_PAD_SOURCE) {
550 min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
551 min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
553 swap(max_sc_h, max_sc_v);
557 v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
558 &r->height, min_h, max_h, align_h,
560 /* Adjust left/top if cropping rectangle is out of bounds */
561 r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
562 r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height);
563 r->left = round_down(r->left, var->hor_offs_align);
565 dbg("pad%d: (%d,%d)/%dx%d, sink fmt: %dx%d",
566 pad, r->left, r->top, r->width, r->height,
567 sink->f_width, sink->f_height);
571 * The video node ioctl operations
573 static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
574 struct v4l2_capability *cap)
576 struct fimc_dev *fimc = video_drvdata(file);
578 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
579 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
580 cap->bus_info[0] = 0;
581 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
582 V4L2_CAP_VIDEO_CAPTURE_MPLANE;
587 static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
588 struct v4l2_fmtdesc *f)
590 struct fimc_fmt *fmt;
592 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
596 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
597 f->pixelformat = fmt->fourcc;
598 if (fmt->fourcc == V4L2_MBUS_FMT_JPEG_1X8)
599 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
604 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
606 * @ctx: FIMC capture context
607 * @tfmt: media bus format to try/set on subdevs
608 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
609 * @set: true to set format on subdevs, false to try only
611 static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
612 struct v4l2_mbus_framefmt *tfmt,
613 struct fimc_fmt **fmt_id,
616 struct fimc_dev *fimc = ctx->fimc_dev;
617 struct v4l2_subdev *sd = fimc->pipeline.sensor;
618 struct v4l2_subdev *csis = fimc->pipeline.csis;
619 struct v4l2_subdev_format sfmt;
620 struct v4l2_mbus_framefmt *mf = &sfmt.format;
621 struct fimc_fmt *ffmt = NULL;
624 if (WARN_ON(!sd || !tfmt))
627 memset(&sfmt, 0, sizeof(sfmt));
630 sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
632 ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
636 * Notify user-space if common pixel code for
637 * host and sensor does not exist.
641 mf->code = tfmt->code = ffmt->mbus_code;
643 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
646 if (mf->code != tfmt->code) {
650 if (mf->width != tfmt->width || mf->width != tfmt->width) {
651 u32 fcc = ffmt->fourcc;
652 tfmt->width = mf->width;
653 tfmt->height = mf->height;
654 ffmt = fimc_capture_try_format(ctx,
655 &tfmt->width, &tfmt->height,
656 NULL, &fcc, FIMC_SD_PAD_SOURCE);
657 if (ffmt && ffmt->mbus_code)
658 mf->code = ffmt->mbus_code;
659 if (mf->width != tfmt->width || mf->width != tfmt->width)
661 tfmt->code = mf->code;
664 ret = v4l2_subdev_call(csis, pad, set_fmt, NULL, &sfmt);
666 if (mf->code == tfmt->code &&
667 mf->width == tfmt->width && mf->width == tfmt->width)
675 dbg("code: 0x%x, %dx%d, %p", mf->code, mf->width, mf->height, ffmt);
679 static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
680 struct v4l2_format *f)
682 struct fimc_dev *fimc = video_drvdata(file);
683 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
685 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
688 return fimc_fill_format(&ctx->d_frame, f);
691 static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
692 struct v4l2_format *f)
694 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
695 struct fimc_dev *fimc = video_drvdata(file);
696 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
697 struct v4l2_mbus_framefmt mf;
698 struct fimc_fmt *ffmt = NULL;
700 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
703 if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
704 fimc_capture_try_format(ctx, &pix->width, &pix->height,
705 NULL, &pix->pixelformat,
707 ctx->s_frame.f_width = pix->width;
708 ctx->s_frame.f_height = pix->height;
710 ffmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
711 NULL, &pix->pixelformat,
716 if (!fimc->vid_cap.user_subdev_api) {
717 mf.width = pix->width;
718 mf.height = pix->height;
719 mf.code = ffmt->mbus_code;
720 fimc_md_graph_lock(fimc);
721 fimc_pipeline_try_format(ctx, &mf, &ffmt, false);
722 fimc_md_graph_unlock(fimc);
724 pix->width = mf.width;
725 pix->height = mf.height;
727 pix->pixelformat = ffmt->fourcc;
730 fimc_adjust_mplane_format(ffmt, pix->width, pix->height, pix);
734 static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx, bool jpeg)
736 ctx->scaler.enabled = !jpeg;
737 fimc_ctrls_activate(ctx, !jpeg);
740 set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
742 clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
745 static int fimc_capture_set_format(struct fimc_dev *fimc, struct v4l2_format *f)
747 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
748 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
749 struct v4l2_mbus_framefmt *mf = &fimc->vid_cap.mf;
750 struct fimc_frame *ff = &ctx->d_frame;
751 struct fimc_fmt *s_fmt = NULL;
754 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
756 if (vb2_is_busy(&fimc->vid_cap.vbq))
759 /* Pre-configure format at camera interface input, for JPEG only */
760 if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
761 fimc_capture_try_format(ctx, &pix->width, &pix->height,
762 NULL, &pix->pixelformat,
764 ctx->s_frame.f_width = pix->width;
765 ctx->s_frame.f_height = pix->height;
767 /* Try the format at the scaler and the DMA output */
768 ff->fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
769 NULL, &pix->pixelformat,
773 /* Try to match format at the host and the sensor */
774 if (!fimc->vid_cap.user_subdev_api) {
775 mf->code = ff->fmt->mbus_code;
776 mf->width = pix->width;
777 mf->height = pix->height;
779 fimc_md_graph_lock(fimc);
780 ret = fimc_pipeline_try_format(ctx, mf, &s_fmt, true);
781 fimc_md_graph_unlock(fimc);
784 pix->width = mf->width;
785 pix->height = mf->height;
787 fimc_adjust_mplane_format(ff->fmt, pix->width, pix->height, pix);
788 for (i = 0; i < ff->fmt->colplanes; i++)
790 (pix->width * pix->height * ff->fmt->depth[i]) / 8;
792 set_frame_bounds(ff, pix->width, pix->height);
793 /* Reset the composition rectangle if not yet configured */
794 if (!(ctx->state & FIMC_DST_CROP))
795 set_frame_crop(ff, 0, 0, pix->width, pix->height);
797 fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ff->fmt->color));
799 /* Reset cropping and set format at the camera interface input */
800 if (!fimc->vid_cap.user_subdev_api) {
801 ctx->s_frame.fmt = s_fmt;
802 set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
803 set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
809 static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
810 struct v4l2_format *f)
812 struct fimc_dev *fimc = video_drvdata(file);
814 return fimc_capture_set_format(fimc, f);
817 static int fimc_cap_enum_input(struct file *file, void *priv,
818 struct v4l2_input *i)
820 struct fimc_dev *fimc = video_drvdata(file);
821 struct v4l2_subdev *sd = fimc->pipeline.sensor;
826 i->type = V4L2_INPUT_TYPE_CAMERA;
828 strlcpy(i->name, sd->name, sizeof(i->name));
832 static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
834 return i == 0 ? i : -EINVAL;
837 static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
844 * fimc_pipeline_validate - check for formats inconsistencies
845 * between source and sink pad of each link
847 * Return 0 if all formats match or -EPIPE otherwise.
849 static int fimc_pipeline_validate(struct fimc_dev *fimc)
851 struct v4l2_subdev_format sink_fmt, src_fmt;
852 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
853 struct v4l2_subdev *sd;
854 struct media_pad *pad;
857 /* Start with the video capture node pad */
858 pad = media_entity_remote_source(&vid_cap->vd_pad);
861 /* FIMC.{N} subdevice */
862 sd = media_entity_to_v4l2_subdev(pad->entity);
865 /* Retrieve format at the sink pad */
866 pad = &sd->entity.pads[0];
867 if (!(pad->flags & MEDIA_PAD_FL_SINK))
869 /* Don't call FIMC subdev operation to avoid nested locking */
870 if (sd == fimc->vid_cap.subdev) {
871 struct fimc_frame *ff = &vid_cap->ctx->s_frame;
872 sink_fmt.format.width = ff->f_width;
873 sink_fmt.format.height = ff->f_height;
874 sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
876 sink_fmt.pad = pad->index;
877 sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
878 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
879 if (ret < 0 && ret != -ENOIOCTLCMD)
882 /* Retrieve format at the source pad */
883 pad = media_entity_remote_source(pad);
885 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
888 sd = media_entity_to_v4l2_subdev(pad->entity);
889 src_fmt.pad = pad->index;
890 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
891 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
892 if (ret < 0 && ret != -ENOIOCTLCMD)
895 if (src_fmt.format.width != sink_fmt.format.width ||
896 src_fmt.format.height != sink_fmt.format.height ||
897 src_fmt.format.code != sink_fmt.format.code)
903 static int fimc_cap_streamon(struct file *file, void *priv,
904 enum v4l2_buf_type type)
906 struct fimc_dev *fimc = video_drvdata(file);
907 struct fimc_pipeline *p = &fimc->pipeline;
910 if (fimc_capture_active(fimc))
913 media_entity_pipeline_start(&p->sensor->entity, p->pipe);
915 if (fimc->vid_cap.user_subdev_api) {
916 ret = fimc_pipeline_validate(fimc);
920 return vb2_streamon(&fimc->vid_cap.vbq, type);
923 static int fimc_cap_streamoff(struct file *file, void *priv,
924 enum v4l2_buf_type type)
926 struct fimc_dev *fimc = video_drvdata(file);
927 struct v4l2_subdev *sd = fimc->pipeline.sensor;
930 ret = vb2_streamoff(&fimc->vid_cap.vbq, type);
932 media_entity_pipeline_stop(&sd->entity);
936 static int fimc_cap_reqbufs(struct file *file, void *priv,
937 struct v4l2_requestbuffers *reqbufs)
939 struct fimc_dev *fimc = video_drvdata(file);
940 int ret = vb2_reqbufs(&fimc->vid_cap.vbq, reqbufs);
943 fimc->vid_cap.reqbufs_count = reqbufs->count;
947 static int fimc_cap_querybuf(struct file *file, void *priv,
948 struct v4l2_buffer *buf)
950 struct fimc_dev *fimc = video_drvdata(file);
952 return vb2_querybuf(&fimc->vid_cap.vbq, buf);
955 static int fimc_cap_qbuf(struct file *file, void *priv,
956 struct v4l2_buffer *buf)
958 struct fimc_dev *fimc = video_drvdata(file);
960 return vb2_qbuf(&fimc->vid_cap.vbq, buf);
963 static int fimc_cap_dqbuf(struct file *file, void *priv,
964 struct v4l2_buffer *buf)
966 struct fimc_dev *fimc = video_drvdata(file);
968 return vb2_dqbuf(&fimc->vid_cap.vbq, buf, file->f_flags & O_NONBLOCK);
971 static int fimc_cap_cropcap(struct file *file, void *fh,
972 struct v4l2_cropcap *cr)
974 struct fimc_dev *fimc = video_drvdata(file);
975 struct fimc_frame *f = &fimc->vid_cap.ctx->s_frame;
977 if (cr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
982 cr->bounds.width = f->o_width;
983 cr->bounds.height = f->o_height;
984 cr->defrect = cr->bounds;
989 static int fimc_cap_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
991 struct fimc_dev *fimc = video_drvdata(file);
992 struct fimc_frame *f = &fimc->vid_cap.ctx->s_frame;
994 cr->c.left = f->offs_h;
995 cr->c.top = f->offs_v;
996 cr->c.width = f->width;
997 cr->c.height = f->height;
1002 static int fimc_cap_s_crop(struct file *file, void *fh, struct v4l2_crop *cr)
1004 struct fimc_dev *fimc = video_drvdata(file);
1005 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1006 struct fimc_frame *ff;
1007 unsigned long flags;
1009 fimc_capture_try_crop(ctx, &cr->c, FIMC_SD_PAD_SINK);
1012 spin_lock_irqsave(&fimc->slock, flags);
1013 set_frame_crop(ff, cr->c.left, cr->c.top, cr->c.width, cr->c.height);
1014 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1015 spin_unlock_irqrestore(&fimc->slock, flags);
1020 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1021 .vidioc_querycap = fimc_vidioc_querycap_capture,
1023 .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
1024 .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
1025 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
1026 .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
1028 .vidioc_reqbufs = fimc_cap_reqbufs,
1029 .vidioc_querybuf = fimc_cap_querybuf,
1031 .vidioc_qbuf = fimc_cap_qbuf,
1032 .vidioc_dqbuf = fimc_cap_dqbuf,
1034 .vidioc_streamon = fimc_cap_streamon,
1035 .vidioc_streamoff = fimc_cap_streamoff,
1037 .vidioc_g_crop = fimc_cap_g_crop,
1038 .vidioc_s_crop = fimc_cap_s_crop,
1039 .vidioc_cropcap = fimc_cap_cropcap,
1041 .vidioc_enum_input = fimc_cap_enum_input,
1042 .vidioc_s_input = fimc_cap_s_input,
1043 .vidioc_g_input = fimc_cap_g_input,
1046 /* Capture subdev media entity operations */
1047 static int fimc_link_setup(struct media_entity *entity,
1048 const struct media_pad *local,
1049 const struct media_pad *remote, u32 flags)
1051 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1052 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1054 if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1057 if (WARN_ON(fimc == NULL))
1060 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1061 local->entity->name, remote->entity->name, flags,
1062 fimc->vid_cap.input);
1064 if (flags & MEDIA_LNK_FL_ENABLED) {
1065 if (fimc->vid_cap.input != 0)
1067 fimc->vid_cap.input = sd->grp_id;
1071 fimc->vid_cap.input = 0;
1075 static const struct media_entity_operations fimc_sd_media_ops = {
1076 .link_setup = fimc_link_setup,
1080 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1081 * @sd: pointer to a subdev generating the notification
1082 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1083 * @arg: pointer to an u32 type integer that stores the frame payload value
1085 * The End Of Frame notification sent by sensor subdev in its still capture
1086 * mode. If there is only a single VSYNC generated by the sensor at the
1087 * beginning of a frame transmission, FIMC does not issue the LastIrq
1088 * (end of frame) interrupt. And this notification is used to complete the
1089 * frame capture and returning a buffer to user-space. Subdev drivers should
1090 * call this notification from their last 'End of frame capture' interrupt.
1092 void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1095 struct fimc_sensor_info *sensor;
1096 struct fimc_vid_buffer *buf;
1097 struct fimc_md *fmd;
1098 struct fimc_dev *fimc;
1099 unsigned long flags;
1104 sensor = v4l2_get_subdev_hostdata(sd);
1105 fmd = entity_to_fimc_mdev(&sd->entity);
1107 spin_lock_irqsave(&fmd->slock, flags);
1108 fimc = sensor ? sensor->host : NULL;
1110 if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1111 test_bit(ST_CAPT_PEND, &fimc->state)) {
1112 unsigned long irq_flags;
1113 spin_lock_irqsave(&fimc->slock, irq_flags);
1114 if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1115 buf = list_entry(fimc->vid_cap.active_buf_q.next,
1116 struct fimc_vid_buffer, list);
1117 vb2_set_plane_payload(&buf->vb, 0, *((u32 *)arg));
1119 fimc_capture_irq_handler(fimc, true);
1120 fimc_deactivate_capture(fimc);
1121 spin_unlock_irqrestore(&fimc->slock, irq_flags);
1123 spin_unlock_irqrestore(&fmd->slock, flags);
1126 static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1127 struct v4l2_subdev_fh *fh,
1128 struct v4l2_subdev_mbus_code_enum *code)
1130 struct fimc_fmt *fmt;
1132 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1135 code->code = fmt->mbus_code;
1139 static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
1140 struct v4l2_subdev_fh *fh,
1141 struct v4l2_subdev_format *fmt)
1143 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1144 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1145 struct v4l2_mbus_framefmt *mf;
1146 struct fimc_frame *ff;
1148 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1149 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1154 mf->colorspace = V4L2_COLORSPACE_JPEG;
1155 ff = fmt->pad == FIMC_SD_PAD_SINK ? &ctx->s_frame : &ctx->d_frame;
1157 mutex_lock(&fimc->lock);
1158 /* The pixel code is same on both input and output pad */
1159 if (!WARN_ON(ctx->s_frame.fmt == NULL))
1160 mf->code = ctx->s_frame.fmt->mbus_code;
1161 mf->width = ff->f_width;
1162 mf->height = ff->f_height;
1163 mutex_unlock(&fimc->lock);
1168 static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
1169 struct v4l2_subdev_fh *fh,
1170 struct v4l2_subdev_format *fmt)
1172 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1173 struct v4l2_mbus_framefmt *mf = &fmt->format;
1174 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1175 struct fimc_frame *ff;
1176 struct fimc_fmt *ffmt;
1178 dbg("pad%d: code: 0x%x, %dx%d",
1179 fmt->pad, mf->code, mf->width, mf->height);
1181 if (fmt->pad == FIMC_SD_PAD_SOURCE &&
1182 vb2_is_busy(&fimc->vid_cap.vbq))
1185 mutex_lock(&fimc->lock);
1186 ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1187 &mf->code, NULL, fmt->pad);
1188 mutex_unlock(&fimc->lock);
1189 mf->colorspace = V4L2_COLORSPACE_JPEG;
1191 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1192 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1196 fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ffmt->color));
1198 ff = fmt->pad == FIMC_SD_PAD_SINK ?
1199 &ctx->s_frame : &ctx->d_frame;
1201 mutex_lock(&fimc->lock);
1202 set_frame_bounds(ff, mf->width, mf->height);
1205 /* Reset the crop rectangle if required. */
1206 if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_DST_CROP)))
1207 set_frame_crop(ff, 0, 0, mf->width, mf->height);
1209 if (fmt->pad == FIMC_SD_PAD_SINK)
1210 ctx->state &= ~FIMC_DST_CROP;
1211 mutex_unlock(&fimc->lock);
1215 static int fimc_subdev_get_crop(struct v4l2_subdev *sd,
1216 struct v4l2_subdev_fh *fh,
1217 struct v4l2_subdev_crop *crop)
1219 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1220 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1221 struct v4l2_rect *r = &crop->rect;
1222 struct fimc_frame *ff;
1224 if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
1225 crop->rect = *v4l2_subdev_get_try_crop(fh, crop->pad);
1228 ff = crop->pad == FIMC_SD_PAD_SINK ?
1229 &ctx->s_frame : &ctx->d_frame;
1231 mutex_lock(&fimc->lock);
1232 r->left = ff->offs_h;
1233 r->top = ff->offs_v;
1234 r->width = ff->width;
1235 r->height = ff->height;
1236 mutex_unlock(&fimc->lock);
1238 dbg("ff:%p, pad%d: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1239 ff, crop->pad, r->left, r->top, r->width, r->height,
1240 ff->f_width, ff->f_height);
1245 static int fimc_subdev_set_crop(struct v4l2_subdev *sd,
1246 struct v4l2_subdev_fh *fh,
1247 struct v4l2_subdev_crop *crop)
1249 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1250 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1251 struct v4l2_rect *r = &crop->rect;
1252 struct fimc_frame *ff;
1253 unsigned long flags;
1255 dbg("(%d,%d)/%dx%d", r->left, r->top, r->width, r->height);
1257 ff = crop->pad == FIMC_SD_PAD_SOURCE ?
1258 &ctx->d_frame : &ctx->s_frame;
1260 mutex_lock(&fimc->lock);
1261 fimc_capture_try_crop(ctx, r, crop->pad);
1263 if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
1264 mutex_lock(&fimc->lock);
1265 *v4l2_subdev_get_try_crop(fh, crop->pad) = *r;
1268 spin_lock_irqsave(&fimc->slock, flags);
1269 set_frame_crop(ff, r->left, r->top, r->width, r->height);
1270 if (crop->pad == FIMC_SD_PAD_SOURCE)
1271 ctx->state |= FIMC_DST_CROP;
1273 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1274 spin_unlock_irqrestore(&fimc->slock, flags);
1276 dbg("pad%d: (%d,%d)/%dx%d", crop->pad, r->left, r->top,
1277 r->width, r->height);
1279 mutex_unlock(&fimc->lock);
1283 static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1284 .enum_mbus_code = fimc_subdev_enum_mbus_code,
1285 .get_fmt = fimc_subdev_get_fmt,
1286 .set_fmt = fimc_subdev_set_fmt,
1287 .get_crop = fimc_subdev_get_crop,
1288 .set_crop = fimc_subdev_set_crop,
1291 static struct v4l2_subdev_ops fimc_subdev_ops = {
1292 .pad = &fimc_subdev_pad_ops,
1295 static int fimc_create_capture_subdev(struct fimc_dev *fimc,
1296 struct v4l2_device *v4l2_dev)
1298 struct v4l2_subdev *sd;
1301 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
1305 v4l2_subdev_init(sd, &fimc_subdev_ops);
1306 sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
1307 snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->pdev->id);
1309 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1310 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1311 ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
1312 fimc->vid_cap.sd_pads, 0);
1315 ret = v4l2_device_register_subdev(v4l2_dev, sd);
1319 fimc->vid_cap.subdev = sd;
1320 v4l2_set_subdevdata(sd, fimc);
1321 sd->entity.ops = &fimc_sd_media_ops;
1324 media_entity_cleanup(&sd->entity);
1330 static void fimc_destroy_capture_subdev(struct fimc_dev *fimc)
1332 struct v4l2_subdev *sd = fimc->vid_cap.subdev;
1336 media_entity_cleanup(&sd->entity);
1337 v4l2_device_unregister_subdev(sd);
1342 /* Set default format at the sensor and host interface */
1343 static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1345 struct v4l2_format fmt = {
1346 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1350 .pixelformat = V4L2_PIX_FMT_YUYV,
1351 .field = V4L2_FIELD_NONE,
1352 .colorspace = V4L2_COLORSPACE_JPEG,
1356 return fimc_capture_set_format(fimc, &fmt);
1359 /* fimc->lock must be already initialized */
1360 int fimc_register_capture_device(struct fimc_dev *fimc,
1361 struct v4l2_device *v4l2_dev)
1363 struct video_device *vfd;
1364 struct fimc_vid_cap *vid_cap;
1365 struct fimc_ctx *ctx;
1366 struct vb2_queue *q;
1369 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1373 ctx->fimc_dev = fimc;
1374 ctx->in_path = FIMC_CAMERA;
1375 ctx->out_path = FIMC_DMA;
1376 ctx->state = FIMC_CTX_CAP;
1377 ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1378 ctx->d_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1380 vfd = video_device_alloc();
1382 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
1386 snprintf(vfd->name, sizeof(vfd->name), "%s.capture",
1387 dev_name(&fimc->pdev->dev));
1389 vfd->fops = &fimc_capture_fops;
1390 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
1391 vfd->v4l2_dev = v4l2_dev;
1393 vfd->release = video_device_release;
1394 vfd->lock = &fimc->lock;
1395 video_set_drvdata(vfd, fimc);
1397 vid_cap = &fimc->vid_cap;
1399 vid_cap->active_buf_cnt = 0;
1400 vid_cap->reqbufs_count = 0;
1401 vid_cap->refcnt = 0;
1403 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1404 INIT_LIST_HEAD(&vid_cap->active_buf_q);
1405 spin_lock_init(&ctx->slock);
1408 q = &fimc->vid_cap.vbq;
1409 memset(q, 0, sizeof(*q));
1410 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1411 q->io_modes = VB2_MMAP | VB2_USERPTR;
1412 q->drv_priv = fimc->vid_cap.ctx;
1413 q->ops = &fimc_capture_qops;
1414 q->mem_ops = &vb2_dma_contig_memops;
1415 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1419 fimc->vid_cap.vd_pad.flags = MEDIA_PAD_FL_SINK;
1420 ret = media_entity_init(&vfd->entity, 1, &fimc->vid_cap.vd_pad, 0);
1423 ret = fimc_create_capture_subdev(fimc, v4l2_dev);
1427 vfd->ctrl_handler = &ctx->ctrl_handler;
1431 media_entity_cleanup(&vfd->entity);
1433 video_device_release(vfd);
1439 void fimc_unregister_capture_device(struct fimc_dev *fimc)
1441 struct video_device *vfd = fimc->vid_cap.vfd;
1444 media_entity_cleanup(&vfd->entity);
1445 /* Can also be called if video device was
1447 video_unregister_device(vfd);
1449 fimc_destroy_capture_subdev(fimc);
1450 kfree(fimc->vid_cap.ctx);
1451 fimc->vid_cap.ctx = NULL;