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, bool suspend)
74 struct fimc_vid_cap *cap = &fimc->vid_cap;
75 struct fimc_vid_buffer *buf;
79 spin_lock_irqsave(&fimc->slock, flags);
80 streaming = fimc->state & (1 << ST_CAPT_ISP_STREAM);
82 fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_SHUT |
83 1 << ST_CAPT_STREAM | 1 << ST_CAPT_ISP_STREAM);
85 fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED);
87 /* Release unused buffers */
88 while (!suspend && !list_empty(&cap->pending_buf_q)) {
89 buf = fimc_pending_queue_pop(cap);
90 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
92 /* If suspending put unused buffers onto pending queue */
93 while (!list_empty(&cap->active_buf_q)) {
94 buf = fimc_active_queue_pop(cap);
96 fimc_pending_queue_add(cap, buf);
98 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
100 set_bit(ST_CAPT_SUSPENDED, &fimc->state);
101 spin_unlock_irqrestore(&fimc->slock, flags);
104 return fimc_pipeline_s_stream(fimc, 0);
109 static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend)
113 if (!fimc_capture_active(fimc))
116 spin_lock_irqsave(&fimc->slock, flags);
117 set_bit(ST_CAPT_SHUT, &fimc->state);
118 fimc_deactivate_capture(fimc);
119 spin_unlock_irqrestore(&fimc->slock, flags);
121 wait_event_timeout(fimc->irq_queue,
122 !test_bit(ST_CAPT_SHUT, &fimc->state),
123 (2*HZ/10)); /* 200 ms */
125 return fimc_capture_state_cleanup(fimc, suspend);
129 * fimc_capture_config_update - apply the camera interface configuration
131 * To be called from within the interrupt handler with fimc.slock
132 * spinlock held. It updates the camera pixel crop, rotation and
135 int fimc_capture_config_update(struct fimc_ctx *ctx)
137 struct fimc_dev *fimc = ctx->fimc_dev;
140 if (test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
143 spin_lock(&ctx->slock);
144 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
145 ret = fimc_set_scaler_info(ctx);
147 fimc_hw_set_prescaler(ctx);
148 fimc_hw_set_mainscaler(ctx);
149 fimc_hw_set_target_format(ctx);
150 fimc_hw_set_rotation(ctx);
151 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
152 fimc_hw_set_out_dma(ctx);
153 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
155 spin_unlock(&ctx->slock);
159 static int start_streaming(struct vb2_queue *q, unsigned int count)
161 struct fimc_ctx *ctx = q->drv_priv;
162 struct fimc_dev *fimc = ctx->fimc_dev;
163 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
168 vid_cap->frame_count = 0;
170 ret = fimc_init_capture(fimc);
174 set_bit(ST_CAPT_PEND, &fimc->state);
176 min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
178 if (vid_cap->active_buf_cnt >= min_bufs &&
179 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
180 fimc_activate_capture(ctx);
182 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
183 fimc_pipeline_s_stream(fimc, 1);
188 fimc_capture_state_cleanup(fimc, false);
192 static int stop_streaming(struct vb2_queue *q)
194 struct fimc_ctx *ctx = q->drv_priv;
195 struct fimc_dev *fimc = ctx->fimc_dev;
197 if (!fimc_capture_active(fimc))
200 return fimc_stop_capture(fimc, false);
203 int fimc_capture_suspend(struct fimc_dev *fimc)
205 bool suspend = fimc_capture_busy(fimc);
207 int ret = fimc_stop_capture(fimc, suspend);
210 return fimc_pipeline_shutdown(fimc);
213 static void buffer_queue(struct vb2_buffer *vb);
215 int fimc_capture_resume(struct fimc_dev *fimc)
217 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
218 struct fimc_vid_buffer *buf;
221 if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
224 INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
225 vid_cap->buf_index = 0;
226 fimc_pipeline_initialize(fimc, &fimc->vid_cap.vfd->entity,
228 fimc_init_capture(fimc);
230 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
232 for (i = 0; i < vid_cap->reqbufs_count; i++) {
233 if (list_empty(&vid_cap->pending_buf_q))
235 buf = fimc_pending_queue_pop(vid_cap);
236 buffer_queue(&buf->vb);
242 static unsigned int get_plane_size(struct fimc_frame *fr, unsigned int plane)
244 if (!fr || plane >= fr->fmt->memplanes)
246 return fr->f_width * fr->f_height * fr->fmt->depth[plane] / 8;
249 static int queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
250 unsigned int *num_planes, unsigned int sizes[],
253 struct fimc_ctx *ctx = vq->drv_priv;
254 struct fimc_fmt *fmt = ctx->d_frame.fmt;
260 *num_planes = fmt->memplanes;
262 for (i = 0; i < fmt->memplanes; i++) {
263 sizes[i] = get_plane_size(&ctx->d_frame, i);
264 allocators[i] = ctx->fimc_dev->alloc_ctx;
270 static int buffer_prepare(struct vb2_buffer *vb)
272 struct vb2_queue *vq = vb->vb2_queue;
273 struct fimc_ctx *ctx = vq->drv_priv;
276 if (ctx->d_frame.fmt == NULL)
279 for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
280 unsigned long size = ctx->d_frame.payload[i];
282 if (vb2_plane_size(vb, i) < size) {
283 v4l2_err(ctx->fimc_dev->vid_cap.vfd,
284 "User buffer too small (%ld < %ld)\n",
285 vb2_plane_size(vb, i), size);
288 vb2_set_plane_payload(vb, i, size);
294 static void buffer_queue(struct vb2_buffer *vb)
296 struct fimc_vid_buffer *buf
297 = container_of(vb, struct fimc_vid_buffer, vb);
298 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
299 struct fimc_dev *fimc = ctx->fimc_dev;
300 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
304 spin_lock_irqsave(&fimc->slock, flags);
305 fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
307 if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
308 !test_bit(ST_CAPT_STREAM, &fimc->state) &&
309 vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
310 /* Setup the buffer directly for processing. */
311 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
314 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
315 buf->index = vid_cap->buf_index;
316 fimc_active_queue_add(vid_cap, buf);
318 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
319 vid_cap->buf_index = 0;
321 fimc_pending_queue_add(vid_cap, buf);
324 min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
327 if (vb2_is_streaming(&vid_cap->vbq) &&
328 vid_cap->active_buf_cnt >= min_bufs &&
329 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
330 fimc_activate_capture(ctx);
331 spin_unlock_irqrestore(&fimc->slock, flags);
333 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
334 fimc_pipeline_s_stream(fimc, 1);
337 spin_unlock_irqrestore(&fimc->slock, flags);
340 static void fimc_lock(struct vb2_queue *vq)
342 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
343 mutex_lock(&ctx->fimc_dev->lock);
346 static void fimc_unlock(struct vb2_queue *vq)
348 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
349 mutex_unlock(&ctx->fimc_dev->lock);
352 static struct vb2_ops fimc_capture_qops = {
353 .queue_setup = queue_setup,
354 .buf_prepare = buffer_prepare,
355 .buf_queue = buffer_queue,
356 .wait_prepare = fimc_unlock,
357 .wait_finish = fimc_lock,
358 .start_streaming = start_streaming,
359 .stop_streaming = stop_streaming,
363 * fimc_capture_ctrls_create - initialize the control handler
364 * Initialize the capture video node control handler and fill it
365 * with the FIMC controls. Inherit any sensor's controls if the
366 * 'user_subdev_api' flag is false (default behaviour).
367 * This function need to be called with the graph mutex held.
369 int fimc_capture_ctrls_create(struct fimc_dev *fimc)
371 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
374 if (WARN_ON(vid_cap->ctx == NULL))
376 if (vid_cap->ctx->ctrls_rdy)
379 ret = fimc_ctrls_create(vid_cap->ctx);
380 if (ret || vid_cap->user_subdev_api)
383 return v4l2_ctrl_add_handler(&vid_cap->ctx->ctrl_handler,
384 fimc->pipeline.sensor->ctrl_handler);
387 static int fimc_capture_set_default_format(struct fimc_dev *fimc);
389 static int fimc_capture_open(struct file *file)
391 struct fimc_dev *fimc = video_drvdata(file);
392 int ret = v4l2_fh_open(file);
397 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
399 /* Return if the corresponding video mem2mem node is already opened. */
400 if (fimc_m2m_active(fimc))
403 set_bit(ST_CAPT_BUSY, &fimc->state);
404 pm_runtime_get_sync(&fimc->pdev->dev);
406 if (++fimc->vid_cap.refcnt == 1) {
407 ret = fimc_pipeline_initialize(fimc,
408 &fimc->vid_cap.vfd->entity, true);
410 dev_err(&fimc->pdev->dev,
411 "Video pipeline initialization failed\n");
412 pm_runtime_put_sync(&fimc->pdev->dev);
413 fimc->vid_cap.refcnt--;
414 v4l2_fh_release(file);
415 clear_bit(ST_CAPT_BUSY, &fimc->state);
418 ret = fimc_capture_ctrls_create(fimc);
420 if (!ret && !fimc->vid_cap.user_subdev_api)
421 ret = fimc_capture_set_default_format(fimc);
426 static int fimc_capture_close(struct file *file)
428 struct fimc_dev *fimc = video_drvdata(file);
430 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
432 if (--fimc->vid_cap.refcnt == 0) {
433 clear_bit(ST_CAPT_BUSY, &fimc->state);
434 fimc_stop_capture(fimc, false);
435 fimc_pipeline_shutdown(fimc);
436 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
439 pm_runtime_put(&fimc->pdev->dev);
441 if (fimc->vid_cap.refcnt == 0) {
442 vb2_queue_release(&fimc->vid_cap.vbq);
443 fimc_ctrls_delete(fimc->vid_cap.ctx);
445 return v4l2_fh_release(file);
448 static unsigned int fimc_capture_poll(struct file *file,
449 struct poll_table_struct *wait)
451 struct fimc_dev *fimc = video_drvdata(file);
453 return vb2_poll(&fimc->vid_cap.vbq, file, wait);
456 static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
458 struct fimc_dev *fimc = video_drvdata(file);
460 return vb2_mmap(&fimc->vid_cap.vbq, vma);
463 static const struct v4l2_file_operations fimc_capture_fops = {
464 .owner = THIS_MODULE,
465 .open = fimc_capture_open,
466 .release = fimc_capture_close,
467 .poll = fimc_capture_poll,
468 .unlocked_ioctl = video_ioctl2,
469 .mmap = fimc_capture_mmap,
473 * Format and crop negotiation helpers
476 static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
477 u32 *width, u32 *height,
478 u32 *code, u32 *fourcc, int pad)
480 bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
481 struct fimc_dev *fimc = ctx->fimc_dev;
482 struct samsung_fimc_variant *var = fimc->variant;
483 struct fimc_pix_limit *pl = var->pix_limit;
484 struct fimc_frame *dst = &ctx->d_frame;
485 u32 depth, min_w, max_w, min_h, align_h = 3;
486 u32 mask = FMT_FLAGS_CAM;
487 struct fimc_fmt *ffmt;
489 /* Color conversion from/to JPEG is not supported */
490 if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
491 fimc_fmt_is_jpeg(ctx->s_frame.fmt->color))
492 *code = V4L2_MBUS_FMT_JPEG_1X8;
494 if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad != FIMC_SD_PAD_SINK)
495 mask |= FMT_FLAGS_M2M;
497 ffmt = fimc_find_format(fourcc, code, mask, 0);
501 *code = ffmt->mbus_code;
503 *fourcc = ffmt->fourcc;
505 if (pad == FIMC_SD_PAD_SINK) {
506 max_w = fimc_fmt_is_jpeg(ffmt->color) ?
507 pl->scaler_dis_w : pl->scaler_en_w;
508 /* Apply the camera input interface pixel constraints */
509 v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
510 height, max_t(u32, *height, 32),
511 FIMC_CAMIF_MAX_HEIGHT,
512 fimc_fmt_is_jpeg(ffmt->color) ? 3 : 1,
516 /* Can't scale or crop in transparent (JPEG) transfer mode */
517 if (fimc_fmt_is_jpeg(ffmt->color)) {
518 *width = ctx->s_frame.f_width;
519 *height = ctx->s_frame.f_height;
522 /* Apply the scaler and the output DMA constraints */
523 max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
524 min_w = ctx->state & FIMC_DST_CROP ? dst->width : var->min_out_pixsize;
525 min_h = ctx->state & FIMC_DST_CROP ? dst->height : var->min_out_pixsize;
526 if (fimc->id == 1 && var->pix_hoff)
527 align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
529 depth = fimc_get_format_depth(ffmt);
530 v4l_bound_align_image(width, min_w, max_w,
531 ffs(var->min_out_pixsize) - 1,
532 height, min_h, FIMC_CAMIF_MAX_HEIGHT,
534 64/(ALIGN(depth, 8)));
536 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
537 pad, code ? *code : 0, *width, *height,
538 dst->f_width, dst->f_height);
543 static void fimc_capture_try_crop(struct fimc_ctx *ctx, struct v4l2_rect *r,
546 bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
547 struct fimc_dev *fimc = ctx->fimc_dev;
548 struct samsung_fimc_variant *var = fimc->variant;
549 struct fimc_pix_limit *pl = var->pix_limit;
550 struct fimc_frame *sink = &ctx->s_frame;
551 u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
552 u32 align_sz = 0, align_h = 4;
553 u32 max_sc_h, max_sc_v;
555 /* In JPEG transparent transfer mode cropping is not supported */
556 if (fimc_fmt_is_jpeg(ctx->d_frame.fmt->color)) {
557 r->width = sink->f_width;
558 r->height = sink->f_height;
559 r->left = r->top = 0;
562 if (pad == FIMC_SD_PAD_SOURCE) {
563 if (ctx->rotation != 90 && ctx->rotation != 270)
565 max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3));
566 max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1));
567 min_sz = var->min_out_pixsize;
569 u32 depth = fimc_get_format_depth(sink->fmt);
570 align_sz = 64/ALIGN(depth, 8);
571 min_sz = var->min_inp_pixsize;
572 min_w = min_h = min_sz;
573 max_sc_h = max_sc_v = 1;
576 * For the crop rectangle at source pad the following constraints
578 * - it must fit in the sink pad format rectangle (f_width/f_height);
579 * - maximum downscaling ratio is 64;
580 * - maximum crop size depends if the rotator is used or not;
581 * - the sink pad format width/height must be 4 multiple of the
582 * prescaler ratios determined by sink pad size and source pad crop,
583 * the prescaler ratio is returned by fimc_get_scaler_factor().
586 rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
587 rotate ? sink->f_height : sink->f_width);
588 max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
589 if (pad == FIMC_SD_PAD_SOURCE) {
590 min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
591 min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
593 swap(max_sc_h, max_sc_v);
597 v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
598 &r->height, min_h, max_h, align_h,
600 /* Adjust left/top if cropping rectangle is out of bounds */
601 r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
602 r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height);
603 r->left = round_down(r->left, var->hor_offs_align);
605 dbg("pad%d: (%d,%d)/%dx%d, sink fmt: %dx%d",
606 pad, r->left, r->top, r->width, r->height,
607 sink->f_width, sink->f_height);
611 * The video node ioctl operations
613 static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
614 struct v4l2_capability *cap)
616 struct fimc_dev *fimc = video_drvdata(file);
618 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
619 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
620 cap->bus_info[0] = 0;
621 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
622 V4L2_CAP_VIDEO_CAPTURE_MPLANE;
627 static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
628 struct v4l2_fmtdesc *f)
630 struct fimc_fmt *fmt;
632 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
636 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
637 f->pixelformat = fmt->fourcc;
638 if (fmt->fourcc == V4L2_MBUS_FMT_JPEG_1X8)
639 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
644 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
646 * @ctx: FIMC capture context
647 * @tfmt: media bus format to try/set on subdevs
648 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
649 * @set: true to set format on subdevs, false to try only
651 static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
652 struct v4l2_mbus_framefmt *tfmt,
653 struct fimc_fmt **fmt_id,
656 struct fimc_dev *fimc = ctx->fimc_dev;
657 struct v4l2_subdev *sd = fimc->pipeline.sensor;
658 struct v4l2_subdev *csis = fimc->pipeline.csis;
659 struct v4l2_subdev_format sfmt;
660 struct v4l2_mbus_framefmt *mf = &sfmt.format;
661 struct fimc_fmt *ffmt = NULL;
664 if (WARN_ON(!sd || !tfmt))
667 memset(&sfmt, 0, sizeof(sfmt));
670 sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
672 ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
676 * Notify user-space if common pixel code for
677 * host and sensor does not exist.
681 mf->code = tfmt->code = ffmt->mbus_code;
683 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
686 if (mf->code != tfmt->code) {
690 if (mf->width != tfmt->width || mf->width != tfmt->width) {
691 u32 fcc = ffmt->fourcc;
692 tfmt->width = mf->width;
693 tfmt->height = mf->height;
694 ffmt = fimc_capture_try_format(ctx,
695 &tfmt->width, &tfmt->height,
696 NULL, &fcc, FIMC_SD_PAD_SOURCE);
697 if (ffmt && ffmt->mbus_code)
698 mf->code = ffmt->mbus_code;
699 if (mf->width != tfmt->width || mf->width != tfmt->width)
701 tfmt->code = mf->code;
704 ret = v4l2_subdev_call(csis, pad, set_fmt, NULL, &sfmt);
706 if (mf->code == tfmt->code &&
707 mf->width == tfmt->width && mf->width == tfmt->width)
715 dbg("code: 0x%x, %dx%d, %p", mf->code, mf->width, mf->height, ffmt);
719 static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
720 struct v4l2_format *f)
722 struct fimc_dev *fimc = video_drvdata(file);
723 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
725 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
728 return fimc_fill_format(&ctx->d_frame, f);
731 static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
732 struct v4l2_format *f)
734 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
735 struct fimc_dev *fimc = video_drvdata(file);
736 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
737 struct v4l2_mbus_framefmt mf;
738 struct fimc_fmt *ffmt = NULL;
740 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
743 if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
744 fimc_capture_try_format(ctx, &pix->width, &pix->height,
745 NULL, &pix->pixelformat,
747 ctx->s_frame.f_width = pix->width;
748 ctx->s_frame.f_height = pix->height;
750 ffmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
751 NULL, &pix->pixelformat,
756 if (!fimc->vid_cap.user_subdev_api) {
757 mf.width = pix->width;
758 mf.height = pix->height;
759 mf.code = ffmt->mbus_code;
760 fimc_md_graph_lock(fimc);
761 fimc_pipeline_try_format(ctx, &mf, &ffmt, false);
762 fimc_md_graph_unlock(fimc);
764 pix->width = mf.width;
765 pix->height = mf.height;
767 pix->pixelformat = ffmt->fourcc;
770 fimc_adjust_mplane_format(ffmt, pix->width, pix->height, pix);
774 static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx, bool jpeg)
776 ctx->scaler.enabled = !jpeg;
777 fimc_ctrls_activate(ctx, !jpeg);
780 set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
782 clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
785 static int fimc_capture_set_format(struct fimc_dev *fimc, struct v4l2_format *f)
787 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
788 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
789 struct v4l2_mbus_framefmt *mf = &fimc->vid_cap.mf;
790 struct fimc_frame *ff = &ctx->d_frame;
791 struct fimc_fmt *s_fmt = NULL;
794 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
796 if (vb2_is_busy(&fimc->vid_cap.vbq))
799 /* Pre-configure format at camera interface input, for JPEG only */
800 if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
801 fimc_capture_try_format(ctx, &pix->width, &pix->height,
802 NULL, &pix->pixelformat,
804 ctx->s_frame.f_width = pix->width;
805 ctx->s_frame.f_height = pix->height;
807 /* Try the format at the scaler and the DMA output */
808 ff->fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
809 NULL, &pix->pixelformat,
813 /* Try to match format at the host and the sensor */
814 if (!fimc->vid_cap.user_subdev_api) {
815 mf->code = ff->fmt->mbus_code;
816 mf->width = pix->width;
817 mf->height = pix->height;
819 fimc_md_graph_lock(fimc);
820 ret = fimc_pipeline_try_format(ctx, mf, &s_fmt, true);
821 fimc_md_graph_unlock(fimc);
824 pix->width = mf->width;
825 pix->height = mf->height;
827 fimc_adjust_mplane_format(ff->fmt, pix->width, pix->height, pix);
828 for (i = 0; i < ff->fmt->colplanes; i++)
830 (pix->width * pix->height * ff->fmt->depth[i]) / 8;
832 set_frame_bounds(ff, pix->width, pix->height);
833 /* Reset the composition rectangle if not yet configured */
834 if (!(ctx->state & FIMC_DST_CROP))
835 set_frame_crop(ff, 0, 0, pix->width, pix->height);
837 fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ff->fmt->color));
839 /* Reset cropping and set format at the camera interface input */
840 if (!fimc->vid_cap.user_subdev_api) {
841 ctx->s_frame.fmt = s_fmt;
842 set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
843 set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
849 static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
850 struct v4l2_format *f)
852 struct fimc_dev *fimc = video_drvdata(file);
854 return fimc_capture_set_format(fimc, f);
857 static int fimc_cap_enum_input(struct file *file, void *priv,
858 struct v4l2_input *i)
860 struct fimc_dev *fimc = video_drvdata(file);
861 struct v4l2_subdev *sd = fimc->pipeline.sensor;
866 i->type = V4L2_INPUT_TYPE_CAMERA;
868 strlcpy(i->name, sd->name, sizeof(i->name));
872 static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
874 return i == 0 ? i : -EINVAL;
877 static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
884 * fimc_pipeline_validate - check for formats inconsistencies
885 * between source and sink pad of each link
887 * Return 0 if all formats match or -EPIPE otherwise.
889 static int fimc_pipeline_validate(struct fimc_dev *fimc)
891 struct v4l2_subdev_format sink_fmt, src_fmt;
892 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
893 struct v4l2_subdev *sd;
894 struct media_pad *pad;
897 /* Start with the video capture node pad */
898 pad = media_entity_remote_source(&vid_cap->vd_pad);
901 /* FIMC.{N} subdevice */
902 sd = media_entity_to_v4l2_subdev(pad->entity);
905 /* Retrieve format at the sink pad */
906 pad = &sd->entity.pads[0];
907 if (!(pad->flags & MEDIA_PAD_FL_SINK))
909 /* Don't call FIMC subdev operation to avoid nested locking */
910 if (sd == fimc->vid_cap.subdev) {
911 struct fimc_frame *ff = &vid_cap->ctx->s_frame;
912 sink_fmt.format.width = ff->f_width;
913 sink_fmt.format.height = ff->f_height;
914 sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
916 sink_fmt.pad = pad->index;
917 sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
918 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
919 if (ret < 0 && ret != -ENOIOCTLCMD)
922 /* Retrieve format at the source pad */
923 pad = media_entity_remote_source(pad);
925 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
928 sd = media_entity_to_v4l2_subdev(pad->entity);
929 src_fmt.pad = pad->index;
930 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
931 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
932 if (ret < 0 && ret != -ENOIOCTLCMD)
935 if (src_fmt.format.width != sink_fmt.format.width ||
936 src_fmt.format.height != sink_fmt.format.height ||
937 src_fmt.format.code != sink_fmt.format.code)
943 static int fimc_cap_streamon(struct file *file, void *priv,
944 enum v4l2_buf_type type)
946 struct fimc_dev *fimc = video_drvdata(file);
947 struct fimc_pipeline *p = &fimc->pipeline;
950 if (fimc_capture_active(fimc))
953 media_entity_pipeline_start(&p->sensor->entity, p->pipe);
955 if (fimc->vid_cap.user_subdev_api) {
956 ret = fimc_pipeline_validate(fimc);
960 return vb2_streamon(&fimc->vid_cap.vbq, type);
963 static int fimc_cap_streamoff(struct file *file, void *priv,
964 enum v4l2_buf_type type)
966 struct fimc_dev *fimc = video_drvdata(file);
967 struct v4l2_subdev *sd = fimc->pipeline.sensor;
970 ret = vb2_streamoff(&fimc->vid_cap.vbq, type);
972 media_entity_pipeline_stop(&sd->entity);
976 static int fimc_cap_reqbufs(struct file *file, void *priv,
977 struct v4l2_requestbuffers *reqbufs)
979 struct fimc_dev *fimc = video_drvdata(file);
980 int ret = vb2_reqbufs(&fimc->vid_cap.vbq, reqbufs);
983 fimc->vid_cap.reqbufs_count = reqbufs->count;
987 static int fimc_cap_querybuf(struct file *file, void *priv,
988 struct v4l2_buffer *buf)
990 struct fimc_dev *fimc = video_drvdata(file);
992 return vb2_querybuf(&fimc->vid_cap.vbq, buf);
995 static int fimc_cap_qbuf(struct file *file, void *priv,
996 struct v4l2_buffer *buf)
998 struct fimc_dev *fimc = video_drvdata(file);
1000 return vb2_qbuf(&fimc->vid_cap.vbq, buf);
1003 static int fimc_cap_dqbuf(struct file *file, void *priv,
1004 struct v4l2_buffer *buf)
1006 struct fimc_dev *fimc = video_drvdata(file);
1008 return vb2_dqbuf(&fimc->vid_cap.vbq, buf, file->f_flags & O_NONBLOCK);
1011 static int fimc_cap_cropcap(struct file *file, void *fh,
1012 struct v4l2_cropcap *cr)
1014 struct fimc_dev *fimc = video_drvdata(file);
1015 struct fimc_frame *f = &fimc->vid_cap.ctx->s_frame;
1017 if (cr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1020 cr->bounds.left = 0;
1022 cr->bounds.width = f->o_width;
1023 cr->bounds.height = f->o_height;
1024 cr->defrect = cr->bounds;
1029 static int fimc_cap_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
1031 struct fimc_dev *fimc = video_drvdata(file);
1032 struct fimc_frame *f = &fimc->vid_cap.ctx->s_frame;
1034 cr->c.left = f->offs_h;
1035 cr->c.top = f->offs_v;
1036 cr->c.width = f->width;
1037 cr->c.height = f->height;
1042 static int fimc_cap_s_crop(struct file *file, void *fh, struct v4l2_crop *cr)
1044 struct fimc_dev *fimc = video_drvdata(file);
1045 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1046 struct fimc_frame *ff;
1047 unsigned long flags;
1049 fimc_capture_try_crop(ctx, &cr->c, FIMC_SD_PAD_SINK);
1052 spin_lock_irqsave(&fimc->slock, flags);
1053 set_frame_crop(ff, cr->c.left, cr->c.top, cr->c.width, cr->c.height);
1054 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1055 spin_unlock_irqrestore(&fimc->slock, flags);
1060 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1061 .vidioc_querycap = fimc_vidioc_querycap_capture,
1063 .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
1064 .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
1065 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
1066 .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
1068 .vidioc_reqbufs = fimc_cap_reqbufs,
1069 .vidioc_querybuf = fimc_cap_querybuf,
1071 .vidioc_qbuf = fimc_cap_qbuf,
1072 .vidioc_dqbuf = fimc_cap_dqbuf,
1074 .vidioc_streamon = fimc_cap_streamon,
1075 .vidioc_streamoff = fimc_cap_streamoff,
1077 .vidioc_g_crop = fimc_cap_g_crop,
1078 .vidioc_s_crop = fimc_cap_s_crop,
1079 .vidioc_cropcap = fimc_cap_cropcap,
1081 .vidioc_enum_input = fimc_cap_enum_input,
1082 .vidioc_s_input = fimc_cap_s_input,
1083 .vidioc_g_input = fimc_cap_g_input,
1086 /* Capture subdev media entity operations */
1087 static int fimc_link_setup(struct media_entity *entity,
1088 const struct media_pad *local,
1089 const struct media_pad *remote, u32 flags)
1091 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1092 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1094 if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1097 if (WARN_ON(fimc == NULL))
1100 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1101 local->entity->name, remote->entity->name, flags,
1102 fimc->vid_cap.input);
1104 if (flags & MEDIA_LNK_FL_ENABLED) {
1105 if (fimc->vid_cap.input != 0)
1107 fimc->vid_cap.input = sd->grp_id;
1111 fimc->vid_cap.input = 0;
1115 static const struct media_entity_operations fimc_sd_media_ops = {
1116 .link_setup = fimc_link_setup,
1120 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1121 * @sd: pointer to a subdev generating the notification
1122 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1123 * @arg: pointer to an u32 type integer that stores the frame payload value
1125 * The End Of Frame notification sent by sensor subdev in its still capture
1126 * mode. If there is only a single VSYNC generated by the sensor at the
1127 * beginning of a frame transmission, FIMC does not issue the LastIrq
1128 * (end of frame) interrupt. And this notification is used to complete the
1129 * frame capture and returning a buffer to user-space. Subdev drivers should
1130 * call this notification from their last 'End of frame capture' interrupt.
1132 void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1135 struct fimc_sensor_info *sensor;
1136 struct fimc_vid_buffer *buf;
1137 struct fimc_md *fmd;
1138 struct fimc_dev *fimc;
1139 unsigned long flags;
1144 sensor = v4l2_get_subdev_hostdata(sd);
1145 fmd = entity_to_fimc_mdev(&sd->entity);
1147 spin_lock_irqsave(&fmd->slock, flags);
1148 fimc = sensor ? sensor->host : NULL;
1150 if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1151 test_bit(ST_CAPT_PEND, &fimc->state)) {
1152 unsigned long irq_flags;
1153 spin_lock_irqsave(&fimc->slock, irq_flags);
1154 if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1155 buf = list_entry(fimc->vid_cap.active_buf_q.next,
1156 struct fimc_vid_buffer, list);
1157 vb2_set_plane_payload(&buf->vb, 0, *((u32 *)arg));
1159 fimc_capture_irq_handler(fimc, true);
1160 fimc_deactivate_capture(fimc);
1161 spin_unlock_irqrestore(&fimc->slock, irq_flags);
1163 spin_unlock_irqrestore(&fmd->slock, flags);
1166 static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1167 struct v4l2_subdev_fh *fh,
1168 struct v4l2_subdev_mbus_code_enum *code)
1170 struct fimc_fmt *fmt;
1172 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1175 code->code = fmt->mbus_code;
1179 static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
1180 struct v4l2_subdev_fh *fh,
1181 struct v4l2_subdev_format *fmt)
1183 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1184 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1185 struct v4l2_mbus_framefmt *mf;
1186 struct fimc_frame *ff;
1188 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1189 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1194 mf->colorspace = V4L2_COLORSPACE_JPEG;
1195 ff = fmt->pad == FIMC_SD_PAD_SINK ? &ctx->s_frame : &ctx->d_frame;
1197 mutex_lock(&fimc->lock);
1198 /* The pixel code is same on both input and output pad */
1199 if (!WARN_ON(ctx->s_frame.fmt == NULL))
1200 mf->code = ctx->s_frame.fmt->mbus_code;
1201 mf->width = ff->f_width;
1202 mf->height = ff->f_height;
1203 mutex_unlock(&fimc->lock);
1208 static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
1209 struct v4l2_subdev_fh *fh,
1210 struct v4l2_subdev_format *fmt)
1212 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1213 struct v4l2_mbus_framefmt *mf = &fmt->format;
1214 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1215 struct fimc_frame *ff;
1216 struct fimc_fmt *ffmt;
1218 dbg("pad%d: code: 0x%x, %dx%d",
1219 fmt->pad, mf->code, mf->width, mf->height);
1221 if (fmt->pad == FIMC_SD_PAD_SOURCE &&
1222 vb2_is_busy(&fimc->vid_cap.vbq))
1225 mutex_lock(&fimc->lock);
1226 ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1227 &mf->code, NULL, fmt->pad);
1228 mutex_unlock(&fimc->lock);
1229 mf->colorspace = V4L2_COLORSPACE_JPEG;
1231 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1232 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1236 fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ffmt->color));
1238 ff = fmt->pad == FIMC_SD_PAD_SINK ?
1239 &ctx->s_frame : &ctx->d_frame;
1241 mutex_lock(&fimc->lock);
1242 set_frame_bounds(ff, mf->width, mf->height);
1245 /* Reset the crop rectangle if required. */
1246 if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_DST_CROP)))
1247 set_frame_crop(ff, 0, 0, mf->width, mf->height);
1249 if (fmt->pad == FIMC_SD_PAD_SINK)
1250 ctx->state &= ~FIMC_DST_CROP;
1251 mutex_unlock(&fimc->lock);
1255 static int fimc_subdev_get_crop(struct v4l2_subdev *sd,
1256 struct v4l2_subdev_fh *fh,
1257 struct v4l2_subdev_crop *crop)
1259 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1260 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1261 struct v4l2_rect *r = &crop->rect;
1262 struct fimc_frame *ff;
1264 if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
1265 crop->rect = *v4l2_subdev_get_try_crop(fh, crop->pad);
1268 ff = crop->pad == FIMC_SD_PAD_SINK ?
1269 &ctx->s_frame : &ctx->d_frame;
1271 mutex_lock(&fimc->lock);
1272 r->left = ff->offs_h;
1273 r->top = ff->offs_v;
1274 r->width = ff->width;
1275 r->height = ff->height;
1276 mutex_unlock(&fimc->lock);
1278 dbg("ff:%p, pad%d: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1279 ff, crop->pad, r->left, r->top, r->width, r->height,
1280 ff->f_width, ff->f_height);
1285 static int fimc_subdev_set_crop(struct v4l2_subdev *sd,
1286 struct v4l2_subdev_fh *fh,
1287 struct v4l2_subdev_crop *crop)
1289 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1290 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1291 struct v4l2_rect *r = &crop->rect;
1292 struct fimc_frame *ff;
1293 unsigned long flags;
1295 dbg("(%d,%d)/%dx%d", r->left, r->top, r->width, r->height);
1297 ff = crop->pad == FIMC_SD_PAD_SOURCE ?
1298 &ctx->d_frame : &ctx->s_frame;
1300 mutex_lock(&fimc->lock);
1301 fimc_capture_try_crop(ctx, r, crop->pad);
1303 if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
1304 mutex_lock(&fimc->lock);
1305 *v4l2_subdev_get_try_crop(fh, crop->pad) = *r;
1308 spin_lock_irqsave(&fimc->slock, flags);
1309 set_frame_crop(ff, r->left, r->top, r->width, r->height);
1310 if (crop->pad == FIMC_SD_PAD_SOURCE)
1311 ctx->state |= FIMC_DST_CROP;
1313 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1314 spin_unlock_irqrestore(&fimc->slock, flags);
1316 dbg("pad%d: (%d,%d)/%dx%d", crop->pad, r->left, r->top,
1317 r->width, r->height);
1319 mutex_unlock(&fimc->lock);
1323 static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1324 .enum_mbus_code = fimc_subdev_enum_mbus_code,
1325 .get_fmt = fimc_subdev_get_fmt,
1326 .set_fmt = fimc_subdev_set_fmt,
1327 .get_crop = fimc_subdev_get_crop,
1328 .set_crop = fimc_subdev_set_crop,
1331 static struct v4l2_subdev_ops fimc_subdev_ops = {
1332 .pad = &fimc_subdev_pad_ops,
1335 static int fimc_create_capture_subdev(struct fimc_dev *fimc,
1336 struct v4l2_device *v4l2_dev)
1338 struct v4l2_subdev *sd;
1341 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
1345 v4l2_subdev_init(sd, &fimc_subdev_ops);
1346 sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
1347 snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->pdev->id);
1349 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1350 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1351 ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
1352 fimc->vid_cap.sd_pads, 0);
1355 ret = v4l2_device_register_subdev(v4l2_dev, sd);
1359 fimc->vid_cap.subdev = sd;
1360 v4l2_set_subdevdata(sd, fimc);
1361 sd->entity.ops = &fimc_sd_media_ops;
1364 media_entity_cleanup(&sd->entity);
1370 static void fimc_destroy_capture_subdev(struct fimc_dev *fimc)
1372 struct v4l2_subdev *sd = fimc->vid_cap.subdev;
1376 media_entity_cleanup(&sd->entity);
1377 v4l2_device_unregister_subdev(sd);
1382 /* Set default format at the sensor and host interface */
1383 static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1385 struct v4l2_format fmt = {
1386 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1390 .pixelformat = V4L2_PIX_FMT_YUYV,
1391 .field = V4L2_FIELD_NONE,
1392 .colorspace = V4L2_COLORSPACE_JPEG,
1396 return fimc_capture_set_format(fimc, &fmt);
1399 /* fimc->lock must be already initialized */
1400 int fimc_register_capture_device(struct fimc_dev *fimc,
1401 struct v4l2_device *v4l2_dev)
1403 struct video_device *vfd;
1404 struct fimc_vid_cap *vid_cap;
1405 struct fimc_ctx *ctx;
1406 struct vb2_queue *q;
1409 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1413 ctx->fimc_dev = fimc;
1414 ctx->in_path = FIMC_CAMERA;
1415 ctx->out_path = FIMC_DMA;
1416 ctx->state = FIMC_CTX_CAP;
1417 ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1418 ctx->d_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1420 vfd = video_device_alloc();
1422 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
1426 snprintf(vfd->name, sizeof(vfd->name), "%s.capture",
1427 dev_name(&fimc->pdev->dev));
1429 vfd->fops = &fimc_capture_fops;
1430 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
1431 vfd->v4l2_dev = v4l2_dev;
1433 vfd->release = video_device_release;
1434 vfd->lock = &fimc->lock;
1435 video_set_drvdata(vfd, fimc);
1437 vid_cap = &fimc->vid_cap;
1439 vid_cap->active_buf_cnt = 0;
1440 vid_cap->reqbufs_count = 0;
1441 vid_cap->refcnt = 0;
1443 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1444 INIT_LIST_HEAD(&vid_cap->active_buf_q);
1445 spin_lock_init(&ctx->slock);
1448 q = &fimc->vid_cap.vbq;
1449 memset(q, 0, sizeof(*q));
1450 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1451 q->io_modes = VB2_MMAP | VB2_USERPTR;
1452 q->drv_priv = fimc->vid_cap.ctx;
1453 q->ops = &fimc_capture_qops;
1454 q->mem_ops = &vb2_dma_contig_memops;
1455 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1459 fimc->vid_cap.vd_pad.flags = MEDIA_PAD_FL_SINK;
1460 ret = media_entity_init(&vfd->entity, 1, &fimc->vid_cap.vd_pad, 0);
1463 ret = fimc_create_capture_subdev(fimc, v4l2_dev);
1467 vfd->ctrl_handler = &ctx->ctrl_handler;
1471 media_entity_cleanup(&vfd->entity);
1473 video_device_release(vfd);
1479 void fimc_unregister_capture_device(struct fimc_dev *fimc)
1481 struct video_device *vfd = fimc->vid_cap.vfd;
1484 media_entity_cleanup(&vfd->entity);
1485 /* Can also be called if video device was
1487 video_unregister_device(vfd);
1489 fimc_destroy_capture_subdev(fimc);
1490 kfree(fimc->vid_cap.ctx);
1491 fimc->vid_cap.ctx = NULL;