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 if (fimc->variant->has_alpha)
67 fimc_hw_set_rgb_alpha(ctx);
68 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
70 spin_unlock_irqrestore(&fimc->slock, flags);
74 static int fimc_capture_state_cleanup(struct fimc_dev *fimc, bool suspend)
76 struct fimc_vid_cap *cap = &fimc->vid_cap;
77 struct fimc_vid_buffer *buf;
81 spin_lock_irqsave(&fimc->slock, flags);
82 streaming = fimc->state & (1 << ST_CAPT_ISP_STREAM);
84 fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_SHUT |
85 1 << ST_CAPT_STREAM | 1 << ST_CAPT_ISP_STREAM);
87 fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED);
89 /* Release unused buffers */
90 while (!suspend && !list_empty(&cap->pending_buf_q)) {
91 buf = fimc_pending_queue_pop(cap);
92 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
94 /* If suspending put unused buffers onto pending queue */
95 while (!list_empty(&cap->active_buf_q)) {
96 buf = fimc_active_queue_pop(cap);
98 fimc_pending_queue_add(cap, buf);
100 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
102 set_bit(ST_CAPT_SUSPENDED, &fimc->state);
107 spin_unlock_irqrestore(&fimc->slock, flags);
110 return fimc_pipeline_s_stream(fimc, 0);
115 static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend)
119 if (!fimc_capture_active(fimc))
122 spin_lock_irqsave(&fimc->slock, flags);
123 set_bit(ST_CAPT_SHUT, &fimc->state);
124 fimc_deactivate_capture(fimc);
125 spin_unlock_irqrestore(&fimc->slock, flags);
127 wait_event_timeout(fimc->irq_queue,
128 !test_bit(ST_CAPT_SHUT, &fimc->state),
129 (2*HZ/10)); /* 200 ms */
131 return fimc_capture_state_cleanup(fimc, suspend);
135 * fimc_capture_config_update - apply the camera interface configuration
137 * To be called from within the interrupt handler with fimc.slock
138 * spinlock held. It updates the camera pixel crop, rotation and
141 int fimc_capture_config_update(struct fimc_ctx *ctx)
143 struct fimc_dev *fimc = ctx->fimc_dev;
146 if (!test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
149 spin_lock(&ctx->slock);
150 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
151 ret = fimc_set_scaler_info(ctx);
153 fimc_hw_set_prescaler(ctx);
154 fimc_hw_set_mainscaler(ctx);
155 fimc_hw_set_target_format(ctx);
156 fimc_hw_set_rotation(ctx);
157 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
158 fimc_hw_set_out_dma(ctx);
159 if (fimc->variant->has_alpha)
160 fimc_hw_set_rgb_alpha(ctx);
161 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
163 spin_unlock(&ctx->slock);
167 static int start_streaming(struct vb2_queue *q, unsigned int count)
169 struct fimc_ctx *ctx = q->drv_priv;
170 struct fimc_dev *fimc = ctx->fimc_dev;
171 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
175 vid_cap->frame_count = 0;
177 ret = fimc_init_capture(fimc);
181 set_bit(ST_CAPT_PEND, &fimc->state);
183 min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
185 if (vid_cap->active_buf_cnt >= min_bufs &&
186 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
187 fimc_activate_capture(ctx);
189 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
190 fimc_pipeline_s_stream(fimc, 1);
195 fimc_capture_state_cleanup(fimc, false);
199 static int stop_streaming(struct vb2_queue *q)
201 struct fimc_ctx *ctx = q->drv_priv;
202 struct fimc_dev *fimc = ctx->fimc_dev;
204 if (!fimc_capture_active(fimc))
207 return fimc_stop_capture(fimc, false);
210 int fimc_capture_suspend(struct fimc_dev *fimc)
212 bool suspend = fimc_capture_busy(fimc);
214 int ret = fimc_stop_capture(fimc, suspend);
217 return fimc_pipeline_shutdown(fimc);
220 static void buffer_queue(struct vb2_buffer *vb);
222 int fimc_capture_resume(struct fimc_dev *fimc)
224 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
225 struct fimc_vid_buffer *buf;
228 if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
231 INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
232 vid_cap->buf_index = 0;
233 fimc_pipeline_initialize(fimc, &fimc->vid_cap.vfd->entity,
235 fimc_init_capture(fimc);
237 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
239 for (i = 0; i < vid_cap->reqbufs_count; i++) {
240 if (list_empty(&vid_cap->pending_buf_q))
242 buf = fimc_pending_queue_pop(vid_cap);
243 buffer_queue(&buf->vb);
249 static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
250 unsigned int *num_buffers, unsigned int *num_planes,
251 unsigned int sizes[], void *allocators[])
253 const struct v4l2_pix_format_mplane *pixm = NULL;
254 struct fimc_ctx *ctx = vq->drv_priv;
255 struct fimc_frame *frame = &ctx->d_frame;
256 struct fimc_fmt *fmt = frame->fmt;
261 pixm = &pfmt->fmt.pix_mp;
262 fmt = fimc_find_format(&pixm->pixelformat, NULL,
263 FMT_FLAGS_CAM | FMT_FLAGS_M2M, -1);
264 wh = pixm->width * pixm->height;
266 wh = frame->f_width * frame->f_height;
272 *num_planes = fmt->memplanes;
274 for (i = 0; i < fmt->memplanes; i++) {
275 unsigned int size = (wh * fmt->depth[i]) / 8;
277 sizes[i] = max(size, pixm->plane_fmt[i].sizeimage);
280 allocators[i] = ctx->fimc_dev->alloc_ctx;
286 static int buffer_prepare(struct vb2_buffer *vb)
288 struct vb2_queue *vq = vb->vb2_queue;
289 struct fimc_ctx *ctx = vq->drv_priv;
292 if (ctx->d_frame.fmt == NULL)
295 for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
296 unsigned long size = ctx->d_frame.payload[i];
298 if (vb2_plane_size(vb, i) < size) {
299 v4l2_err(ctx->fimc_dev->vid_cap.vfd,
300 "User buffer too small (%ld < %ld)\n",
301 vb2_plane_size(vb, i), size);
304 vb2_set_plane_payload(vb, i, size);
310 static void buffer_queue(struct vb2_buffer *vb)
312 struct fimc_vid_buffer *buf
313 = container_of(vb, struct fimc_vid_buffer, vb);
314 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
315 struct fimc_dev *fimc = ctx->fimc_dev;
316 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
320 spin_lock_irqsave(&fimc->slock, flags);
321 fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
323 if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
324 !test_bit(ST_CAPT_STREAM, &fimc->state) &&
325 vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
326 /* Setup the buffer directly for processing. */
327 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
330 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
331 buf->index = vid_cap->buf_index;
332 fimc_active_queue_add(vid_cap, buf);
334 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
335 vid_cap->buf_index = 0;
337 fimc_pending_queue_add(vid_cap, buf);
340 min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
343 if (vb2_is_streaming(&vid_cap->vbq) &&
344 vid_cap->active_buf_cnt >= min_bufs &&
345 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
346 fimc_activate_capture(ctx);
347 spin_unlock_irqrestore(&fimc->slock, flags);
349 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
350 fimc_pipeline_s_stream(fimc, 1);
353 spin_unlock_irqrestore(&fimc->slock, flags);
356 static void fimc_lock(struct vb2_queue *vq)
358 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
359 mutex_lock(&ctx->fimc_dev->lock);
362 static void fimc_unlock(struct vb2_queue *vq)
364 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
365 mutex_unlock(&ctx->fimc_dev->lock);
368 static struct vb2_ops fimc_capture_qops = {
369 .queue_setup = queue_setup,
370 .buf_prepare = buffer_prepare,
371 .buf_queue = buffer_queue,
372 .wait_prepare = fimc_unlock,
373 .wait_finish = fimc_lock,
374 .start_streaming = start_streaming,
375 .stop_streaming = stop_streaming,
379 * fimc_capture_ctrls_create - initialize the control handler
380 * Initialize the capture video node control handler and fill it
381 * with the FIMC controls. Inherit any sensor's controls if the
382 * 'user_subdev_api' flag is false (default behaviour).
383 * This function need to be called with the graph mutex held.
385 int fimc_capture_ctrls_create(struct fimc_dev *fimc)
387 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
390 if (WARN_ON(vid_cap->ctx == NULL))
392 if (vid_cap->ctx->ctrls_rdy)
395 ret = fimc_ctrls_create(vid_cap->ctx);
396 if (ret || vid_cap->user_subdev_api)
399 return v4l2_ctrl_add_handler(&vid_cap->ctx->ctrl_handler,
400 fimc->pipeline.sensor->ctrl_handler);
403 static int fimc_capture_set_default_format(struct fimc_dev *fimc);
405 static int fimc_capture_open(struct file *file)
407 struct fimc_dev *fimc = video_drvdata(file);
408 int ret = v4l2_fh_open(file);
413 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
415 /* Return if the corresponding video mem2mem node is already opened. */
416 if (fimc_m2m_active(fimc))
419 set_bit(ST_CAPT_BUSY, &fimc->state);
420 pm_runtime_get_sync(&fimc->pdev->dev);
422 if (++fimc->vid_cap.refcnt == 1) {
423 ret = fimc_pipeline_initialize(fimc,
424 &fimc->vid_cap.vfd->entity, true);
426 dev_err(&fimc->pdev->dev,
427 "Video pipeline initialization failed\n");
428 pm_runtime_put_sync(&fimc->pdev->dev);
429 fimc->vid_cap.refcnt--;
430 v4l2_fh_release(file);
431 clear_bit(ST_CAPT_BUSY, &fimc->state);
434 ret = fimc_capture_ctrls_create(fimc);
436 if (!ret && !fimc->vid_cap.user_subdev_api)
437 ret = fimc_capture_set_default_format(fimc);
442 static int fimc_capture_close(struct file *file)
444 struct fimc_dev *fimc = video_drvdata(file);
446 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
448 if (--fimc->vid_cap.refcnt == 0) {
449 clear_bit(ST_CAPT_BUSY, &fimc->state);
450 fimc_stop_capture(fimc, false);
451 fimc_pipeline_shutdown(fimc);
452 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
455 pm_runtime_put(&fimc->pdev->dev);
457 if (fimc->vid_cap.refcnt == 0) {
458 vb2_queue_release(&fimc->vid_cap.vbq);
459 fimc_ctrls_delete(fimc->vid_cap.ctx);
461 return v4l2_fh_release(file);
464 static unsigned int fimc_capture_poll(struct file *file,
465 struct poll_table_struct *wait)
467 struct fimc_dev *fimc = video_drvdata(file);
469 return vb2_poll(&fimc->vid_cap.vbq, file, wait);
472 static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
474 struct fimc_dev *fimc = video_drvdata(file);
476 return vb2_mmap(&fimc->vid_cap.vbq, vma);
479 static const struct v4l2_file_operations fimc_capture_fops = {
480 .owner = THIS_MODULE,
481 .open = fimc_capture_open,
482 .release = fimc_capture_close,
483 .poll = fimc_capture_poll,
484 .unlocked_ioctl = video_ioctl2,
485 .mmap = fimc_capture_mmap,
489 * Format and crop negotiation helpers
492 static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
493 u32 *width, u32 *height,
494 u32 *code, u32 *fourcc, int pad)
496 bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
497 struct fimc_dev *fimc = ctx->fimc_dev;
498 struct samsung_fimc_variant *var = fimc->variant;
499 struct fimc_pix_limit *pl = var->pix_limit;
500 struct fimc_frame *dst = &ctx->d_frame;
501 u32 depth, min_w, max_w, min_h, align_h = 3;
502 u32 mask = FMT_FLAGS_CAM;
503 struct fimc_fmt *ffmt;
505 /* Color conversion from/to JPEG is not supported */
506 if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
507 fimc_fmt_is_jpeg(ctx->s_frame.fmt->color))
508 *code = V4L2_MBUS_FMT_JPEG_1X8;
510 if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad != FIMC_SD_PAD_SINK)
511 mask |= FMT_FLAGS_M2M;
513 ffmt = fimc_find_format(fourcc, code, mask, 0);
517 *code = ffmt->mbus_code;
519 *fourcc = ffmt->fourcc;
521 if (pad == FIMC_SD_PAD_SINK) {
522 max_w = fimc_fmt_is_jpeg(ffmt->color) ?
523 pl->scaler_dis_w : pl->scaler_en_w;
524 /* Apply the camera input interface pixel constraints */
525 v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
526 height, max_t(u32, *height, 32),
527 FIMC_CAMIF_MAX_HEIGHT,
528 fimc_fmt_is_jpeg(ffmt->color) ? 3 : 1,
532 /* Can't scale or crop in transparent (JPEG) transfer mode */
533 if (fimc_fmt_is_jpeg(ffmt->color)) {
534 *width = ctx->s_frame.f_width;
535 *height = ctx->s_frame.f_height;
538 /* Apply the scaler and the output DMA constraints */
539 max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
540 min_w = ctx->state & FIMC_DST_CROP ? dst->width : var->min_out_pixsize;
541 min_h = ctx->state & FIMC_DST_CROP ? dst->height : var->min_out_pixsize;
542 if (var->min_vsize_align == 1 && !rotation)
543 align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
545 depth = fimc_get_format_depth(ffmt);
546 v4l_bound_align_image(width, min_w, max_w,
547 ffs(var->min_out_pixsize) - 1,
548 height, min_h, FIMC_CAMIF_MAX_HEIGHT,
550 64/(ALIGN(depth, 8)));
552 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
553 pad, code ? *code : 0, *width, *height,
554 dst->f_width, dst->f_height);
559 static void fimc_capture_try_crop(struct fimc_ctx *ctx, struct v4l2_rect *r,
562 bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
563 struct fimc_dev *fimc = ctx->fimc_dev;
564 struct samsung_fimc_variant *var = fimc->variant;
565 struct fimc_pix_limit *pl = var->pix_limit;
566 struct fimc_frame *sink = &ctx->s_frame;
567 u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
568 u32 align_sz = 0, align_h = 4;
569 u32 max_sc_h, max_sc_v;
571 /* In JPEG transparent transfer mode cropping is not supported */
572 if (fimc_fmt_is_jpeg(ctx->d_frame.fmt->color)) {
573 r->width = sink->f_width;
574 r->height = sink->f_height;
575 r->left = r->top = 0;
578 if (pad == FIMC_SD_PAD_SOURCE) {
579 if (ctx->rotation != 90 && ctx->rotation != 270)
581 max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3));
582 max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1));
583 min_sz = var->min_out_pixsize;
585 u32 depth = fimc_get_format_depth(sink->fmt);
586 align_sz = 64/ALIGN(depth, 8);
587 min_sz = var->min_inp_pixsize;
588 min_w = min_h = min_sz;
589 max_sc_h = max_sc_v = 1;
592 * For the crop rectangle at source pad the following constraints
594 * - it must fit in the sink pad format rectangle (f_width/f_height);
595 * - maximum downscaling ratio is 64;
596 * - maximum crop size depends if the rotator is used or not;
597 * - the sink pad format width/height must be 4 multiple of the
598 * prescaler ratios determined by sink pad size and source pad crop,
599 * the prescaler ratio is returned by fimc_get_scaler_factor().
602 rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
603 rotate ? sink->f_height : sink->f_width);
604 max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
605 if (pad == FIMC_SD_PAD_SOURCE) {
606 min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
607 min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
609 swap(max_sc_h, max_sc_v);
613 v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
614 &r->height, min_h, max_h, align_h,
616 /* Adjust left/top if cropping rectangle is out of bounds */
617 r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
618 r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height);
619 r->left = round_down(r->left, var->hor_offs_align);
621 dbg("pad%d: (%d,%d)/%dx%d, sink fmt: %dx%d",
622 pad, r->left, r->top, r->width, r->height,
623 sink->f_width, sink->f_height);
627 * The video node ioctl operations
629 static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
630 struct v4l2_capability *cap)
632 struct fimc_dev *fimc = video_drvdata(file);
634 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
635 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
636 cap->bus_info[0] = 0;
637 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
642 static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
643 struct v4l2_fmtdesc *f)
645 struct fimc_fmt *fmt;
647 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
651 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
652 f->pixelformat = fmt->fourcc;
653 if (fmt->fourcc == V4L2_MBUS_FMT_JPEG_1X8)
654 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
659 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
661 * @ctx: FIMC capture context
662 * @tfmt: media bus format to try/set on subdevs
663 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
664 * @set: true to set format on subdevs, false to try only
666 static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
667 struct v4l2_mbus_framefmt *tfmt,
668 struct fimc_fmt **fmt_id,
671 struct fimc_dev *fimc = ctx->fimc_dev;
672 struct v4l2_subdev *sd = fimc->pipeline.sensor;
673 struct v4l2_subdev *csis = fimc->pipeline.csis;
674 struct v4l2_subdev_format sfmt;
675 struct v4l2_mbus_framefmt *mf = &sfmt.format;
676 struct fimc_fmt *ffmt = NULL;
679 if (WARN_ON(!sd || !tfmt))
682 memset(&sfmt, 0, sizeof(sfmt));
685 sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
687 ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
691 * Notify user-space if common pixel code for
692 * host and sensor does not exist.
696 mf->code = tfmt->code = ffmt->mbus_code;
698 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
701 if (mf->code != tfmt->code) {
705 if (mf->width != tfmt->width || mf->height != tfmt->height) {
706 u32 fcc = ffmt->fourcc;
707 tfmt->width = mf->width;
708 tfmt->height = mf->height;
709 ffmt = fimc_capture_try_format(ctx,
710 &tfmt->width, &tfmt->height,
711 NULL, &fcc, FIMC_SD_PAD_SOURCE);
712 if (ffmt && ffmt->mbus_code)
713 mf->code = ffmt->mbus_code;
714 if (mf->width != tfmt->width ||
715 mf->height != tfmt->height)
717 tfmt->code = mf->code;
720 ret = v4l2_subdev_call(csis, pad, set_fmt, NULL, &sfmt);
722 if (mf->code == tfmt->code &&
723 mf->width == tfmt->width && mf->height == tfmt->height)
731 dbg("code: 0x%x, %dx%d, %p", mf->code, mf->width, mf->height, ffmt);
735 static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
736 struct v4l2_format *f)
738 struct fimc_dev *fimc = video_drvdata(file);
739 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
741 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
744 return fimc_fill_format(&ctx->d_frame, f);
747 static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
748 struct v4l2_format *f)
750 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
751 struct fimc_dev *fimc = video_drvdata(file);
752 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
753 struct v4l2_mbus_framefmt mf;
754 struct fimc_fmt *ffmt = NULL;
756 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
759 if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
760 fimc_capture_try_format(ctx, &pix->width, &pix->height,
761 NULL, &pix->pixelformat,
763 ctx->s_frame.f_width = pix->width;
764 ctx->s_frame.f_height = pix->height;
766 ffmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
767 NULL, &pix->pixelformat,
772 if (!fimc->vid_cap.user_subdev_api) {
773 mf.width = pix->width;
774 mf.height = pix->height;
775 mf.code = ffmt->mbus_code;
776 fimc_md_graph_lock(fimc);
777 fimc_pipeline_try_format(ctx, &mf, &ffmt, false);
778 fimc_md_graph_unlock(fimc);
780 pix->width = mf.width;
781 pix->height = mf.height;
783 pix->pixelformat = ffmt->fourcc;
786 fimc_adjust_mplane_format(ffmt, pix->width, pix->height, pix);
790 static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx, bool jpeg)
792 ctx->scaler.enabled = !jpeg;
793 fimc_ctrls_activate(ctx, !jpeg);
796 set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
798 clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
801 static int fimc_capture_set_format(struct fimc_dev *fimc, struct v4l2_format *f)
803 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
804 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
805 struct v4l2_mbus_framefmt *mf = &fimc->vid_cap.mf;
806 struct fimc_frame *ff = &ctx->d_frame;
807 struct fimc_fmt *s_fmt = NULL;
810 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
812 if (vb2_is_busy(&fimc->vid_cap.vbq))
815 /* Pre-configure format at camera interface input, for JPEG only */
816 if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
817 fimc_capture_try_format(ctx, &pix->width, &pix->height,
818 NULL, &pix->pixelformat,
820 ctx->s_frame.f_width = pix->width;
821 ctx->s_frame.f_height = pix->height;
823 /* Try the format at the scaler and the DMA output */
824 ff->fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
825 NULL, &pix->pixelformat,
830 /* Update RGB Alpha control state and value range */
831 fimc_alpha_ctrl_update(ctx);
833 /* Try to match format at the host and the sensor */
834 if (!fimc->vid_cap.user_subdev_api) {
835 mf->code = ff->fmt->mbus_code;
836 mf->width = pix->width;
837 mf->height = pix->height;
839 fimc_md_graph_lock(fimc);
840 ret = fimc_pipeline_try_format(ctx, mf, &s_fmt, true);
841 fimc_md_graph_unlock(fimc);
844 pix->width = mf->width;
845 pix->height = mf->height;
847 fimc_adjust_mplane_format(ff->fmt, pix->width, pix->height, pix);
848 for (i = 0; i < ff->fmt->colplanes; i++)
850 (pix->width * pix->height * ff->fmt->depth[i]) / 8;
852 set_frame_bounds(ff, pix->width, pix->height);
853 /* Reset the composition rectangle if not yet configured */
854 if (!(ctx->state & FIMC_DST_CROP))
855 set_frame_crop(ff, 0, 0, pix->width, pix->height);
857 fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ff->fmt->color));
859 /* Reset cropping and set format at the camera interface input */
860 if (!fimc->vid_cap.user_subdev_api) {
861 ctx->s_frame.fmt = s_fmt;
862 set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
863 set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
869 static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
870 struct v4l2_format *f)
872 struct fimc_dev *fimc = video_drvdata(file);
874 return fimc_capture_set_format(fimc, f);
877 static int fimc_cap_enum_input(struct file *file, void *priv,
878 struct v4l2_input *i)
880 struct fimc_dev *fimc = video_drvdata(file);
881 struct v4l2_subdev *sd = fimc->pipeline.sensor;
886 i->type = V4L2_INPUT_TYPE_CAMERA;
888 strlcpy(i->name, sd->name, sizeof(i->name));
892 static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
894 return i == 0 ? i : -EINVAL;
897 static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
904 * fimc_pipeline_validate - check for formats inconsistencies
905 * between source and sink pad of each link
907 * Return 0 if all formats match or -EPIPE otherwise.
909 static int fimc_pipeline_validate(struct fimc_dev *fimc)
911 struct v4l2_subdev_format sink_fmt, src_fmt;
912 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
913 struct v4l2_subdev *sd;
914 struct media_pad *pad;
917 /* Start with the video capture node pad */
918 pad = media_entity_remote_source(&vid_cap->vd_pad);
921 /* FIMC.{N} subdevice */
922 sd = media_entity_to_v4l2_subdev(pad->entity);
925 /* Retrieve format at the sink pad */
926 pad = &sd->entity.pads[0];
927 if (!(pad->flags & MEDIA_PAD_FL_SINK))
929 /* Don't call FIMC subdev operation to avoid nested locking */
930 if (sd == fimc->vid_cap.subdev) {
931 struct fimc_frame *ff = &vid_cap->ctx->s_frame;
932 sink_fmt.format.width = ff->f_width;
933 sink_fmt.format.height = ff->f_height;
934 sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
936 sink_fmt.pad = pad->index;
937 sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
938 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
939 if (ret < 0 && ret != -ENOIOCTLCMD)
942 /* Retrieve format at the source pad */
943 pad = media_entity_remote_source(pad);
945 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
948 sd = media_entity_to_v4l2_subdev(pad->entity);
949 src_fmt.pad = pad->index;
950 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
951 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
952 if (ret < 0 && ret != -ENOIOCTLCMD)
955 if (src_fmt.format.width != sink_fmt.format.width ||
956 src_fmt.format.height != sink_fmt.format.height ||
957 src_fmt.format.code != sink_fmt.format.code)
963 static int fimc_cap_streamon(struct file *file, void *priv,
964 enum v4l2_buf_type type)
966 struct fimc_dev *fimc = video_drvdata(file);
967 struct fimc_pipeline *p = &fimc->pipeline;
970 if (fimc_capture_active(fimc))
973 media_entity_pipeline_start(&p->sensor->entity, p->pipe);
975 if (fimc->vid_cap.user_subdev_api) {
976 ret = fimc_pipeline_validate(fimc);
980 return vb2_streamon(&fimc->vid_cap.vbq, type);
983 static int fimc_cap_streamoff(struct file *file, void *priv,
984 enum v4l2_buf_type type)
986 struct fimc_dev *fimc = video_drvdata(file);
987 struct v4l2_subdev *sd = fimc->pipeline.sensor;
990 ret = vb2_streamoff(&fimc->vid_cap.vbq, type);
992 media_entity_pipeline_stop(&sd->entity);
996 static int fimc_cap_reqbufs(struct file *file, void *priv,
997 struct v4l2_requestbuffers *reqbufs)
999 struct fimc_dev *fimc = video_drvdata(file);
1000 int ret = vb2_reqbufs(&fimc->vid_cap.vbq, reqbufs);
1003 fimc->vid_cap.reqbufs_count = reqbufs->count;
1007 static int fimc_cap_querybuf(struct file *file, void *priv,
1008 struct v4l2_buffer *buf)
1010 struct fimc_dev *fimc = video_drvdata(file);
1012 return vb2_querybuf(&fimc->vid_cap.vbq, buf);
1015 static int fimc_cap_qbuf(struct file *file, void *priv,
1016 struct v4l2_buffer *buf)
1018 struct fimc_dev *fimc = video_drvdata(file);
1020 return vb2_qbuf(&fimc->vid_cap.vbq, buf);
1023 static int fimc_cap_dqbuf(struct file *file, void *priv,
1024 struct v4l2_buffer *buf)
1026 struct fimc_dev *fimc = video_drvdata(file);
1028 return vb2_dqbuf(&fimc->vid_cap.vbq, buf, file->f_flags & O_NONBLOCK);
1031 static int fimc_cap_create_bufs(struct file *file, void *priv,
1032 struct v4l2_create_buffers *create)
1034 struct fimc_dev *fimc = video_drvdata(file);
1036 return vb2_create_bufs(&fimc->vid_cap.vbq, create);
1039 static int fimc_cap_prepare_buf(struct file *file, void *priv,
1040 struct v4l2_buffer *b)
1042 struct fimc_dev *fimc = video_drvdata(file);
1044 return vb2_prepare_buf(&fimc->vid_cap.vbq, b);
1047 static int fimc_cap_g_selection(struct file *file, void *fh,
1048 struct v4l2_selection *s)
1050 struct fimc_dev *fimc = video_drvdata(file);
1051 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1052 struct fimc_frame *f = &ctx->s_frame;
1054 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1057 switch (s->target) {
1058 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1059 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1061 case V4L2_SEL_TGT_CROP_BOUNDS:
1062 case V4L2_SEL_TGT_CROP_DEFAULT:
1065 s->r.width = f->o_width;
1066 s->r.height = f->o_height;
1069 case V4L2_SEL_TGT_COMPOSE_ACTIVE:
1071 case V4L2_SEL_TGT_CROP_ACTIVE:
1072 s->r.left = f->offs_h;
1073 s->r.top = f->offs_v;
1074 s->r.width = f->width;
1075 s->r.height = f->height;
1082 /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
1083 int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
1085 if (a->left < b->left || a->top < b->top)
1087 if (a->left + a->width > b->left + b->width)
1089 if (a->top + a->height > b->top + b->height)
1095 static int fimc_cap_s_selection(struct file *file, void *fh,
1096 struct v4l2_selection *s)
1098 struct fimc_dev *fimc = video_drvdata(file);
1099 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1100 struct v4l2_rect rect = s->r;
1101 struct fimc_frame *f;
1102 unsigned long flags;
1105 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1108 switch (s->target) {
1109 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1110 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1111 case V4L2_SEL_TGT_COMPOSE_ACTIVE:
1113 pad = FIMC_SD_PAD_SOURCE;
1115 case V4L2_SEL_TGT_CROP_BOUNDS:
1116 case V4L2_SEL_TGT_CROP_DEFAULT:
1117 case V4L2_SEL_TGT_CROP_ACTIVE:
1119 pad = FIMC_SD_PAD_SINK;
1125 fimc_capture_try_crop(ctx, &rect, pad);
1127 if (s->flags & V4L2_SEL_FLAG_LE &&
1128 !enclosed_rectangle(&rect, &s->r))
1131 if (s->flags & V4L2_SEL_FLAG_GE &&
1132 !enclosed_rectangle(&s->r, &rect))
1136 spin_lock_irqsave(&fimc->slock, flags);
1137 set_frame_crop(f, s->r.left, s->r.top, s->r.width,
1139 spin_unlock_irqrestore(&fimc->slock, flags);
1141 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1145 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1146 .vidioc_querycap = fimc_vidioc_querycap_capture,
1148 .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
1149 .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
1150 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
1151 .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
1153 .vidioc_reqbufs = fimc_cap_reqbufs,
1154 .vidioc_querybuf = fimc_cap_querybuf,
1156 .vidioc_qbuf = fimc_cap_qbuf,
1157 .vidioc_dqbuf = fimc_cap_dqbuf,
1159 .vidioc_prepare_buf = fimc_cap_prepare_buf,
1160 .vidioc_create_bufs = fimc_cap_create_bufs,
1162 .vidioc_streamon = fimc_cap_streamon,
1163 .vidioc_streamoff = fimc_cap_streamoff,
1165 .vidioc_g_selection = fimc_cap_g_selection,
1166 .vidioc_s_selection = fimc_cap_s_selection,
1168 .vidioc_enum_input = fimc_cap_enum_input,
1169 .vidioc_s_input = fimc_cap_s_input,
1170 .vidioc_g_input = fimc_cap_g_input,
1173 /* Capture subdev media entity operations */
1174 static int fimc_link_setup(struct media_entity *entity,
1175 const struct media_pad *local,
1176 const struct media_pad *remote, u32 flags)
1178 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1179 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1181 if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1184 if (WARN_ON(fimc == NULL))
1187 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1188 local->entity->name, remote->entity->name, flags,
1189 fimc->vid_cap.input);
1191 if (flags & MEDIA_LNK_FL_ENABLED) {
1192 if (fimc->vid_cap.input != 0)
1194 fimc->vid_cap.input = sd->grp_id;
1198 fimc->vid_cap.input = 0;
1202 static const struct media_entity_operations fimc_sd_media_ops = {
1203 .link_setup = fimc_link_setup,
1207 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1208 * @sd: pointer to a subdev generating the notification
1209 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1210 * @arg: pointer to an u32 type integer that stores the frame payload value
1212 * The End Of Frame notification sent by sensor subdev in its still capture
1213 * mode. If there is only a single VSYNC generated by the sensor at the
1214 * beginning of a frame transmission, FIMC does not issue the LastIrq
1215 * (end of frame) interrupt. And this notification is used to complete the
1216 * frame capture and returning a buffer to user-space. Subdev drivers should
1217 * call this notification from their last 'End of frame capture' interrupt.
1219 void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1222 struct fimc_sensor_info *sensor;
1223 struct fimc_vid_buffer *buf;
1224 struct fimc_md *fmd;
1225 struct fimc_dev *fimc;
1226 unsigned long flags;
1231 sensor = v4l2_get_subdev_hostdata(sd);
1232 fmd = entity_to_fimc_mdev(&sd->entity);
1234 spin_lock_irqsave(&fmd->slock, flags);
1235 fimc = sensor ? sensor->host : NULL;
1237 if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1238 test_bit(ST_CAPT_PEND, &fimc->state)) {
1239 unsigned long irq_flags;
1240 spin_lock_irqsave(&fimc->slock, irq_flags);
1241 if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1242 buf = list_entry(fimc->vid_cap.active_buf_q.next,
1243 struct fimc_vid_buffer, list);
1244 vb2_set_plane_payload(&buf->vb, 0, *((u32 *)arg));
1246 fimc_capture_irq_handler(fimc, true);
1247 fimc_deactivate_capture(fimc);
1248 spin_unlock_irqrestore(&fimc->slock, irq_flags);
1250 spin_unlock_irqrestore(&fmd->slock, flags);
1253 static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1254 struct v4l2_subdev_fh *fh,
1255 struct v4l2_subdev_mbus_code_enum *code)
1257 struct fimc_fmt *fmt;
1259 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1262 code->code = fmt->mbus_code;
1266 static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
1267 struct v4l2_subdev_fh *fh,
1268 struct v4l2_subdev_format *fmt)
1270 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1271 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1272 struct v4l2_mbus_framefmt *mf;
1273 struct fimc_frame *ff;
1275 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1276 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1281 mf->colorspace = V4L2_COLORSPACE_JPEG;
1282 ff = fmt->pad == FIMC_SD_PAD_SINK ? &ctx->s_frame : &ctx->d_frame;
1284 mutex_lock(&fimc->lock);
1285 /* The pixel code is same on both input and output pad */
1286 if (!WARN_ON(ctx->s_frame.fmt == NULL))
1287 mf->code = ctx->s_frame.fmt->mbus_code;
1288 mf->width = ff->f_width;
1289 mf->height = ff->f_height;
1290 mutex_unlock(&fimc->lock);
1295 static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
1296 struct v4l2_subdev_fh *fh,
1297 struct v4l2_subdev_format *fmt)
1299 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1300 struct v4l2_mbus_framefmt *mf = &fmt->format;
1301 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1302 struct fimc_frame *ff;
1303 struct fimc_fmt *ffmt;
1305 dbg("pad%d: code: 0x%x, %dx%d",
1306 fmt->pad, mf->code, mf->width, mf->height);
1308 if (fmt->pad == FIMC_SD_PAD_SOURCE &&
1309 vb2_is_busy(&fimc->vid_cap.vbq))
1312 mutex_lock(&fimc->lock);
1313 ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1314 &mf->code, NULL, fmt->pad);
1315 mutex_unlock(&fimc->lock);
1316 mf->colorspace = V4L2_COLORSPACE_JPEG;
1318 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1319 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1323 /* Update RGB Alpha control state and value range */
1324 fimc_alpha_ctrl_update(ctx);
1326 fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ffmt->color));
1328 ff = fmt->pad == FIMC_SD_PAD_SINK ?
1329 &ctx->s_frame : &ctx->d_frame;
1331 mutex_lock(&fimc->lock);
1332 set_frame_bounds(ff, mf->width, mf->height);
1333 fimc->vid_cap.mf = *mf;
1336 /* Reset the crop rectangle if required. */
1337 if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_DST_CROP)))
1338 set_frame_crop(ff, 0, 0, mf->width, mf->height);
1340 if (fmt->pad == FIMC_SD_PAD_SINK)
1341 ctx->state &= ~FIMC_DST_CROP;
1342 mutex_unlock(&fimc->lock);
1346 static int fimc_subdev_get_crop(struct v4l2_subdev *sd,
1347 struct v4l2_subdev_fh *fh,
1348 struct v4l2_subdev_crop *crop)
1350 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1351 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1352 struct v4l2_rect *r = &crop->rect;
1353 struct fimc_frame *ff;
1355 if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
1356 crop->rect = *v4l2_subdev_get_try_crop(fh, crop->pad);
1359 ff = crop->pad == FIMC_SD_PAD_SINK ?
1360 &ctx->s_frame : &ctx->d_frame;
1362 mutex_lock(&fimc->lock);
1363 r->left = ff->offs_h;
1364 r->top = ff->offs_v;
1365 r->width = ff->width;
1366 r->height = ff->height;
1367 mutex_unlock(&fimc->lock);
1369 dbg("ff:%p, pad%d: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1370 ff, crop->pad, r->left, r->top, r->width, r->height,
1371 ff->f_width, ff->f_height);
1376 static int fimc_subdev_set_crop(struct v4l2_subdev *sd,
1377 struct v4l2_subdev_fh *fh,
1378 struct v4l2_subdev_crop *crop)
1380 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1381 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1382 struct v4l2_rect *r = &crop->rect;
1383 struct fimc_frame *ff;
1384 unsigned long flags;
1386 dbg("(%d,%d)/%dx%d", r->left, r->top, r->width, r->height);
1388 ff = crop->pad == FIMC_SD_PAD_SOURCE ?
1389 &ctx->d_frame : &ctx->s_frame;
1391 mutex_lock(&fimc->lock);
1392 fimc_capture_try_crop(ctx, r, crop->pad);
1394 if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
1395 mutex_unlock(&fimc->lock);
1396 *v4l2_subdev_get_try_crop(fh, crop->pad) = *r;
1399 spin_lock_irqsave(&fimc->slock, flags);
1400 set_frame_crop(ff, r->left, r->top, r->width, r->height);
1401 if (crop->pad == FIMC_SD_PAD_SOURCE)
1402 ctx->state |= FIMC_DST_CROP;
1404 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1405 spin_unlock_irqrestore(&fimc->slock, flags);
1407 dbg("pad%d: (%d,%d)/%dx%d", crop->pad, r->left, r->top,
1408 r->width, r->height);
1410 mutex_unlock(&fimc->lock);
1414 static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1415 .enum_mbus_code = fimc_subdev_enum_mbus_code,
1416 .get_fmt = fimc_subdev_get_fmt,
1417 .set_fmt = fimc_subdev_set_fmt,
1418 .get_crop = fimc_subdev_get_crop,
1419 .set_crop = fimc_subdev_set_crop,
1422 static struct v4l2_subdev_ops fimc_subdev_ops = {
1423 .pad = &fimc_subdev_pad_ops,
1426 static int fimc_create_capture_subdev(struct fimc_dev *fimc,
1427 struct v4l2_device *v4l2_dev)
1429 struct v4l2_subdev *sd;
1432 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
1436 v4l2_subdev_init(sd, &fimc_subdev_ops);
1437 sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
1438 snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->pdev->id);
1440 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1441 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1442 ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
1443 fimc->vid_cap.sd_pads, 0);
1446 ret = v4l2_device_register_subdev(v4l2_dev, sd);
1450 fimc->vid_cap.subdev = sd;
1451 v4l2_set_subdevdata(sd, fimc);
1452 sd->entity.ops = &fimc_sd_media_ops;
1455 media_entity_cleanup(&sd->entity);
1461 static void fimc_destroy_capture_subdev(struct fimc_dev *fimc)
1463 struct v4l2_subdev *sd = fimc->vid_cap.subdev;
1467 media_entity_cleanup(&sd->entity);
1468 v4l2_device_unregister_subdev(sd);
1470 fimc->vid_cap.subdev = NULL;
1473 /* Set default format at the sensor and host interface */
1474 static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1476 struct v4l2_format fmt = {
1477 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1481 .pixelformat = V4L2_PIX_FMT_YUYV,
1482 .field = V4L2_FIELD_NONE,
1483 .colorspace = V4L2_COLORSPACE_JPEG,
1487 return fimc_capture_set_format(fimc, &fmt);
1490 /* fimc->lock must be already initialized */
1491 int fimc_register_capture_device(struct fimc_dev *fimc,
1492 struct v4l2_device *v4l2_dev)
1494 struct video_device *vfd;
1495 struct fimc_vid_cap *vid_cap;
1496 struct fimc_ctx *ctx;
1497 struct vb2_queue *q;
1500 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1504 ctx->fimc_dev = fimc;
1505 ctx->in_path = FIMC_CAMERA;
1506 ctx->out_path = FIMC_DMA;
1507 ctx->state = FIMC_CTX_CAP;
1508 ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1509 ctx->d_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1511 vfd = video_device_alloc();
1513 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
1517 snprintf(vfd->name, sizeof(vfd->name), "%s.capture",
1518 dev_name(&fimc->pdev->dev));
1520 vfd->fops = &fimc_capture_fops;
1521 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
1522 vfd->v4l2_dev = v4l2_dev;
1524 vfd->release = video_device_release;
1525 vfd->lock = &fimc->lock;
1526 video_set_drvdata(vfd, fimc);
1528 vid_cap = &fimc->vid_cap;
1530 vid_cap->active_buf_cnt = 0;
1531 vid_cap->reqbufs_count = 0;
1532 vid_cap->refcnt = 0;
1534 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1535 INIT_LIST_HEAD(&vid_cap->active_buf_q);
1536 spin_lock_init(&ctx->slock);
1539 q = &fimc->vid_cap.vbq;
1540 memset(q, 0, sizeof(*q));
1541 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1542 q->io_modes = VB2_MMAP | VB2_USERPTR;
1543 q->drv_priv = fimc->vid_cap.ctx;
1544 q->ops = &fimc_capture_qops;
1545 q->mem_ops = &vb2_dma_contig_memops;
1546 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1550 fimc->vid_cap.vd_pad.flags = MEDIA_PAD_FL_SINK;
1551 ret = media_entity_init(&vfd->entity, 1, &fimc->vid_cap.vd_pad, 0);
1554 ret = fimc_create_capture_subdev(fimc, v4l2_dev);
1558 vfd->ctrl_handler = &ctx->ctrl_handler;
1562 media_entity_cleanup(&vfd->entity);
1564 video_device_release(vfd);
1570 void fimc_unregister_capture_device(struct fimc_dev *fimc)
1572 struct video_device *vfd = fimc->vid_cap.vfd;
1575 media_entity_cleanup(&vfd->entity);
1576 /* Can also be called if video device was
1578 video_unregister_device(vfd);
1580 fimc_destroy_capture_subdev(fimc);
1581 kfree(fimc->vid_cap.ctx);
1582 fimc->vid_cap.ctx = NULL;