2 * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver
4 * Copyright (C) 2010 - 2012 Samsung Electronics Co., Ltd.
5 * 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>
31 #include "fimc-core.h"
33 #include "media-dev.h"
35 static int fimc_capture_hw_init(struct fimc_dev *fimc)
37 struct fimc_source_info *si = &fimc->vid_cap.source_config;
38 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
42 if (ctx == NULL || ctx->s_frame.fmt == NULL)
45 if (si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK) {
46 ret = fimc_hw_camblk_cfg_writeback(fimc);
51 spin_lock_irqsave(&fimc->slock, flags);
52 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
53 fimc_set_yuv_order(ctx);
55 fimc_hw_set_camera_polarity(fimc, si);
56 fimc_hw_set_camera_type(fimc, si);
57 fimc_hw_set_camera_source(fimc, si);
58 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
60 ret = fimc_set_scaler_info(ctx);
62 fimc_hw_set_input_path(ctx);
63 fimc_hw_set_prescaler(ctx);
64 fimc_hw_set_mainscaler(ctx);
65 fimc_hw_set_target_format(ctx);
66 fimc_hw_set_rotation(ctx);
67 fimc_hw_set_effect(ctx);
68 fimc_hw_set_output_path(ctx);
69 fimc_hw_set_out_dma(ctx);
70 if (fimc->drv_data->alpha_color)
71 fimc_hw_set_rgb_alpha(ctx);
72 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
74 spin_unlock_irqrestore(&fimc->slock, flags);
79 * Reinitialize the driver so it is ready to start the streaming again.
80 * Set fimc->state to indicate stream off and the hardware shut down state.
81 * If not suspending (@suspend is false), return any buffers to videobuf2.
82 * Otherwise put any owned buffers onto the pending buffers queue, so they
83 * can be re-spun when the device is being resumed. Also perform FIMC
84 * software reset and disable streaming on the whole pipeline if required.
86 static int fimc_capture_state_cleanup(struct fimc_dev *fimc, bool suspend)
88 struct fimc_vid_cap *cap = &fimc->vid_cap;
89 struct fimc_vid_buffer *buf;
93 spin_lock_irqsave(&fimc->slock, flags);
94 streaming = fimc->state & (1 << ST_CAPT_ISP_STREAM);
96 fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_SHUT |
97 1 << ST_CAPT_STREAM | 1 << ST_CAPT_ISP_STREAM);
99 fimc->state |= (1 << ST_CAPT_SUSPENDED);
101 fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED);
103 /* Release unused buffers */
104 while (!suspend && !list_empty(&cap->pending_buf_q)) {
105 buf = fimc_pending_queue_pop(cap);
106 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
108 /* If suspending put unused buffers onto pending queue */
109 while (!list_empty(&cap->active_buf_q)) {
110 buf = fimc_active_queue_pop(cap);
112 fimc_pending_queue_add(cap, buf);
114 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
120 spin_unlock_irqrestore(&fimc->slock, flags);
123 return fimc_pipeline_call(fimc, set_stream,
129 static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend)
133 if (!fimc_capture_active(fimc))
136 spin_lock_irqsave(&fimc->slock, flags);
137 set_bit(ST_CAPT_SHUT, &fimc->state);
138 fimc_deactivate_capture(fimc);
139 spin_unlock_irqrestore(&fimc->slock, flags);
141 wait_event_timeout(fimc->irq_queue,
142 !test_bit(ST_CAPT_SHUT, &fimc->state),
143 (2*HZ/10)); /* 200 ms */
145 return fimc_capture_state_cleanup(fimc, suspend);
149 * fimc_capture_config_update - apply the camera interface configuration
151 * To be called from within the interrupt handler with fimc.slock
152 * spinlock held. It updates the camera pixel crop, rotation and
155 static int fimc_capture_config_update(struct fimc_ctx *ctx)
157 struct fimc_dev *fimc = ctx->fimc_dev;
160 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
162 ret = fimc_set_scaler_info(ctx);
166 fimc_hw_set_prescaler(ctx);
167 fimc_hw_set_mainscaler(ctx);
168 fimc_hw_set_target_format(ctx);
169 fimc_hw_set_rotation(ctx);
170 fimc_hw_set_effect(ctx);
171 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
172 fimc_hw_set_out_dma(ctx);
173 if (fimc->drv_data->alpha_color)
174 fimc_hw_set_rgb_alpha(ctx);
176 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
180 void fimc_capture_irq_handler(struct fimc_dev *fimc, int deq_buf)
182 struct v4l2_subdev *csis = fimc->pipeline.subdevs[IDX_CSIS];
183 struct fimc_vid_cap *cap = &fimc->vid_cap;
184 struct fimc_frame *f = &cap->ctx->d_frame;
185 struct fimc_vid_buffer *v_buf;
189 if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
190 wake_up(&fimc->irq_queue);
194 if (!list_empty(&cap->active_buf_q) &&
195 test_bit(ST_CAPT_RUN, &fimc->state) && deq_buf) {
196 ktime_get_real_ts(&ts);
198 v_buf = fimc_active_queue_pop(cap);
200 tv = &v_buf->vb.v4l2_buf.timestamp;
201 tv->tv_sec = ts.tv_sec;
202 tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
203 v_buf->vb.v4l2_buf.sequence = cap->frame_count++;
205 vb2_buffer_done(&v_buf->vb, VB2_BUF_STATE_DONE);
208 if (!list_empty(&cap->pending_buf_q)) {
210 v_buf = fimc_pending_queue_pop(cap);
211 fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
212 v_buf->index = cap->buf_index;
214 /* Move the buffer to the capture active queue */
215 fimc_active_queue_add(cap, v_buf);
217 dbg("next frame: %d, done frame: %d",
218 fimc_hw_get_frame_index(fimc), v_buf->index);
220 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
224 * Set up a buffer at MIPI-CSIS if current image format
225 * requires the frame embedded data capture.
227 if (f->fmt->mdataplanes && !list_empty(&cap->active_buf_q)) {
228 unsigned int plane = ffs(f->fmt->mdataplanes) - 1;
229 unsigned int size = f->payload[plane];
230 s32 index = fimc_hw_get_frame_index(fimc);
233 list_for_each_entry(v_buf, &cap->active_buf_q, list) {
234 if (v_buf->index != index)
236 vaddr = vb2_plane_vaddr(&v_buf->vb, plane);
237 v4l2_subdev_call(csis, video, s_rx_buffer,
243 if (cap->active_buf_cnt == 0) {
245 clear_bit(ST_CAPT_RUN, &fimc->state);
247 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
250 set_bit(ST_CAPT_RUN, &fimc->state);
253 if (test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
254 fimc_capture_config_update(cap->ctx);
256 if (cap->active_buf_cnt == 1) {
257 fimc_deactivate_capture(fimc);
258 clear_bit(ST_CAPT_STREAM, &fimc->state);
261 dbg("frame: %d, active_buf_cnt: %d",
262 fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
266 static int start_streaming(struct vb2_queue *q, unsigned int count)
268 struct fimc_ctx *ctx = q->drv_priv;
269 struct fimc_dev *fimc = ctx->fimc_dev;
270 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
274 vid_cap->frame_count = 0;
276 ret = fimc_capture_hw_init(fimc);
278 fimc_capture_state_cleanup(fimc, false);
282 set_bit(ST_CAPT_PEND, &fimc->state);
284 min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
286 if (vid_cap->active_buf_cnt >= min_bufs &&
287 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
288 fimc_activate_capture(ctx);
290 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
291 return fimc_pipeline_call(fimc, set_stream,
298 static int stop_streaming(struct vb2_queue *q)
300 struct fimc_ctx *ctx = q->drv_priv;
301 struct fimc_dev *fimc = ctx->fimc_dev;
303 if (!fimc_capture_active(fimc))
306 return fimc_stop_capture(fimc, false);
309 int fimc_capture_suspend(struct fimc_dev *fimc)
311 bool suspend = fimc_capture_busy(fimc);
313 int ret = fimc_stop_capture(fimc, suspend);
316 return fimc_pipeline_call(fimc, close, &fimc->pipeline);
319 static void buffer_queue(struct vb2_buffer *vb);
321 int fimc_capture_resume(struct fimc_dev *fimc)
323 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
324 struct exynos_video_entity *ve = &vid_cap->ve;
325 struct fimc_vid_buffer *buf;
328 if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
331 INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
332 vid_cap->buf_index = 0;
333 fimc_pipeline_call(fimc, open, &fimc->pipeline,
334 &ve->vdev.entity, false);
335 fimc_capture_hw_init(fimc);
337 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
339 for (i = 0; i < vid_cap->reqbufs_count; i++) {
340 if (list_empty(&vid_cap->pending_buf_q))
342 buf = fimc_pending_queue_pop(vid_cap);
343 buffer_queue(&buf->vb);
349 static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
350 unsigned int *num_buffers, unsigned int *num_planes,
351 unsigned int sizes[], void *allocators[])
353 const struct v4l2_pix_format_mplane *pixm = NULL;
354 struct fimc_ctx *ctx = vq->drv_priv;
355 struct fimc_frame *frame = &ctx->d_frame;
356 struct fimc_fmt *fmt = frame->fmt;
361 pixm = &pfmt->fmt.pix_mp;
362 fmt = fimc_find_format(&pixm->pixelformat, NULL,
363 FMT_FLAGS_CAM | FMT_FLAGS_M2M, -1);
364 wh = pixm->width * pixm->height;
366 wh = frame->f_width * frame->f_height;
372 *num_planes = fmt->memplanes;
374 for (i = 0; i < fmt->memplanes; i++) {
375 unsigned int size = (wh * fmt->depth[i]) / 8;
377 sizes[i] = max(size, pixm->plane_fmt[i].sizeimage);
378 else if (fimc_fmt_is_user_defined(fmt->color))
379 sizes[i] = frame->payload[i];
381 sizes[i] = max_t(u32, size, frame->payload[i]);
383 allocators[i] = ctx->fimc_dev->alloc_ctx;
389 static int buffer_prepare(struct vb2_buffer *vb)
391 struct vb2_queue *vq = vb->vb2_queue;
392 struct fimc_ctx *ctx = vq->drv_priv;
395 if (ctx->d_frame.fmt == NULL)
398 for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
399 unsigned long size = ctx->d_frame.payload[i];
401 if (vb2_plane_size(vb, i) < size) {
402 v4l2_err(&ctx->fimc_dev->vid_cap.ve.vdev,
403 "User buffer too small (%ld < %ld)\n",
404 vb2_plane_size(vb, i), size);
407 vb2_set_plane_payload(vb, i, size);
413 static void buffer_queue(struct vb2_buffer *vb)
415 struct fimc_vid_buffer *buf
416 = container_of(vb, struct fimc_vid_buffer, vb);
417 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
418 struct fimc_dev *fimc = ctx->fimc_dev;
419 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
420 struct exynos_video_entity *ve = &vid_cap->ve;
424 spin_lock_irqsave(&fimc->slock, flags);
425 fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
427 if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
428 !test_bit(ST_CAPT_STREAM, &fimc->state) &&
429 vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
430 /* Setup the buffer directly for processing. */
431 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
434 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
435 buf->index = vid_cap->buf_index;
436 fimc_active_queue_add(vid_cap, buf);
438 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
439 vid_cap->buf_index = 0;
441 fimc_pending_queue_add(vid_cap, buf);
444 min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
447 if (vb2_is_streaming(&vid_cap->vbq) &&
448 vid_cap->active_buf_cnt >= min_bufs &&
449 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
452 fimc_activate_capture(ctx);
453 spin_unlock_irqrestore(&fimc->slock, flags);
455 if (test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
458 ret = fimc_pipeline_call(fimc, set_stream, &fimc->pipeline, 1);
460 v4l2_err(&ve->vdev, "stream on failed: %d\n", ret);
463 spin_unlock_irqrestore(&fimc->slock, flags);
466 static struct vb2_ops fimc_capture_qops = {
467 .queue_setup = queue_setup,
468 .buf_prepare = buffer_prepare,
469 .buf_queue = buffer_queue,
470 .wait_prepare = vb2_ops_wait_prepare,
471 .wait_finish = vb2_ops_wait_finish,
472 .start_streaming = start_streaming,
473 .stop_streaming = stop_streaming,
476 static int fimc_capture_set_default_format(struct fimc_dev *fimc);
478 static int fimc_capture_open(struct file *file)
480 struct fimc_dev *fimc = video_drvdata(file);
481 struct fimc_vid_cap *vc = &fimc->vid_cap;
482 struct exynos_video_entity *ve = &vc->ve;
485 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
487 mutex_lock(&fimc->lock);
489 if (fimc_m2m_active(fimc))
492 set_bit(ST_CAPT_BUSY, &fimc->state);
493 ret = pm_runtime_get_sync(&fimc->pdev->dev);
497 ret = v4l2_fh_open(file);
499 pm_runtime_put_sync(&fimc->pdev->dev);
503 if (v4l2_fh_is_singular_file(file)) {
504 fimc_md_graph_lock(ve);
506 ret = fimc_pipeline_call(fimc, open, &fimc->pipeline,
507 &fimc->vid_cap.ve.vdev.entity, true);
509 ret = fimc_capture_set_default_format(fimc);
511 if (ret == 0 && vc->user_subdev_api && vc->inh_sensor_ctrls) {
513 * Recreate controls of the the video node to drop
514 * any controls inherited from the sensor subdev.
516 fimc_ctrls_delete(vc->ctx);
518 ret = fimc_ctrls_create(vc->ctx);
520 vc->inh_sensor_ctrls = false;
523 ve->vdev.entity.use_count++;
525 fimc_md_graph_unlock(ve);
528 clear_bit(ST_CAPT_BUSY, &fimc->state);
529 pm_runtime_put_sync(&fimc->pdev->dev);
530 v4l2_fh_release(file);
534 mutex_unlock(&fimc->lock);
538 static int fimc_capture_release(struct file *file)
540 struct fimc_dev *fimc = video_drvdata(file);
541 struct fimc_vid_cap *vc = &fimc->vid_cap;
542 bool close = v4l2_fh_is_singular_file(file);
545 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
547 mutex_lock(&fimc->lock);
549 if (close && vc->streaming) {
550 media_entity_pipeline_stop(&vc->ve.vdev.entity);
551 vc->streaming = false;
554 ret = vb2_fop_release(file);
557 clear_bit(ST_CAPT_BUSY, &fimc->state);
558 fimc_stop_capture(fimc, false);
559 fimc_pipeline_call(fimc, close, &fimc->pipeline);
560 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
562 fimc_md_graph_lock(&vc->ve);
563 vc->ve.vdev.entity.use_count--;
564 fimc_md_graph_unlock(&vc->ve);
567 pm_runtime_put_sync(&fimc->pdev->dev);
568 mutex_unlock(&fimc->lock);
573 static const struct v4l2_file_operations fimc_capture_fops = {
574 .owner = THIS_MODULE,
575 .open = fimc_capture_open,
576 .release = fimc_capture_release,
577 .poll = vb2_fop_poll,
578 .unlocked_ioctl = video_ioctl2,
579 .mmap = vb2_fop_mmap,
583 * Format and crop negotiation helpers
586 static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
587 u32 *width, u32 *height,
588 u32 *code, u32 *fourcc, int pad)
590 bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
591 struct fimc_dev *fimc = ctx->fimc_dev;
592 const struct fimc_variant *var = fimc->variant;
593 const struct fimc_pix_limit *pl = var->pix_limit;
594 struct fimc_frame *dst = &ctx->d_frame;
595 u32 depth, min_w, max_w, min_h, align_h = 3;
596 u32 mask = FMT_FLAGS_CAM;
597 struct fimc_fmt *ffmt;
599 /* Conversion from/to JPEG or User Defined format is not supported */
600 if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
601 fimc_fmt_is_user_defined(ctx->s_frame.fmt->color))
602 *code = ctx->s_frame.fmt->mbus_code;
604 if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad == FIMC_SD_PAD_SOURCE)
605 mask |= FMT_FLAGS_M2M;
607 if (pad == FIMC_SD_PAD_SINK_FIFO)
608 mask = FMT_FLAGS_WRITEBACK;
610 ffmt = fimc_find_format(fourcc, code, mask, 0);
615 *code = ffmt->mbus_code;
617 *fourcc = ffmt->fourcc;
619 if (pad != FIMC_SD_PAD_SOURCE) {
620 max_w = fimc_fmt_is_user_defined(ffmt->color) ?
621 pl->scaler_dis_w : pl->scaler_en_w;
622 /* Apply the camera input interface pixel constraints */
623 v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
624 height, max_t(u32, *height, 32),
625 FIMC_CAMIF_MAX_HEIGHT,
626 fimc_fmt_is_user_defined(ffmt->color) ?
631 /* Can't scale or crop in transparent (JPEG) transfer mode */
632 if (fimc_fmt_is_user_defined(ffmt->color)) {
633 *width = ctx->s_frame.f_width;
634 *height = ctx->s_frame.f_height;
637 /* Apply the scaler and the output DMA constraints */
638 max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
639 if (ctx->state & FIMC_COMPOSE) {
640 min_w = dst->offs_h + dst->width;
641 min_h = dst->offs_v + dst->height;
643 min_w = var->min_out_pixsize;
644 min_h = var->min_out_pixsize;
646 if (var->min_vsize_align == 1 && !rotation)
647 align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
649 depth = fimc_get_format_depth(ffmt);
650 v4l_bound_align_image(width, min_w, max_w,
651 ffs(var->min_out_pixsize) - 1,
652 height, min_h, FIMC_CAMIF_MAX_HEIGHT,
654 64/(ALIGN(depth, 8)));
656 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
657 pad, code ? *code : 0, *width, *height,
658 dst->f_width, dst->f_height);
663 static void fimc_capture_try_selection(struct fimc_ctx *ctx,
667 bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
668 struct fimc_dev *fimc = ctx->fimc_dev;
669 const struct fimc_variant *var = fimc->variant;
670 const struct fimc_pix_limit *pl = var->pix_limit;
671 struct fimc_frame *sink = &ctx->s_frame;
672 u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
673 u32 align_sz = 0, align_h = 4;
674 u32 max_sc_h, max_sc_v;
676 /* In JPEG transparent transfer mode cropping is not supported */
677 if (fimc_fmt_is_user_defined(ctx->d_frame.fmt->color)) {
678 r->width = sink->f_width;
679 r->height = sink->f_height;
680 r->left = r->top = 0;
683 if (target == V4L2_SEL_TGT_COMPOSE) {
684 if (ctx->rotation != 90 && ctx->rotation != 270)
686 max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3));
687 max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1));
688 min_sz = var->min_out_pixsize;
690 u32 depth = fimc_get_format_depth(sink->fmt);
691 align_sz = 64/ALIGN(depth, 8);
692 min_sz = var->min_inp_pixsize;
693 min_w = min_h = min_sz;
694 max_sc_h = max_sc_v = 1;
697 * For the compose rectangle the following constraints must be met:
698 * - it must fit in the sink pad format rectangle (f_width/f_height);
699 * - maximum downscaling ratio is 64;
700 * - maximum crop size depends if the rotator is used or not;
701 * - the sink pad format width/height must be 4 multiple of the
702 * prescaler ratios determined by sink pad size and source pad crop,
703 * the prescaler ratio is returned by fimc_get_scaler_factor().
706 rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
707 rotate ? sink->f_height : sink->f_width);
708 max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
710 if (target == V4L2_SEL_TGT_COMPOSE) {
711 min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
712 min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
714 swap(max_sc_h, max_sc_v);
718 v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
719 &r->height, min_h, max_h, align_h,
721 /* Adjust left/top if crop/compose rectangle is out of bounds */
722 r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
723 r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height);
724 r->left = round_down(r->left, var->hor_offs_align);
726 dbg("target %#x: (%d,%d)/%dx%d, sink fmt: %dx%d",
727 target, r->left, r->top, r->width, r->height,
728 sink->f_width, sink->f_height);
732 * The video node ioctl operations
734 static int fimc_cap_querycap(struct file *file, void *priv,
735 struct v4l2_capability *cap)
737 struct fimc_dev *fimc = video_drvdata(file);
739 __fimc_vidioc_querycap(&fimc->pdev->dev, cap, V4L2_CAP_STREAMING |
740 V4L2_CAP_VIDEO_CAPTURE_MPLANE);
744 static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
745 struct v4l2_fmtdesc *f)
747 struct fimc_fmt *fmt;
749 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
753 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
754 f->pixelformat = fmt->fourcc;
755 if (fmt->fourcc == V4L2_MBUS_FMT_JPEG_1X8)
756 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
760 static struct media_entity *fimc_pipeline_get_head(struct media_entity *me)
762 struct media_pad *pad = &me->pads[0];
764 while (!(pad->flags & MEDIA_PAD_FL_SOURCE)) {
765 pad = media_entity_remote_pad(pad);
776 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
778 * @ctx: FIMC capture context
779 * @tfmt: media bus format to try/set on subdevs
780 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
781 * @set: true to set format on subdevs, false to try only
783 static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
784 struct v4l2_mbus_framefmt *tfmt,
785 struct fimc_fmt **fmt_id,
788 struct fimc_dev *fimc = ctx->fimc_dev;
789 struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
790 struct v4l2_subdev_format sfmt;
791 struct v4l2_mbus_framefmt *mf = &sfmt.format;
792 struct media_entity *me;
793 struct fimc_fmt *ffmt;
794 struct media_pad *pad;
798 if (WARN_ON(!sd || !tfmt))
801 memset(&sfmt, 0, sizeof(sfmt));
803 sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
805 me = fimc_pipeline_get_head(&sd->entity);
808 ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
812 * Notify user-space if common pixel code for
813 * host and sensor does not exist.
817 mf->code = tfmt->code = ffmt->mbus_code;
819 /* set format on all pipeline subdevs */
820 while (me != &fimc->vid_cap.subdev.entity) {
821 sd = media_entity_to_v4l2_subdev(me);
824 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
828 if (me->pads[0].flags & MEDIA_PAD_FL_SINK) {
829 sfmt.pad = me->num_pads - 1;
830 mf->code = tfmt->code;
831 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL,
837 pad = media_entity_remote_pad(&me->pads[sfmt.pad]);
843 if (mf->code != tfmt->code)
847 tfmt->width = mf->width;
848 tfmt->height = mf->height;
849 ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
850 NULL, &fcc, FIMC_SD_PAD_SINK_CAM);
851 ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
852 NULL, &fcc, FIMC_SD_PAD_SOURCE);
853 if (ffmt && ffmt->mbus_code)
854 mf->code = ffmt->mbus_code;
855 if (mf->width != tfmt->width || mf->height != tfmt->height)
857 tfmt->code = mf->code;
869 * fimc_get_sensor_frame_desc - query the sensor for media bus frame parameters
870 * @sensor: pointer to the sensor subdev
871 * @plane_fmt: provides plane sizes corresponding to the frame layout entries
872 * @try: true to set the frame parameters, false to query only
874 * This function is used by this driver only for compressed/blob data formats.
876 static int fimc_get_sensor_frame_desc(struct v4l2_subdev *sensor,
877 struct v4l2_plane_pix_format *plane_fmt,
878 unsigned int num_planes, bool try)
880 struct v4l2_mbus_frame_desc fd;
884 for (i = 0; i < num_planes; i++)
885 fd.entry[i].length = plane_fmt[i].sizeimage;
887 pad = sensor->entity.num_pads - 1;
889 ret = v4l2_subdev_call(sensor, pad, set_frame_desc, pad, &fd);
891 ret = v4l2_subdev_call(sensor, pad, get_frame_desc, pad, &fd);
896 if (num_planes != fd.num_entries)
899 for (i = 0; i < num_planes; i++)
900 plane_fmt[i].sizeimage = fd.entry[i].length;
902 if (fd.entry[0].length > FIMC_MAX_JPEG_BUF_SIZE) {
903 v4l2_err(sensor->v4l2_dev, "Unsupported buffer size: %u\n",
912 static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
913 struct v4l2_format *f)
915 struct fimc_dev *fimc = video_drvdata(file);
917 __fimc_get_format(&fimc->vid_cap.ctx->d_frame, f);
921 static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
922 struct v4l2_format *f)
924 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
925 struct fimc_dev *fimc = video_drvdata(file);
926 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
927 struct exynos_video_entity *ve = &fimc->vid_cap.ve;
928 struct v4l2_mbus_framefmt mf;
929 struct fimc_fmt *ffmt = NULL;
932 if (fimc_jpeg_fourcc(pix->pixelformat)) {
933 fimc_capture_try_format(ctx, &pix->width, &pix->height,
934 NULL, &pix->pixelformat,
935 FIMC_SD_PAD_SINK_CAM);
936 ctx->s_frame.f_width = pix->width;
937 ctx->s_frame.f_height = pix->height;
939 ffmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
940 NULL, &pix->pixelformat,
945 if (!fimc->vid_cap.user_subdev_api) {
946 mf.width = pix->width;
947 mf.height = pix->height;
948 mf.code = ffmt->mbus_code;
950 fimc_md_graph_lock(ve);
951 fimc_pipeline_try_format(ctx, &mf, &ffmt, false);
952 fimc_md_graph_unlock(ve);
954 pix->width = mf.width;
955 pix->height = mf.height;
957 pix->pixelformat = ffmt->fourcc;
960 fimc_adjust_mplane_format(ffmt, pix->width, pix->height, pix);
962 if (ffmt->flags & FMT_FLAGS_COMPRESSED)
963 fimc_get_sensor_frame_desc(fimc->pipeline.subdevs[IDX_SENSOR],
964 pix->plane_fmt, ffmt->memplanes, true);
969 static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx,
970 enum fimc_color_fmt color)
972 bool jpeg = fimc_fmt_is_user_defined(color);
974 ctx->scaler.enabled = !jpeg;
975 fimc_ctrls_activate(ctx, !jpeg);
978 set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
980 clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
983 static int __fimc_capture_set_format(struct fimc_dev *fimc,
984 struct v4l2_format *f)
986 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
987 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
988 struct v4l2_mbus_framefmt *mf = &fimc->vid_cap.ci_fmt;
989 struct fimc_frame *ff = &ctx->d_frame;
990 struct fimc_fmt *s_fmt = NULL;
993 if (vb2_is_busy(&fimc->vid_cap.vbq))
996 /* Pre-configure format at camera interface input, for JPEG only */
997 if (fimc_jpeg_fourcc(pix->pixelformat)) {
998 fimc_capture_try_format(ctx, &pix->width, &pix->height,
999 NULL, &pix->pixelformat,
1000 FIMC_SD_PAD_SINK_CAM);
1001 ctx->s_frame.f_width = pix->width;
1002 ctx->s_frame.f_height = pix->height;
1004 /* Try the format at the scaler and the DMA output */
1005 ff->fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
1006 NULL, &pix->pixelformat,
1007 FIMC_SD_PAD_SOURCE);
1011 /* Update RGB Alpha control state and value range */
1012 fimc_alpha_ctrl_update(ctx);
1014 /* Try to match format at the host and the sensor */
1015 if (!fimc->vid_cap.user_subdev_api) {
1016 mf->code = ff->fmt->mbus_code;
1017 mf->width = pix->width;
1018 mf->height = pix->height;
1019 ret = fimc_pipeline_try_format(ctx, mf, &s_fmt, true);
1023 pix->width = mf->width;
1024 pix->height = mf->height;
1027 fimc_adjust_mplane_format(ff->fmt, pix->width, pix->height, pix);
1029 if (ff->fmt->flags & FMT_FLAGS_COMPRESSED) {
1030 ret = fimc_get_sensor_frame_desc(fimc->pipeline.subdevs[IDX_SENSOR],
1031 pix->plane_fmt, ff->fmt->memplanes,
1037 for (i = 0; i < ff->fmt->memplanes; i++) {
1038 ff->bytesperline[i] = pix->plane_fmt[i].bytesperline;
1039 ff->payload[i] = pix->plane_fmt[i].sizeimage;
1042 set_frame_bounds(ff, pix->width, pix->height);
1043 /* Reset the composition rectangle if not yet configured */
1044 if (!(ctx->state & FIMC_COMPOSE))
1045 set_frame_crop(ff, 0, 0, pix->width, pix->height);
1047 fimc_capture_mark_jpeg_xfer(ctx, ff->fmt->color);
1049 /* Reset cropping and set format at the camera interface input */
1050 if (!fimc->vid_cap.user_subdev_api) {
1051 ctx->s_frame.fmt = s_fmt;
1052 set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
1053 set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
1059 static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
1060 struct v4l2_format *f)
1062 struct fimc_dev *fimc = video_drvdata(file);
1065 fimc_md_graph_lock(&fimc->vid_cap.ve);
1067 * The graph is walked within __fimc_capture_set_format() to set
1068 * the format at subdevs thus the graph mutex needs to be held at
1071 ret = __fimc_capture_set_format(fimc, f);
1073 fimc_md_graph_unlock(&fimc->vid_cap.ve);
1077 static int fimc_cap_enum_input(struct file *file, void *priv,
1078 struct v4l2_input *i)
1080 struct fimc_dev *fimc = video_drvdata(file);
1081 struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
1086 i->type = V4L2_INPUT_TYPE_CAMERA;
1088 strlcpy(i->name, sd->name, sizeof(i->name));
1092 static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
1094 return i == 0 ? i : -EINVAL;
1097 static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
1104 * fimc_pipeline_validate - check for formats inconsistencies
1105 * between source and sink pad of each link
1107 * Return 0 if all formats match or -EPIPE otherwise.
1109 static int fimc_pipeline_validate(struct fimc_dev *fimc)
1111 struct v4l2_subdev_format sink_fmt, src_fmt;
1112 struct fimc_vid_cap *vc = &fimc->vid_cap;
1113 struct v4l2_subdev *sd = &vc->subdev;
1114 struct media_pad *sink_pad, *src_pad;
1119 * Find current entity sink pad and any remote sink pad linked
1120 * to it. We stop if there is no sink pad in current entity or
1121 * it is not linked to any other remote entity.
1125 for (i = 0; i < sd->entity.num_pads; i++) {
1126 struct media_pad *p = &sd->entity.pads[i];
1128 if (p->flags & MEDIA_PAD_FL_SINK) {
1130 src_pad = media_entity_remote_pad(sink_pad);
1136 if (src_pad == NULL ||
1137 media_entity_type(src_pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1140 /* Don't call FIMC subdev operation to avoid nested locking */
1141 if (sd == &vc->subdev) {
1142 struct fimc_frame *ff = &vc->ctx->s_frame;
1143 sink_fmt.format.width = ff->f_width;
1144 sink_fmt.format.height = ff->f_height;
1145 sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
1147 sink_fmt.pad = sink_pad->index;
1148 sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1149 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
1150 if (ret < 0 && ret != -ENOIOCTLCMD)
1154 /* Retrieve format at the source pad */
1155 sd = media_entity_to_v4l2_subdev(src_pad->entity);
1156 src_fmt.pad = src_pad->index;
1157 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1158 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
1159 if (ret < 0 && ret != -ENOIOCTLCMD)
1162 if (src_fmt.format.width != sink_fmt.format.width ||
1163 src_fmt.format.height != sink_fmt.format.height ||
1164 src_fmt.format.code != sink_fmt.format.code)
1167 if (sd == fimc->pipeline.subdevs[IDX_SENSOR] &&
1168 fimc_user_defined_mbus_fmt(src_fmt.format.code)) {
1169 struct v4l2_plane_pix_format plane_fmt[FIMC_MAX_PLANES];
1170 struct fimc_frame *frame = &vc->ctx->d_frame;
1173 ret = fimc_get_sensor_frame_desc(sd, plane_fmt,
1174 frame->fmt->memplanes,
1179 for (i = 0; i < frame->fmt->memplanes; i++)
1180 if (frame->payload[i] < plane_fmt[i].sizeimage)
1187 static int fimc_cap_streamon(struct file *file, void *priv,
1188 enum v4l2_buf_type type)
1190 struct fimc_dev *fimc = video_drvdata(file);
1191 struct fimc_pipeline *p = &fimc->pipeline;
1192 struct fimc_vid_cap *vc = &fimc->vid_cap;
1193 struct media_entity *entity = &vc->ve.vdev.entity;
1194 struct fimc_source_info *si = NULL;
1195 struct v4l2_subdev *sd;
1198 if (fimc_capture_active(fimc))
1201 ret = media_entity_pipeline_start(entity, p->m_pipeline);
1205 sd = p->subdevs[IDX_SENSOR];
1207 si = v4l2_get_subdev_hostdata(sd);
1214 * Save configuration data related to currently attached image
1215 * sensor or other data source, e.g. FIMC-IS.
1217 vc->source_config = *si;
1219 if (vc->input == GRP_ID_FIMC_IS)
1220 vc->source_config.fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
1222 if (vc->user_subdev_api) {
1223 ret = fimc_pipeline_validate(fimc);
1228 ret = vb2_ioctl_streamon(file, priv, type);
1230 vc->streaming = true;
1235 media_entity_pipeline_stop(entity);
1239 static int fimc_cap_streamoff(struct file *file, void *priv,
1240 enum v4l2_buf_type type)
1242 struct fimc_dev *fimc = video_drvdata(file);
1243 struct fimc_vid_cap *vc = &fimc->vid_cap;
1246 ret = vb2_ioctl_streamoff(file, priv, type);
1250 media_entity_pipeline_stop(&vc->ve.vdev.entity);
1251 vc->streaming = false;
1255 static int fimc_cap_reqbufs(struct file *file, void *priv,
1256 struct v4l2_requestbuffers *reqbufs)
1258 struct fimc_dev *fimc = video_drvdata(file);
1261 ret = vb2_ioctl_reqbufs(file, priv, reqbufs);
1264 fimc->vid_cap.reqbufs_count = reqbufs->count;
1269 static int fimc_cap_g_selection(struct file *file, void *fh,
1270 struct v4l2_selection *s)
1272 struct fimc_dev *fimc = video_drvdata(file);
1273 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1274 struct fimc_frame *f = &ctx->s_frame;
1276 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1279 switch (s->target) {
1280 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1281 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1283 case V4L2_SEL_TGT_CROP_BOUNDS:
1284 case V4L2_SEL_TGT_CROP_DEFAULT:
1287 s->r.width = f->o_width;
1288 s->r.height = f->o_height;
1291 case V4L2_SEL_TGT_COMPOSE:
1293 case V4L2_SEL_TGT_CROP:
1294 s->r.left = f->offs_h;
1295 s->r.top = f->offs_v;
1296 s->r.width = f->width;
1297 s->r.height = f->height;
1304 /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
1305 static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
1307 if (a->left < b->left || a->top < b->top)
1309 if (a->left + a->width > b->left + b->width)
1311 if (a->top + a->height > b->top + b->height)
1317 static int fimc_cap_s_selection(struct file *file, void *fh,
1318 struct v4l2_selection *s)
1320 struct fimc_dev *fimc = video_drvdata(file);
1321 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1322 struct v4l2_rect rect = s->r;
1323 struct fimc_frame *f;
1324 unsigned long flags;
1326 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1329 if (s->target == V4L2_SEL_TGT_COMPOSE)
1331 else if (s->target == V4L2_SEL_TGT_CROP)
1336 fimc_capture_try_selection(ctx, &rect, s->target);
1338 if (s->flags & V4L2_SEL_FLAG_LE &&
1339 !enclosed_rectangle(&rect, &s->r))
1342 if (s->flags & V4L2_SEL_FLAG_GE &&
1343 !enclosed_rectangle(&s->r, &rect))
1347 spin_lock_irqsave(&fimc->slock, flags);
1348 set_frame_crop(f, s->r.left, s->r.top, s->r.width,
1350 spin_unlock_irqrestore(&fimc->slock, flags);
1352 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1356 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1357 .vidioc_querycap = fimc_cap_querycap,
1359 .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
1360 .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
1361 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
1362 .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
1364 .vidioc_reqbufs = fimc_cap_reqbufs,
1365 .vidioc_querybuf = vb2_ioctl_querybuf,
1366 .vidioc_qbuf = vb2_ioctl_qbuf,
1367 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1368 .vidioc_expbuf = vb2_ioctl_expbuf,
1369 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1370 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1372 .vidioc_streamon = fimc_cap_streamon,
1373 .vidioc_streamoff = fimc_cap_streamoff,
1375 .vidioc_g_selection = fimc_cap_g_selection,
1376 .vidioc_s_selection = fimc_cap_s_selection,
1378 .vidioc_enum_input = fimc_cap_enum_input,
1379 .vidioc_s_input = fimc_cap_s_input,
1380 .vidioc_g_input = fimc_cap_g_input,
1383 /* Capture subdev media entity operations */
1384 static int fimc_link_setup(struct media_entity *entity,
1385 const struct media_pad *local,
1386 const struct media_pad *remote, u32 flags)
1388 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1389 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1390 struct fimc_vid_cap *vc = &fimc->vid_cap;
1391 struct v4l2_subdev *sensor;
1393 if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1396 if (WARN_ON(fimc == NULL))
1399 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1400 local->entity->name, remote->entity->name, flags,
1401 fimc->vid_cap.input);
1403 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
1404 fimc->vid_cap.input = 0;
1411 vc->input = sd->grp_id;
1413 if (vc->user_subdev_api || vc->inh_sensor_ctrls)
1416 /* Inherit V4L2 controls from the image sensor subdev. */
1417 sensor = fimc_find_remote_sensor(&vc->subdev.entity);
1421 return v4l2_ctrl_add_handler(&vc->ctx->ctrls.handler,
1422 sensor->ctrl_handler, NULL);
1425 static const struct media_entity_operations fimc_sd_media_ops = {
1426 .link_setup = fimc_link_setup,
1430 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1431 * @sd: pointer to a subdev generating the notification
1432 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1433 * @arg: pointer to an u32 type integer that stores the frame payload value
1435 * The End Of Frame notification sent by sensor subdev in its still capture
1436 * mode. If there is only a single VSYNC generated by the sensor at the
1437 * beginning of a frame transmission, FIMC does not issue the LastIrq
1438 * (end of frame) interrupt. And this notification is used to complete the
1439 * frame capture and returning a buffer to user-space. Subdev drivers should
1440 * call this notification from their last 'End of frame capture' interrupt.
1442 void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1445 struct fimc_source_info *si;
1446 struct fimc_vid_buffer *buf;
1447 struct fimc_md *fmd;
1448 struct fimc_dev *fimc;
1449 unsigned long flags;
1454 si = v4l2_get_subdev_hostdata(sd);
1455 fmd = entity_to_fimc_mdev(&sd->entity);
1457 spin_lock_irqsave(&fmd->slock, flags);
1459 fimc = si ? source_to_sensor_info(si)->host : NULL;
1461 if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1462 test_bit(ST_CAPT_PEND, &fimc->state)) {
1463 unsigned long irq_flags;
1464 spin_lock_irqsave(&fimc->slock, irq_flags);
1465 if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1466 buf = list_entry(fimc->vid_cap.active_buf_q.next,
1467 struct fimc_vid_buffer, list);
1468 vb2_set_plane_payload(&buf->vb, 0, *((u32 *)arg));
1470 fimc_capture_irq_handler(fimc, 1);
1471 fimc_deactivate_capture(fimc);
1472 spin_unlock_irqrestore(&fimc->slock, irq_flags);
1474 spin_unlock_irqrestore(&fmd->slock, flags);
1477 static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1478 struct v4l2_subdev_fh *fh,
1479 struct v4l2_subdev_mbus_code_enum *code)
1481 struct fimc_fmt *fmt;
1483 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1486 code->code = fmt->mbus_code;
1490 static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
1491 struct v4l2_subdev_fh *fh,
1492 struct v4l2_subdev_format *fmt)
1494 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1495 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1496 struct fimc_frame *ff = &ctx->s_frame;
1497 struct v4l2_mbus_framefmt *mf;
1499 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1500 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1506 mutex_lock(&fimc->lock);
1509 case FIMC_SD_PAD_SOURCE:
1510 if (!WARN_ON(ff->fmt == NULL))
1511 mf->code = ff->fmt->mbus_code;
1512 /* Sink pads crop rectangle size */
1513 mf->width = ff->width;
1514 mf->height = ff->height;
1516 case FIMC_SD_PAD_SINK_FIFO:
1517 *mf = fimc->vid_cap.wb_fmt;
1519 case FIMC_SD_PAD_SINK_CAM:
1521 *mf = fimc->vid_cap.ci_fmt;
1525 mutex_unlock(&fimc->lock);
1526 mf->colorspace = V4L2_COLORSPACE_JPEG;
1531 static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
1532 struct v4l2_subdev_fh *fh,
1533 struct v4l2_subdev_format *fmt)
1535 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1536 struct v4l2_mbus_framefmt *mf = &fmt->format;
1537 struct fimc_vid_cap *vc = &fimc->vid_cap;
1538 struct fimc_ctx *ctx = vc->ctx;
1539 struct fimc_frame *ff;
1540 struct fimc_fmt *ffmt;
1542 dbg("pad%d: code: 0x%x, %dx%d",
1543 fmt->pad, mf->code, mf->width, mf->height);
1545 if (fmt->pad == FIMC_SD_PAD_SOURCE && vb2_is_busy(&vc->vbq))
1548 mutex_lock(&fimc->lock);
1549 ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1550 &mf->code, NULL, fmt->pad);
1551 mutex_unlock(&fimc->lock);
1552 mf->colorspace = V4L2_COLORSPACE_JPEG;
1554 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1555 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1559 /* There must be a bug in the driver if this happens */
1560 if (WARN_ON(ffmt == NULL))
1563 /* Update RGB Alpha control state and value range */
1564 fimc_alpha_ctrl_update(ctx);
1566 fimc_capture_mark_jpeg_xfer(ctx, ffmt->color);
1567 if (fmt->pad == FIMC_SD_PAD_SOURCE) {
1569 /* Sink pads crop rectangle size */
1570 mf->width = ctx->s_frame.width;
1571 mf->height = ctx->s_frame.height;
1576 mutex_lock(&fimc->lock);
1577 set_frame_bounds(ff, mf->width, mf->height);
1579 if (fmt->pad == FIMC_SD_PAD_SINK_FIFO)
1581 else if (fmt->pad == FIMC_SD_PAD_SINK_CAM)
1586 /* Reset the crop rectangle if required. */
1587 if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_COMPOSE)))
1588 set_frame_crop(ff, 0, 0, mf->width, mf->height);
1590 if (fmt->pad != FIMC_SD_PAD_SOURCE)
1591 ctx->state &= ~FIMC_COMPOSE;
1593 mutex_unlock(&fimc->lock);
1597 static int fimc_subdev_get_selection(struct v4l2_subdev *sd,
1598 struct v4l2_subdev_fh *fh,
1599 struct v4l2_subdev_selection *sel)
1601 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1602 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1603 struct fimc_frame *f = &ctx->s_frame;
1604 struct v4l2_rect *r = &sel->r;
1605 struct v4l2_rect *try_sel;
1607 if (sel->pad == FIMC_SD_PAD_SOURCE)
1610 mutex_lock(&fimc->lock);
1612 switch (sel->target) {
1613 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1615 case V4L2_SEL_TGT_CROP_BOUNDS:
1616 r->width = f->o_width;
1617 r->height = f->o_height;
1620 mutex_unlock(&fimc->lock);
1623 case V4L2_SEL_TGT_CROP:
1624 try_sel = v4l2_subdev_get_try_crop(fh, sel->pad);
1626 case V4L2_SEL_TGT_COMPOSE:
1627 try_sel = v4l2_subdev_get_try_compose(fh, sel->pad);
1631 mutex_unlock(&fimc->lock);
1635 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1638 r->left = f->offs_h;
1640 r->width = f->width;
1641 r->height = f->height;
1644 dbg("target %#x: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1645 sel->pad, r->left, r->top, r->width, r->height,
1646 f->f_width, f->f_height);
1648 mutex_unlock(&fimc->lock);
1652 static int fimc_subdev_set_selection(struct v4l2_subdev *sd,
1653 struct v4l2_subdev_fh *fh,
1654 struct v4l2_subdev_selection *sel)
1656 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1657 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1658 struct fimc_frame *f = &ctx->s_frame;
1659 struct v4l2_rect *r = &sel->r;
1660 struct v4l2_rect *try_sel;
1661 unsigned long flags;
1663 if (sel->pad == FIMC_SD_PAD_SOURCE)
1666 mutex_lock(&fimc->lock);
1667 fimc_capture_try_selection(ctx, r, V4L2_SEL_TGT_CROP);
1669 switch (sel->target) {
1670 case V4L2_SEL_TGT_CROP:
1671 try_sel = v4l2_subdev_get_try_crop(fh, sel->pad);
1673 case V4L2_SEL_TGT_COMPOSE:
1674 try_sel = v4l2_subdev_get_try_compose(fh, sel->pad);
1678 mutex_unlock(&fimc->lock);
1682 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1685 spin_lock_irqsave(&fimc->slock, flags);
1686 set_frame_crop(f, r->left, r->top, r->width, r->height);
1687 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1688 if (sel->target == V4L2_SEL_TGT_COMPOSE)
1689 ctx->state |= FIMC_COMPOSE;
1690 spin_unlock_irqrestore(&fimc->slock, flags);
1693 dbg("target %#x: (%d,%d)/%dx%d", sel->target, r->left, r->top,
1694 r->width, r->height);
1696 mutex_unlock(&fimc->lock);
1700 static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1701 .enum_mbus_code = fimc_subdev_enum_mbus_code,
1702 .get_selection = fimc_subdev_get_selection,
1703 .set_selection = fimc_subdev_set_selection,
1704 .get_fmt = fimc_subdev_get_fmt,
1705 .set_fmt = fimc_subdev_set_fmt,
1708 static struct v4l2_subdev_ops fimc_subdev_ops = {
1709 .pad = &fimc_subdev_pad_ops,
1712 /* Set default format at the sensor and host interface */
1713 static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1715 struct v4l2_format fmt = {
1716 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1720 .pixelformat = V4L2_PIX_FMT_YUYV,
1721 .field = V4L2_FIELD_NONE,
1722 .colorspace = V4L2_COLORSPACE_JPEG,
1726 return __fimc_capture_set_format(fimc, &fmt);
1729 /* fimc->lock must be already initialized */
1730 static int fimc_register_capture_device(struct fimc_dev *fimc,
1731 struct v4l2_device *v4l2_dev)
1733 struct video_device *vfd = &fimc->vid_cap.ve.vdev;
1734 struct vb2_queue *q = &fimc->vid_cap.vbq;
1735 struct fimc_ctx *ctx;
1736 struct fimc_vid_cap *vid_cap;
1739 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1743 ctx->fimc_dev = fimc;
1744 ctx->in_path = FIMC_IO_CAMERA;
1745 ctx->out_path = FIMC_IO_DMA;
1746 ctx->state = FIMC_CTX_CAP;
1747 ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1748 ctx->d_frame.fmt = ctx->s_frame.fmt;
1750 memset(vfd, 0, sizeof(*vfd));
1751 snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.capture", fimc->id);
1753 vfd->fops = &fimc_capture_fops;
1754 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
1755 vfd->v4l2_dev = v4l2_dev;
1757 vfd->release = video_device_release_empty;
1759 vfd->lock = &fimc->lock;
1761 video_set_drvdata(vfd, fimc);
1762 vid_cap = &fimc->vid_cap;
1763 vid_cap->active_buf_cnt = 0;
1764 vid_cap->reqbufs_count = 0;
1767 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1768 INIT_LIST_HEAD(&vid_cap->active_buf_q);
1770 memset(q, 0, sizeof(*q));
1771 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1772 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1774 q->ops = &fimc_capture_qops;
1775 q->mem_ops = &vb2_dma_contig_memops;
1776 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1777 q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1778 q->lock = &fimc->lock;
1780 ret = vb2_queue_init(q);
1784 vid_cap->vd_pad.flags = MEDIA_PAD_FL_SINK;
1785 ret = media_entity_init(&vfd->entity, 1, &vid_cap->vd_pad, 0);
1789 ret = fimc_ctrls_create(ctx);
1791 goto err_me_cleanup;
1793 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
1797 v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
1798 vfd->name, video_device_node_name(vfd));
1800 vfd->ctrl_handler = &ctx->ctrls.handler;
1804 fimc_ctrls_delete(ctx);
1806 media_entity_cleanup(&vfd->entity);
1812 static int fimc_capture_subdev_registered(struct v4l2_subdev *sd)
1814 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1820 ret = fimc_register_m2m_device(fimc, sd->v4l2_dev);
1824 fimc->pipeline_ops = v4l2_get_subdev_hostdata(sd);
1826 ret = fimc_register_capture_device(fimc, sd->v4l2_dev);
1828 fimc_unregister_m2m_device(fimc);
1829 fimc->pipeline_ops = NULL;
1835 static void fimc_capture_subdev_unregistered(struct v4l2_subdev *sd)
1837 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1838 struct video_device *vdev;
1843 fimc_unregister_m2m_device(fimc);
1844 vdev = &fimc->vid_cap.ve.vdev;
1846 if (video_is_registered(vdev)) {
1847 video_unregister_device(vdev);
1848 media_entity_cleanup(&vdev->entity);
1849 fimc_ctrls_delete(fimc->vid_cap.ctx);
1850 fimc->pipeline_ops = NULL;
1852 kfree(fimc->vid_cap.ctx);
1853 fimc->vid_cap.ctx = NULL;
1856 static const struct v4l2_subdev_internal_ops fimc_capture_sd_internal_ops = {
1857 .registered = fimc_capture_subdev_registered,
1858 .unregistered = fimc_capture_subdev_unregistered,
1861 int fimc_initialize_capture_subdev(struct fimc_dev *fimc)
1863 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1866 v4l2_subdev_init(sd, &fimc_subdev_ops);
1867 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1868 snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->id);
1870 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK_CAM].flags = MEDIA_PAD_FL_SINK;
1871 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK_FIFO].flags = MEDIA_PAD_FL_SINK;
1872 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1873 ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
1874 fimc->vid_cap.sd_pads, 0);
1878 sd->entity.ops = &fimc_sd_media_ops;
1879 sd->internal_ops = &fimc_capture_sd_internal_ops;
1880 v4l2_set_subdevdata(sd, fimc);
1884 void fimc_unregister_capture_subdev(struct fimc_dev *fimc)
1886 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1888 v4l2_device_unregister_subdev(sd);
1889 media_entity_cleanup(&sd->entity);
1890 v4l2_set_subdevdata(sd, NULL);