1 // SPDX-License-Identifier: GPL-2.0-only
3 * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver
5 * Copyright (C) 2010 - 2012 Samsung Electronics Co., Ltd.
6 * Sylwester Nawrocki <s.nawrocki@samsung.com>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/types.h>
12 #include <linux/errno.h>
13 #include <linux/bug.h>
14 #include <linux/interrupt.h>
15 #include <linux/device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/list.h>
18 #include <linux/slab.h>
20 #include <linux/videodev2.h>
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-ioctl.h>
23 #include <media/v4l2-mem2mem.h>
24 #include <media/videobuf2-v4l2.h>
25 #include <media/videobuf2-dma-contig.h>
28 #include "fimc-core.h"
30 #include "media-dev.h"
32 static int fimc_capture_hw_init(struct fimc_dev *fimc)
34 struct fimc_source_info *si = &fimc->vid_cap.source_config;
35 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
39 if (ctx == NULL || ctx->s_frame.fmt == NULL)
42 if (si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK) {
43 ret = fimc_hw_camblk_cfg_writeback(fimc);
48 spin_lock_irqsave(&fimc->slock, flags);
49 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
50 fimc_set_yuv_order(ctx);
52 fimc_hw_set_camera_polarity(fimc, si);
53 fimc_hw_set_camera_type(fimc, si);
54 fimc_hw_set_camera_source(fimc, si);
55 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
57 ret = fimc_set_scaler_info(ctx);
59 fimc_hw_set_input_path(ctx);
60 fimc_hw_set_prescaler(ctx);
61 fimc_hw_set_mainscaler(ctx);
62 fimc_hw_set_target_format(ctx);
63 fimc_hw_set_rotation(ctx);
64 fimc_hw_set_effect(ctx);
65 fimc_hw_set_output_path(ctx);
66 fimc_hw_set_out_dma(ctx);
67 if (fimc->drv_data->alpha_color)
68 fimc_hw_set_rgb_alpha(ctx);
69 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
71 spin_unlock_irqrestore(&fimc->slock, flags);
76 * Reinitialize the driver so it is ready to start the streaming again.
77 * Set fimc->state to indicate stream off and the hardware shut down state.
78 * If not suspending (@suspend is false), return any buffers to videobuf2.
79 * Otherwise put any owned buffers onto the pending buffers queue, so they
80 * can be re-spun when the device is being resumed. Also perform FIMC
81 * software reset and disable streaming on the whole pipeline if required.
83 static int fimc_capture_state_cleanup(struct fimc_dev *fimc, bool suspend)
85 struct fimc_vid_cap *cap = &fimc->vid_cap;
86 struct fimc_vid_buffer *buf;
90 spin_lock_irqsave(&fimc->slock, flags);
91 streaming = fimc->state & (1 << ST_CAPT_ISP_STREAM);
93 fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_SHUT |
94 1 << ST_CAPT_STREAM | 1 << ST_CAPT_ISP_STREAM);
96 fimc->state |= (1 << ST_CAPT_SUSPENDED);
98 fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED);
100 /* Release unused buffers */
101 while (!suspend && !list_empty(&cap->pending_buf_q)) {
102 buf = fimc_pending_queue_pop(cap);
103 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
105 /* If suspending put unused buffers onto pending queue */
106 while (!list_empty(&cap->active_buf_q)) {
107 buf = fimc_active_queue_pop(cap);
109 fimc_pending_queue_add(cap, buf);
111 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
117 spin_unlock_irqrestore(&fimc->slock, flags);
120 return fimc_pipeline_call(&cap->ve, set_stream, 0);
125 static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend)
129 if (!fimc_capture_active(fimc))
132 spin_lock_irqsave(&fimc->slock, flags);
133 set_bit(ST_CAPT_SHUT, &fimc->state);
134 fimc_deactivate_capture(fimc);
135 spin_unlock_irqrestore(&fimc->slock, flags);
137 wait_event_timeout(fimc->irq_queue,
138 !test_bit(ST_CAPT_SHUT, &fimc->state),
139 (2*HZ/10)); /* 200 ms */
141 return fimc_capture_state_cleanup(fimc, suspend);
145 * fimc_capture_config_update - apply the camera interface configuration
146 * @ctx: FIMC capture context
148 * To be called from within the interrupt handler with fimc.slock
149 * spinlock held. It updates the camera pixel crop, rotation and
152 static int fimc_capture_config_update(struct fimc_ctx *ctx)
154 struct fimc_dev *fimc = ctx->fimc_dev;
157 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
159 ret = fimc_set_scaler_info(ctx);
163 fimc_hw_set_prescaler(ctx);
164 fimc_hw_set_mainscaler(ctx);
165 fimc_hw_set_target_format(ctx);
166 fimc_hw_set_rotation(ctx);
167 fimc_hw_set_effect(ctx);
168 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
169 fimc_hw_set_out_dma(ctx);
170 if (fimc->drv_data->alpha_color)
171 fimc_hw_set_rgb_alpha(ctx);
173 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
177 void fimc_capture_irq_handler(struct fimc_dev *fimc, int deq_buf)
179 struct fimc_vid_cap *cap = &fimc->vid_cap;
180 struct fimc_pipeline *p = to_fimc_pipeline(cap->ve.pipe);
181 struct v4l2_subdev *csis = p->subdevs[IDX_CSIS];
182 struct fimc_frame *f = &cap->ctx->d_frame;
183 struct fimc_vid_buffer *v_buf;
185 if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
186 wake_up(&fimc->irq_queue);
190 if (!list_empty(&cap->active_buf_q) &&
191 test_bit(ST_CAPT_RUN, &fimc->state) && deq_buf) {
192 v_buf = fimc_active_queue_pop(cap);
194 v_buf->vb.vb2_buf.timestamp = ktime_get_ns();
195 v_buf->vb.sequence = cap->frame_count++;
197 vb2_buffer_done(&v_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
200 if (!list_empty(&cap->pending_buf_q)) {
202 v_buf = fimc_pending_queue_pop(cap);
203 fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
204 v_buf->index = cap->buf_index;
206 /* Move the buffer to the capture active queue */
207 fimc_active_queue_add(cap, v_buf);
209 dbg("next frame: %d, done frame: %d",
210 fimc_hw_get_frame_index(fimc), v_buf->index);
212 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
216 * Set up a buffer at MIPI-CSIS if current image format
217 * requires the frame embedded data capture.
219 if (f->fmt->mdataplanes && !list_empty(&cap->active_buf_q)) {
220 unsigned int plane = ffs(f->fmt->mdataplanes) - 1;
221 unsigned int size = f->payload[plane];
222 s32 index = fimc_hw_get_frame_index(fimc);
225 list_for_each_entry(v_buf, &cap->active_buf_q, list) {
226 if (v_buf->index != index)
228 vaddr = vb2_plane_vaddr(&v_buf->vb.vb2_buf, plane);
229 v4l2_subdev_call(csis, video, s_rx_buffer,
235 if (cap->active_buf_cnt == 0) {
237 clear_bit(ST_CAPT_RUN, &fimc->state);
239 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
242 set_bit(ST_CAPT_RUN, &fimc->state);
245 if (test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
246 fimc_capture_config_update(cap->ctx);
248 if (cap->active_buf_cnt == 1) {
249 fimc_deactivate_capture(fimc);
250 clear_bit(ST_CAPT_STREAM, &fimc->state);
253 dbg("frame: %d, active_buf_cnt: %d",
254 fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
258 static int start_streaming(struct vb2_queue *q, unsigned int count)
260 struct fimc_ctx *ctx = q->drv_priv;
261 struct fimc_dev *fimc = ctx->fimc_dev;
262 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
266 vid_cap->frame_count = 0;
268 ret = fimc_capture_hw_init(fimc);
270 fimc_capture_state_cleanup(fimc, false);
274 set_bit(ST_CAPT_PEND, &fimc->state);
276 min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
278 if (vid_cap->active_buf_cnt >= min_bufs &&
279 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
280 fimc_activate_capture(ctx);
282 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
283 return fimc_pipeline_call(&vid_cap->ve, set_stream, 1);
289 static void stop_streaming(struct vb2_queue *q)
291 struct fimc_ctx *ctx = q->drv_priv;
292 struct fimc_dev *fimc = ctx->fimc_dev;
294 if (!fimc_capture_active(fimc))
297 fimc_stop_capture(fimc, false);
300 int fimc_capture_suspend(struct fimc_dev *fimc)
302 bool suspend = fimc_capture_busy(fimc);
304 int ret = fimc_stop_capture(fimc, suspend);
307 return fimc_pipeline_call(&fimc->vid_cap.ve, close);
310 static void buffer_queue(struct vb2_buffer *vb);
312 int fimc_capture_resume(struct fimc_dev *fimc)
314 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
315 struct exynos_video_entity *ve = &vid_cap->ve;
316 struct fimc_vid_buffer *buf;
319 if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
322 INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
323 vid_cap->buf_index = 0;
324 fimc_pipeline_call(ve, open, &ve->vdev.entity, false);
325 fimc_capture_hw_init(fimc);
327 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
329 for (i = 0; i < vid_cap->reqbufs_count; i++) {
330 if (list_empty(&vid_cap->pending_buf_q))
332 buf = fimc_pending_queue_pop(vid_cap);
333 buffer_queue(&buf->vb.vb2_buf);
339 static int queue_setup(struct vb2_queue *vq,
340 unsigned int *num_buffers, unsigned int *num_planes,
341 unsigned int sizes[], struct device *alloc_devs[])
343 struct fimc_ctx *ctx = vq->drv_priv;
344 struct fimc_frame *frame = &ctx->d_frame;
345 struct fimc_fmt *fmt = frame->fmt;
346 unsigned long wh = frame->f_width * frame->f_height;
353 if (*num_planes != fmt->memplanes)
355 for (i = 0; i < *num_planes; i++)
356 if (sizes[i] < (wh * fmt->depth[i]) / 8)
361 *num_planes = fmt->memplanes;
363 for (i = 0; i < fmt->memplanes; i++) {
364 unsigned int size = (wh * fmt->depth[i]) / 8;
366 if (fimc_fmt_is_user_defined(fmt->color))
367 sizes[i] = frame->payload[i];
369 sizes[i] = max_t(u32, size, frame->payload[i]);
375 static int buffer_prepare(struct vb2_buffer *vb)
377 struct vb2_queue *vq = vb->vb2_queue;
378 struct fimc_ctx *ctx = vq->drv_priv;
381 if (ctx->d_frame.fmt == NULL)
384 for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
385 unsigned long size = ctx->d_frame.payload[i];
387 if (vb2_plane_size(vb, i) < size) {
388 v4l2_err(&ctx->fimc_dev->vid_cap.ve.vdev,
389 "User buffer too small (%ld < %ld)\n",
390 vb2_plane_size(vb, i), size);
393 vb2_set_plane_payload(vb, i, size);
399 static void buffer_queue(struct vb2_buffer *vb)
401 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
402 struct fimc_vid_buffer *buf
403 = container_of(vbuf, struct fimc_vid_buffer, vb);
404 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
405 struct fimc_dev *fimc = ctx->fimc_dev;
406 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
407 struct exynos_video_entity *ve = &vid_cap->ve;
411 spin_lock_irqsave(&fimc->slock, flags);
412 fimc_prepare_addr(ctx, &buf->vb.vb2_buf, &ctx->d_frame, &buf->paddr);
414 if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
415 !test_bit(ST_CAPT_STREAM, &fimc->state) &&
416 vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
417 /* Setup the buffer directly for processing. */
418 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
421 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
422 buf->index = vid_cap->buf_index;
423 fimc_active_queue_add(vid_cap, buf);
425 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
426 vid_cap->buf_index = 0;
428 fimc_pending_queue_add(vid_cap, buf);
431 min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
434 if (vb2_is_streaming(&vid_cap->vbq) &&
435 vid_cap->active_buf_cnt >= min_bufs &&
436 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
439 fimc_activate_capture(ctx);
440 spin_unlock_irqrestore(&fimc->slock, flags);
442 if (test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
445 ret = fimc_pipeline_call(ve, set_stream, 1);
447 v4l2_err(&ve->vdev, "stream on failed: %d\n", ret);
450 spin_unlock_irqrestore(&fimc->slock, flags);
453 static const struct vb2_ops fimc_capture_qops = {
454 .queue_setup = queue_setup,
455 .buf_prepare = buffer_prepare,
456 .buf_queue = buffer_queue,
457 .wait_prepare = vb2_ops_wait_prepare,
458 .wait_finish = vb2_ops_wait_finish,
459 .start_streaming = start_streaming,
460 .stop_streaming = stop_streaming,
463 static int fimc_capture_set_default_format(struct fimc_dev *fimc);
465 static int fimc_capture_open(struct file *file)
467 struct fimc_dev *fimc = video_drvdata(file);
468 struct fimc_vid_cap *vc = &fimc->vid_cap;
469 struct exynos_video_entity *ve = &vc->ve;
472 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
474 mutex_lock(&fimc->lock);
476 if (fimc_m2m_active(fimc))
479 set_bit(ST_CAPT_BUSY, &fimc->state);
480 ret = pm_runtime_get_sync(&fimc->pdev->dev);
484 ret = v4l2_fh_open(file);
486 pm_runtime_put_sync(&fimc->pdev->dev);
490 if (v4l2_fh_is_singular_file(file)) {
491 fimc_md_graph_lock(ve);
493 ret = fimc_pipeline_call(ve, open, &ve->vdev.entity, true);
495 if (ret == 0 && vc->user_subdev_api && vc->inh_sensor_ctrls) {
497 * Recreate controls of the the video node to drop
498 * any controls inherited from the sensor subdev.
500 fimc_ctrls_delete(vc->ctx);
502 ret = fimc_ctrls_create(vc->ctx);
504 vc->inh_sensor_ctrls = false;
507 ve->vdev.entity.use_count++;
509 fimc_md_graph_unlock(ve);
512 ret = fimc_capture_set_default_format(fimc);
515 clear_bit(ST_CAPT_BUSY, &fimc->state);
516 pm_runtime_put_sync(&fimc->pdev->dev);
517 v4l2_fh_release(file);
521 mutex_unlock(&fimc->lock);
525 static int fimc_capture_release(struct file *file)
527 struct fimc_dev *fimc = video_drvdata(file);
528 struct fimc_vid_cap *vc = &fimc->vid_cap;
529 bool close = v4l2_fh_is_singular_file(file);
532 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
534 mutex_lock(&fimc->lock);
536 if (close && vc->streaming) {
537 media_pipeline_stop(&vc->ve.vdev.entity);
538 vc->streaming = false;
541 ret = _vb2_fop_release(file, NULL);
544 clear_bit(ST_CAPT_BUSY, &fimc->state);
545 fimc_pipeline_call(&vc->ve, close);
546 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
548 fimc_md_graph_lock(&vc->ve);
549 vc->ve.vdev.entity.use_count--;
550 fimc_md_graph_unlock(&vc->ve);
553 pm_runtime_put_sync(&fimc->pdev->dev);
554 mutex_unlock(&fimc->lock);
559 static const struct v4l2_file_operations fimc_capture_fops = {
560 .owner = THIS_MODULE,
561 .open = fimc_capture_open,
562 .release = fimc_capture_release,
563 .poll = vb2_fop_poll,
564 .unlocked_ioctl = video_ioctl2,
565 .mmap = vb2_fop_mmap,
569 * Format and crop negotiation helpers
572 static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
573 u32 *width, u32 *height,
574 u32 *code, u32 *fourcc, int pad)
576 bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
577 struct fimc_dev *fimc = ctx->fimc_dev;
578 const struct fimc_variant *var = fimc->variant;
579 const struct fimc_pix_limit *pl = var->pix_limit;
580 struct fimc_frame *dst = &ctx->d_frame;
581 u32 depth, min_w, max_w, min_h, align_h = 3;
582 u32 mask = FMT_FLAGS_CAM;
583 struct fimc_fmt *ffmt;
585 /* Conversion from/to JPEG or User Defined format is not supported */
586 if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
587 fimc_fmt_is_user_defined(ctx->s_frame.fmt->color))
588 *code = ctx->s_frame.fmt->mbus_code;
590 if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad == FIMC_SD_PAD_SOURCE)
591 mask |= FMT_FLAGS_M2M;
593 if (pad == FIMC_SD_PAD_SINK_FIFO)
594 mask = FMT_FLAGS_WRITEBACK;
596 ffmt = fimc_find_format(fourcc, code, mask, 0);
601 *code = ffmt->mbus_code;
603 *fourcc = ffmt->fourcc;
605 if (pad != FIMC_SD_PAD_SOURCE) {
606 max_w = fimc_fmt_is_user_defined(ffmt->color) ?
607 pl->scaler_dis_w : pl->scaler_en_w;
608 /* Apply the camera input interface pixel constraints */
609 v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
610 height, max_t(u32, *height, 32),
611 FIMC_CAMIF_MAX_HEIGHT,
612 fimc_fmt_is_user_defined(ffmt->color) ?
617 /* Can't scale or crop in transparent (JPEG) transfer mode */
618 if (fimc_fmt_is_user_defined(ffmt->color)) {
619 *width = ctx->s_frame.f_width;
620 *height = ctx->s_frame.f_height;
623 /* Apply the scaler and the output DMA constraints */
624 max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
625 if (ctx->state & FIMC_COMPOSE) {
626 min_w = dst->offs_h + dst->width;
627 min_h = dst->offs_v + dst->height;
629 min_w = var->min_out_pixsize;
630 min_h = var->min_out_pixsize;
632 if (var->min_vsize_align == 1 && !rotation)
633 align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
635 depth = fimc_get_format_depth(ffmt);
636 v4l_bound_align_image(width, min_w, max_w,
637 ffs(var->min_out_pixsize) - 1,
638 height, min_h, FIMC_CAMIF_MAX_HEIGHT,
640 64/(ALIGN(depth, 8)));
642 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
643 pad, code ? *code : 0, *width, *height,
644 dst->f_width, dst->f_height);
649 static void fimc_capture_try_selection(struct fimc_ctx *ctx,
653 bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
654 struct fimc_dev *fimc = ctx->fimc_dev;
655 const struct fimc_variant *var = fimc->variant;
656 const struct fimc_pix_limit *pl = var->pix_limit;
657 struct fimc_frame *sink = &ctx->s_frame;
658 u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
659 u32 align_sz = 0, align_h = 4;
660 u32 max_sc_h, max_sc_v;
662 /* In JPEG transparent transfer mode cropping is not supported */
663 if (fimc_fmt_is_user_defined(ctx->d_frame.fmt->color)) {
664 r->width = sink->f_width;
665 r->height = sink->f_height;
666 r->left = r->top = 0;
669 if (target == V4L2_SEL_TGT_COMPOSE) {
670 u32 tmp_min_h = ffs(sink->width) - 3;
671 u32 tmp_min_v = ffs(sink->height) - 1;
673 if (ctx->rotation != 90 && ctx->rotation != 270)
675 max_sc_h = min(SCALER_MAX_HRATIO, 1 << tmp_min_h);
676 max_sc_v = min(SCALER_MAX_VRATIO, 1 << tmp_min_v);
677 min_sz = var->min_out_pixsize;
679 u32 depth = fimc_get_format_depth(sink->fmt);
680 align_sz = 64/ALIGN(depth, 8);
681 min_sz = var->min_inp_pixsize;
682 min_w = min_h = min_sz;
683 max_sc_h = max_sc_v = 1;
686 * For the compose rectangle the following constraints must be met:
687 * - it must fit in the sink pad format rectangle (f_width/f_height);
688 * - maximum downscaling ratio is 64;
689 * - maximum crop size depends if the rotator is used or not;
690 * - the sink pad format width/height must be 4 multiple of the
691 * prescaler ratios determined by sink pad size and source pad crop,
692 * the prescaler ratio is returned by fimc_get_scaler_factor().
695 rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
696 rotate ? sink->f_height : sink->f_width);
697 max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
699 if (target == V4L2_SEL_TGT_COMPOSE) {
700 min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
701 min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
703 swap(max_sc_h, max_sc_v);
707 v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
708 &r->height, min_h, max_h, align_h,
710 /* Adjust left/top if crop/compose rectangle is out of bounds */
711 r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
712 r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height);
713 r->left = round_down(r->left, var->hor_offs_align);
715 dbg("target %#x: (%d,%d)/%dx%d, sink fmt: %dx%d",
716 target, r->left, r->top, r->width, r->height,
717 sink->f_width, sink->f_height);
721 * The video node ioctl operations
723 static int fimc_cap_querycap(struct file *file, void *priv,
724 struct v4l2_capability *cap)
726 struct fimc_dev *fimc = video_drvdata(file);
728 __fimc_vidioc_querycap(&fimc->pdev->dev, cap);
732 static int fimc_cap_enum_fmt(struct file *file, void *priv,
733 struct v4l2_fmtdesc *f)
735 struct fimc_fmt *fmt;
737 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
741 strscpy(f->description, fmt->name, sizeof(f->description));
742 f->pixelformat = fmt->fourcc;
743 if (fmt->fourcc == MEDIA_BUS_FMT_JPEG_1X8)
744 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
748 static struct media_entity *fimc_pipeline_get_head(struct media_entity *me)
750 struct media_pad *pad = &me->pads[0];
752 while (!(pad->flags & MEDIA_PAD_FL_SOURCE)) {
753 pad = media_entity_remote_pad(pad);
764 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
766 * @ctx: FIMC capture context
767 * @tfmt: media bus format to try/set on subdevs
768 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
769 * @set: true to set format on subdevs, false to try only
771 static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
772 struct v4l2_mbus_framefmt *tfmt,
773 struct fimc_fmt **fmt_id,
776 struct fimc_dev *fimc = ctx->fimc_dev;
777 struct fimc_pipeline *p = to_fimc_pipeline(fimc->vid_cap.ve.pipe);
778 struct v4l2_subdev *sd = p->subdevs[IDX_SENSOR];
779 struct v4l2_subdev_format sfmt;
780 struct v4l2_mbus_framefmt *mf = &sfmt.format;
781 struct media_entity *me;
782 struct fimc_fmt *ffmt;
783 struct media_pad *pad;
787 if (WARN_ON(!sd || !tfmt))
790 memset(&sfmt, 0, sizeof(sfmt));
792 sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
794 me = fimc_pipeline_get_head(&sd->entity);
797 ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
801 * Notify user-space if common pixel code for
802 * host and sensor does not exist.
806 mf->code = tfmt->code = ffmt->mbus_code;
808 /* set format on all pipeline subdevs */
809 while (me != &fimc->vid_cap.subdev.entity) {
810 sd = media_entity_to_v4l2_subdev(me);
813 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
817 if (me->pads[0].flags & MEDIA_PAD_FL_SINK) {
818 sfmt.pad = me->num_pads - 1;
819 mf->code = tfmt->code;
820 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL,
826 pad = media_entity_remote_pad(&me->pads[sfmt.pad]);
832 if (mf->code != tfmt->code)
836 tfmt->width = mf->width;
837 tfmt->height = mf->height;
838 ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
839 NULL, &fcc, FIMC_SD_PAD_SINK_CAM);
840 ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
841 NULL, &fcc, FIMC_SD_PAD_SOURCE);
842 if (ffmt && ffmt->mbus_code)
843 mf->code = ffmt->mbus_code;
844 if (mf->width != tfmt->width || mf->height != tfmt->height)
846 tfmt->code = mf->code;
858 * fimc_get_sensor_frame_desc - query the sensor for media bus frame parameters
859 * @sensor: pointer to the sensor subdev
860 * @plane_fmt: provides plane sizes corresponding to the frame layout entries
861 * @num_planes: number of planes
862 * @try: true to set the frame parameters, false to query only
864 * This function is used by this driver only for compressed/blob data formats.
866 static int fimc_get_sensor_frame_desc(struct v4l2_subdev *sensor,
867 struct v4l2_plane_pix_format *plane_fmt,
868 unsigned int num_planes, bool try)
870 struct v4l2_mbus_frame_desc fd;
874 for (i = 0; i < num_planes; i++)
875 fd.entry[i].length = plane_fmt[i].sizeimage;
877 pad = sensor->entity.num_pads - 1;
879 ret = v4l2_subdev_call(sensor, pad, set_frame_desc, pad, &fd);
881 ret = v4l2_subdev_call(sensor, pad, get_frame_desc, pad, &fd);
886 if (num_planes != fd.num_entries)
889 for (i = 0; i < num_planes; i++)
890 plane_fmt[i].sizeimage = fd.entry[i].length;
892 if (fd.entry[0].length > FIMC_MAX_JPEG_BUF_SIZE) {
893 v4l2_err(sensor->v4l2_dev, "Unsupported buffer size: %u\n",
902 static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
903 struct v4l2_format *f)
905 struct fimc_dev *fimc = video_drvdata(file);
907 __fimc_get_format(&fimc->vid_cap.ctx->d_frame, f);
912 * Try or set format on the fimc.X.capture video node and additionally
913 * on the whole pipeline if @try is false.
914 * Locking: the caller must _not_ hold the graph mutex.
916 static int __video_try_or_set_format(struct fimc_dev *fimc,
917 struct v4l2_format *f, bool try,
918 struct fimc_fmt **inp_fmt,
919 struct fimc_fmt **out_fmt)
921 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
922 struct fimc_vid_cap *vc = &fimc->vid_cap;
923 struct exynos_video_entity *ve = &vc->ve;
924 struct fimc_ctx *ctx = vc->ctx;
925 unsigned int width = 0, height = 0;
928 /* Pre-configure format at the camera input interface, for JPEG only */
929 if (fimc_jpeg_fourcc(pix->pixelformat)) {
930 fimc_capture_try_format(ctx, &pix->width, &pix->height,
931 NULL, &pix->pixelformat,
932 FIMC_SD_PAD_SINK_CAM);
935 height = pix->height;
937 ctx->s_frame.f_width = pix->width;
938 ctx->s_frame.f_height = pix->height;
942 /* Try the format at the scaler and the DMA output */
943 *out_fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
944 NULL, &pix->pixelformat,
946 if (*out_fmt == NULL)
949 /* Restore image width/height for JPEG (no resizing supported). */
950 if (try && fimc_jpeg_fourcc(pix->pixelformat)) {
952 pix->height = height;
955 /* Try to match format at the host and the sensor */
956 if (!vc->user_subdev_api) {
957 struct v4l2_mbus_framefmt mbus_fmt;
958 struct v4l2_mbus_framefmt *mf;
960 mf = try ? &mbus_fmt : &fimc->vid_cap.ci_fmt;
962 mf->code = (*out_fmt)->mbus_code;
963 mf->width = pix->width;
964 mf->height = pix->height;
966 fimc_md_graph_lock(ve);
967 ret = fimc_pipeline_try_format(ctx, mf, inp_fmt, try);
968 fimc_md_graph_unlock(ve);
973 pix->width = mf->width;
974 pix->height = mf->height;
977 fimc_adjust_mplane_format(*out_fmt, pix->width, pix->height, pix);
979 if ((*out_fmt)->flags & FMT_FLAGS_COMPRESSED) {
980 struct v4l2_subdev *sensor;
982 fimc_md_graph_lock(ve);
984 sensor = __fimc_md_get_subdev(ve->pipe, IDX_SENSOR);
986 fimc_get_sensor_frame_desc(sensor, pix->plane_fmt,
987 (*out_fmt)->memplanes, try);
991 fimc_md_graph_unlock(ve);
997 static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
998 struct v4l2_format *f)
1000 struct fimc_dev *fimc = video_drvdata(file);
1001 struct fimc_fmt *out_fmt = NULL, *inp_fmt = NULL;
1003 return __video_try_or_set_format(fimc, f, true, &inp_fmt, &out_fmt);
1006 static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx,
1007 enum fimc_color_fmt color)
1009 bool jpeg = fimc_fmt_is_user_defined(color);
1011 ctx->scaler.enabled = !jpeg;
1012 fimc_ctrls_activate(ctx, !jpeg);
1015 set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
1017 clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
1020 static int __fimc_capture_set_format(struct fimc_dev *fimc,
1021 struct v4l2_format *f)
1023 struct fimc_vid_cap *vc = &fimc->vid_cap;
1024 struct fimc_ctx *ctx = vc->ctx;
1025 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
1026 struct fimc_frame *ff = &ctx->d_frame;
1027 struct fimc_fmt *inp_fmt = NULL;
1030 if (vb2_is_busy(&fimc->vid_cap.vbq))
1033 ret = __video_try_or_set_format(fimc, f, false, &inp_fmt, &ff->fmt);
1037 /* Update RGB Alpha control state and value range */
1038 fimc_alpha_ctrl_update(ctx);
1040 for (i = 0; i < ff->fmt->memplanes; i++) {
1041 ff->bytesperline[i] = pix->plane_fmt[i].bytesperline;
1042 ff->payload[i] = pix->plane_fmt[i].sizeimage;
1045 set_frame_bounds(ff, pix->width, pix->height);
1046 /* Reset the composition rectangle if not yet configured */
1047 if (!(ctx->state & FIMC_COMPOSE))
1048 set_frame_crop(ff, 0, 0, pix->width, pix->height);
1050 fimc_capture_mark_jpeg_xfer(ctx, ff->fmt->color);
1052 /* Reset cropping and set format at the camera interface input */
1053 if (!vc->user_subdev_api) {
1054 ctx->s_frame.fmt = inp_fmt;
1055 set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
1056 set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
1062 static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
1063 struct v4l2_format *f)
1065 struct fimc_dev *fimc = video_drvdata(file);
1067 return __fimc_capture_set_format(fimc, f);
1070 static int fimc_cap_enum_input(struct file *file, void *priv,
1071 struct v4l2_input *i)
1073 struct fimc_dev *fimc = video_drvdata(file);
1074 struct exynos_video_entity *ve = &fimc->vid_cap.ve;
1075 struct v4l2_subdev *sd;
1080 i->type = V4L2_INPUT_TYPE_CAMERA;
1081 fimc_md_graph_lock(ve);
1082 sd = __fimc_md_get_subdev(ve->pipe, IDX_SENSOR);
1083 fimc_md_graph_unlock(ve);
1086 strscpy(i->name, sd->name, sizeof(i->name));
1091 static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
1093 return i == 0 ? i : -EINVAL;
1096 static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
1103 * fimc_pipeline_validate - check for formats inconsistencies
1104 * between source and sink pad of each link
1105 * @fimc: the FIMC device this context applies to
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 fimc_pipeline *p = to_fimc_pipeline(vc->ve.pipe);
1115 struct media_pad *sink_pad, *src_pad;
1120 * Find current entity sink pad and any remote sink pad linked
1121 * to it. We stop if there is no sink pad in current entity or
1122 * it is not linked to any other remote entity.
1126 for (i = 0; i < sd->entity.num_pads; i++) {
1127 struct media_pad *p = &sd->entity.pads[i];
1129 if (p->flags & MEDIA_PAD_FL_SINK) {
1131 src_pad = media_entity_remote_pad(sink_pad);
1137 if (!src_pad || !is_media_entity_v4l2_subdev(src_pad->entity))
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 == p->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_vid_cap *vc = &fimc->vid_cap;
1192 struct media_entity *entity = &vc->ve.vdev.entity;
1193 struct fimc_source_info *si = NULL;
1194 struct v4l2_subdev *sd;
1197 if (fimc_capture_active(fimc))
1200 ret = media_pipeline_start(entity, &vc->ve.pipe->mp);
1204 sd = __fimc_md_get_subdev(vc->ve.pipe, IDX_SENSOR);
1206 si = v4l2_get_subdev_hostdata(sd);
1213 * Save configuration data related to currently attached image
1214 * sensor or other data source, e.g. FIMC-IS.
1216 vc->source_config = *si;
1218 if (vc->input == GRP_ID_FIMC_IS)
1219 vc->source_config.fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
1221 if (vc->user_subdev_api) {
1222 ret = fimc_pipeline_validate(fimc);
1227 ret = vb2_ioctl_streamon(file, priv, type);
1229 vc->streaming = true;
1234 media_pipeline_stop(entity);
1238 static int fimc_cap_streamoff(struct file *file, void *priv,
1239 enum v4l2_buf_type type)
1241 struct fimc_dev *fimc = video_drvdata(file);
1242 struct fimc_vid_cap *vc = &fimc->vid_cap;
1245 ret = vb2_ioctl_streamoff(file, priv, type);
1249 media_pipeline_stop(&vc->ve.vdev.entity);
1250 vc->streaming = false;
1254 static int fimc_cap_reqbufs(struct file *file, void *priv,
1255 struct v4l2_requestbuffers *reqbufs)
1257 struct fimc_dev *fimc = video_drvdata(file);
1260 ret = vb2_ioctl_reqbufs(file, priv, reqbufs);
1263 fimc->vid_cap.reqbufs_count = reqbufs->count;
1268 static int fimc_cap_g_selection(struct file *file, void *fh,
1269 struct v4l2_selection *s)
1271 struct fimc_dev *fimc = video_drvdata(file);
1272 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1273 struct fimc_frame *f = &ctx->s_frame;
1275 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1278 switch (s->target) {
1279 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1280 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:
1294 case V4L2_SEL_TGT_CROP:
1295 s->r.left = f->offs_h;
1296 s->r.top = f->offs_v;
1297 s->r.width = f->width;
1298 s->r.height = f->height;
1305 /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
1306 static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
1308 if (a->left < b->left || a->top < b->top)
1310 if (a->left + a->width > b->left + b->width)
1312 if (a->top + a->height > b->top + b->height)
1318 static int fimc_cap_s_selection(struct file *file, void *fh,
1319 struct v4l2_selection *s)
1321 struct fimc_dev *fimc = video_drvdata(file);
1322 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1323 struct v4l2_rect rect = s->r;
1324 struct fimc_frame *f;
1325 unsigned long flags;
1327 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1330 if (s->target == V4L2_SEL_TGT_COMPOSE)
1332 else if (s->target == V4L2_SEL_TGT_CROP)
1337 fimc_capture_try_selection(ctx, &rect, s->target);
1339 if (s->flags & V4L2_SEL_FLAG_LE &&
1340 !enclosed_rectangle(&rect, &s->r))
1343 if (s->flags & V4L2_SEL_FLAG_GE &&
1344 !enclosed_rectangle(&s->r, &rect))
1348 spin_lock_irqsave(&fimc->slock, flags);
1349 set_frame_crop(f, s->r.left, s->r.top, s->r.width,
1351 spin_unlock_irqrestore(&fimc->slock, flags);
1353 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1357 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1358 .vidioc_querycap = fimc_cap_querycap,
1360 .vidioc_enum_fmt_vid_cap = fimc_cap_enum_fmt,
1361 .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
1362 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
1363 .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
1365 .vidioc_reqbufs = fimc_cap_reqbufs,
1366 .vidioc_querybuf = vb2_ioctl_querybuf,
1367 .vidioc_qbuf = vb2_ioctl_qbuf,
1368 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1369 .vidioc_expbuf = vb2_ioctl_expbuf,
1370 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1371 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1373 .vidioc_streamon = fimc_cap_streamon,
1374 .vidioc_streamoff = fimc_cap_streamoff,
1376 .vidioc_g_selection = fimc_cap_g_selection,
1377 .vidioc_s_selection = fimc_cap_s_selection,
1379 .vidioc_enum_input = fimc_cap_enum_input,
1380 .vidioc_s_input = fimc_cap_s_input,
1381 .vidioc_g_input = fimc_cap_g_input,
1384 /* Capture subdev media entity operations */
1385 static int fimc_link_setup(struct media_entity *entity,
1386 const struct media_pad *local,
1387 const struct media_pad *remote, u32 flags)
1389 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1390 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1391 struct fimc_vid_cap *vc = &fimc->vid_cap;
1392 struct v4l2_subdev *sensor;
1394 if (!is_media_entity_v4l2_subdev(remote->entity))
1397 if (WARN_ON(fimc == NULL))
1400 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1401 local->entity->name, remote->entity->name, flags,
1402 fimc->vid_cap.input);
1404 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
1405 fimc->vid_cap.input = 0;
1412 vc->input = sd->grp_id;
1414 if (vc->user_subdev_api || vc->inh_sensor_ctrls)
1417 /* Inherit V4L2 controls from the image sensor subdev. */
1418 sensor = fimc_find_remote_sensor(&vc->subdev.entity);
1422 return v4l2_ctrl_add_handler(&vc->ctx->ctrls.handler,
1423 sensor->ctrl_handler, NULL, true);
1426 static const struct media_entity_operations fimc_sd_media_ops = {
1427 .link_setup = fimc_link_setup,
1431 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1432 * @sd: pointer to a subdev generating the notification
1433 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1434 * @arg: pointer to an u32 type integer that stores the frame payload value
1436 * The End Of Frame notification sent by sensor subdev in its still capture
1437 * mode. If there is only a single VSYNC generated by the sensor at the
1438 * beginning of a frame transmission, FIMC does not issue the LastIrq
1439 * (end of frame) interrupt. And this notification is used to complete the
1440 * frame capture and returning a buffer to user-space. Subdev drivers should
1441 * call this notification from their last 'End of frame capture' interrupt.
1443 void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1446 struct fimc_source_info *si;
1447 struct fimc_vid_buffer *buf;
1448 struct fimc_md *fmd;
1449 struct fimc_dev *fimc;
1450 unsigned long flags;
1455 si = v4l2_get_subdev_hostdata(sd);
1456 fmd = entity_to_fimc_mdev(&sd->entity);
1458 spin_lock_irqsave(&fmd->slock, flags);
1460 fimc = si ? source_to_sensor_info(si)->host : NULL;
1462 if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1463 test_bit(ST_CAPT_PEND, &fimc->state)) {
1464 unsigned long irq_flags;
1465 spin_lock_irqsave(&fimc->slock, irq_flags);
1466 if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1467 buf = list_entry(fimc->vid_cap.active_buf_q.next,
1468 struct fimc_vid_buffer, list);
1469 vb2_set_plane_payload(&buf->vb.vb2_buf, 0,
1472 fimc_capture_irq_handler(fimc, 1);
1473 fimc_deactivate_capture(fimc);
1474 spin_unlock_irqrestore(&fimc->slock, irq_flags);
1476 spin_unlock_irqrestore(&fmd->slock, flags);
1479 static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1480 struct v4l2_subdev_pad_config *cfg,
1481 struct v4l2_subdev_mbus_code_enum *code)
1483 struct fimc_fmt *fmt;
1485 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1488 code->code = fmt->mbus_code;
1492 static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
1493 struct v4l2_subdev_pad_config *cfg,
1494 struct v4l2_subdev_format *fmt)
1496 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1497 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1498 struct fimc_frame *ff = &ctx->s_frame;
1499 struct v4l2_mbus_framefmt *mf;
1501 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1502 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
1508 mutex_lock(&fimc->lock);
1511 case FIMC_SD_PAD_SOURCE:
1512 if (!WARN_ON(ff->fmt == NULL))
1513 mf->code = ff->fmt->mbus_code;
1514 /* Sink pads crop rectangle size */
1515 mf->width = ff->width;
1516 mf->height = ff->height;
1518 case FIMC_SD_PAD_SINK_FIFO:
1519 *mf = fimc->vid_cap.wb_fmt;
1521 case FIMC_SD_PAD_SINK_CAM:
1523 *mf = fimc->vid_cap.ci_fmt;
1527 mutex_unlock(&fimc->lock);
1528 mf->colorspace = V4L2_COLORSPACE_JPEG;
1533 static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
1534 struct v4l2_subdev_pad_config *cfg,
1535 struct v4l2_subdev_format *fmt)
1537 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1538 struct v4l2_mbus_framefmt *mf = &fmt->format;
1539 struct fimc_vid_cap *vc = &fimc->vid_cap;
1540 struct fimc_ctx *ctx = vc->ctx;
1541 struct fimc_frame *ff;
1542 struct fimc_fmt *ffmt;
1544 dbg("pad%d: code: 0x%x, %dx%d",
1545 fmt->pad, mf->code, mf->width, mf->height);
1547 if (fmt->pad == FIMC_SD_PAD_SOURCE && vb2_is_busy(&vc->vbq))
1550 mutex_lock(&fimc->lock);
1551 ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1552 &mf->code, NULL, fmt->pad);
1553 mutex_unlock(&fimc->lock);
1554 mf->colorspace = V4L2_COLORSPACE_JPEG;
1556 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1557 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
1561 /* There must be a bug in the driver if this happens */
1562 if (WARN_ON(ffmt == NULL))
1565 /* Update RGB Alpha control state and value range */
1566 fimc_alpha_ctrl_update(ctx);
1568 fimc_capture_mark_jpeg_xfer(ctx, ffmt->color);
1569 if (fmt->pad == FIMC_SD_PAD_SOURCE) {
1571 /* Sink pads crop rectangle size */
1572 mf->width = ctx->s_frame.width;
1573 mf->height = ctx->s_frame.height;
1578 mutex_lock(&fimc->lock);
1579 set_frame_bounds(ff, mf->width, mf->height);
1581 if (fmt->pad == FIMC_SD_PAD_SINK_FIFO)
1583 else if (fmt->pad == FIMC_SD_PAD_SINK_CAM)
1588 /* Reset the crop rectangle if required. */
1589 if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_COMPOSE)))
1590 set_frame_crop(ff, 0, 0, mf->width, mf->height);
1592 if (fmt->pad != FIMC_SD_PAD_SOURCE)
1593 ctx->state &= ~FIMC_COMPOSE;
1595 mutex_unlock(&fimc->lock);
1599 static int fimc_subdev_get_selection(struct v4l2_subdev *sd,
1600 struct v4l2_subdev_pad_config *cfg,
1601 struct v4l2_subdev_selection *sel)
1603 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1604 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1605 struct fimc_frame *f = &ctx->s_frame;
1606 struct v4l2_rect *r = &sel->r;
1607 struct v4l2_rect *try_sel;
1609 if (sel->pad == FIMC_SD_PAD_SOURCE)
1612 mutex_lock(&fimc->lock);
1614 switch (sel->target) {
1615 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1618 case V4L2_SEL_TGT_CROP_BOUNDS:
1619 r->width = f->o_width;
1620 r->height = f->o_height;
1623 mutex_unlock(&fimc->lock);
1626 case V4L2_SEL_TGT_CROP:
1627 try_sel = v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
1629 case V4L2_SEL_TGT_COMPOSE:
1630 try_sel = v4l2_subdev_get_try_compose(sd, cfg, sel->pad);
1634 mutex_unlock(&fimc->lock);
1638 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1641 r->left = f->offs_h;
1643 r->width = f->width;
1644 r->height = f->height;
1647 dbg("target %#x: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1648 sel->pad, r->left, r->top, r->width, r->height,
1649 f->f_width, f->f_height);
1651 mutex_unlock(&fimc->lock);
1655 static int fimc_subdev_set_selection(struct v4l2_subdev *sd,
1656 struct v4l2_subdev_pad_config *cfg,
1657 struct v4l2_subdev_selection *sel)
1659 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1660 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1661 struct fimc_frame *f = &ctx->s_frame;
1662 struct v4l2_rect *r = &sel->r;
1663 struct v4l2_rect *try_sel;
1664 unsigned long flags;
1666 if (sel->pad == FIMC_SD_PAD_SOURCE)
1669 mutex_lock(&fimc->lock);
1670 fimc_capture_try_selection(ctx, r, V4L2_SEL_TGT_CROP);
1672 switch (sel->target) {
1673 case V4L2_SEL_TGT_CROP:
1674 try_sel = v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
1676 case V4L2_SEL_TGT_COMPOSE:
1677 try_sel = v4l2_subdev_get_try_compose(sd, cfg, sel->pad);
1681 mutex_unlock(&fimc->lock);
1685 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1688 spin_lock_irqsave(&fimc->slock, flags);
1689 set_frame_crop(f, r->left, r->top, r->width, r->height);
1690 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1691 if (sel->target == V4L2_SEL_TGT_COMPOSE)
1692 ctx->state |= FIMC_COMPOSE;
1693 spin_unlock_irqrestore(&fimc->slock, flags);
1696 dbg("target %#x: (%d,%d)/%dx%d", sel->target, r->left, r->top,
1697 r->width, r->height);
1699 mutex_unlock(&fimc->lock);
1703 static const struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1704 .enum_mbus_code = fimc_subdev_enum_mbus_code,
1705 .get_selection = fimc_subdev_get_selection,
1706 .set_selection = fimc_subdev_set_selection,
1707 .get_fmt = fimc_subdev_get_fmt,
1708 .set_fmt = fimc_subdev_set_fmt,
1711 static const struct v4l2_subdev_ops fimc_subdev_ops = {
1712 .pad = &fimc_subdev_pad_ops,
1715 /* Set default format at the sensor and host interface */
1716 static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1718 struct v4l2_format fmt = {
1719 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1721 .width = FIMC_DEFAULT_WIDTH,
1722 .height = FIMC_DEFAULT_HEIGHT,
1723 .pixelformat = V4L2_PIX_FMT_YUYV,
1724 .field = V4L2_FIELD_NONE,
1725 .colorspace = V4L2_COLORSPACE_JPEG,
1729 return __fimc_capture_set_format(fimc, &fmt);
1732 /* fimc->lock must be already initialized */
1733 static int fimc_register_capture_device(struct fimc_dev *fimc,
1734 struct v4l2_device *v4l2_dev)
1736 struct video_device *vfd = &fimc->vid_cap.ve.vdev;
1737 struct vb2_queue *q = &fimc->vid_cap.vbq;
1738 struct fimc_ctx *ctx;
1739 struct fimc_vid_cap *vid_cap;
1740 struct fimc_fmt *fmt;
1743 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1747 ctx->fimc_dev = fimc;
1748 ctx->in_path = FIMC_IO_CAMERA;
1749 ctx->out_path = FIMC_IO_DMA;
1750 ctx->state = FIMC_CTX_CAP;
1751 ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1752 ctx->d_frame.fmt = ctx->s_frame.fmt;
1754 memset(vfd, 0, sizeof(*vfd));
1755 snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.capture", fimc->id);
1757 vfd->fops = &fimc_capture_fops;
1758 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
1759 vfd->v4l2_dev = v4l2_dev;
1761 vfd->release = video_device_release_empty;
1763 vfd->lock = &fimc->lock;
1764 vfd->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
1766 video_set_drvdata(vfd, fimc);
1767 vid_cap = &fimc->vid_cap;
1768 vid_cap->active_buf_cnt = 0;
1769 vid_cap->reqbufs_count = 0;
1772 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1773 INIT_LIST_HEAD(&vid_cap->active_buf_q);
1775 memset(q, 0, sizeof(*q));
1776 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1777 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1779 q->ops = &fimc_capture_qops;
1780 q->mem_ops = &vb2_dma_contig_memops;
1781 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1782 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1783 q->lock = &fimc->lock;
1784 q->dev = &fimc->pdev->dev;
1786 ret = vb2_queue_init(q);
1790 /* Default format configuration */
1791 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1792 vid_cap->ci_fmt.width = FIMC_DEFAULT_WIDTH;
1793 vid_cap->ci_fmt.height = FIMC_DEFAULT_HEIGHT;
1794 vid_cap->ci_fmt.code = fmt->mbus_code;
1796 ctx->s_frame.width = FIMC_DEFAULT_WIDTH;
1797 ctx->s_frame.height = FIMC_DEFAULT_HEIGHT;
1798 ctx->s_frame.fmt = fmt;
1800 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_WRITEBACK, 0);
1801 vid_cap->wb_fmt = vid_cap->ci_fmt;
1802 vid_cap->wb_fmt.code = fmt->mbus_code;
1804 vid_cap->vd_pad.flags = MEDIA_PAD_FL_SINK;
1805 vfd->entity.function = MEDIA_ENT_F_PROC_VIDEO_SCALER;
1806 ret = media_entity_pads_init(&vfd->entity, 1, &vid_cap->vd_pad);
1810 ret = fimc_ctrls_create(ctx);
1812 goto err_me_cleanup;
1814 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
1818 v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
1819 vfd->name, video_device_node_name(vfd));
1821 vfd->ctrl_handler = &ctx->ctrls.handler;
1825 fimc_ctrls_delete(ctx);
1827 media_entity_cleanup(&vfd->entity);
1833 static int fimc_capture_subdev_registered(struct v4l2_subdev *sd)
1835 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1841 ret = fimc_register_m2m_device(fimc, sd->v4l2_dev);
1845 fimc->vid_cap.ve.pipe = v4l2_get_subdev_hostdata(sd);
1847 ret = fimc_register_capture_device(fimc, sd->v4l2_dev);
1849 fimc_unregister_m2m_device(fimc);
1850 fimc->vid_cap.ve.pipe = NULL;
1856 static void fimc_capture_subdev_unregistered(struct v4l2_subdev *sd)
1858 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1859 struct video_device *vdev;
1864 mutex_lock(&fimc->lock);
1866 fimc_unregister_m2m_device(fimc);
1867 vdev = &fimc->vid_cap.ve.vdev;
1869 if (video_is_registered(vdev)) {
1870 video_unregister_device(vdev);
1871 media_entity_cleanup(&vdev->entity);
1872 fimc_ctrls_delete(fimc->vid_cap.ctx);
1873 fimc->vid_cap.ve.pipe = NULL;
1875 kfree(fimc->vid_cap.ctx);
1876 fimc->vid_cap.ctx = NULL;
1878 mutex_unlock(&fimc->lock);
1881 static const struct v4l2_subdev_internal_ops fimc_capture_sd_internal_ops = {
1882 .registered = fimc_capture_subdev_registered,
1883 .unregistered = fimc_capture_subdev_unregistered,
1886 int fimc_initialize_capture_subdev(struct fimc_dev *fimc)
1888 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1891 v4l2_subdev_init(sd, &fimc_subdev_ops);
1892 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1893 snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->id);
1895 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK_CAM].flags = MEDIA_PAD_FL_SINK;
1896 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK_FIFO].flags = MEDIA_PAD_FL_SINK;
1897 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1898 ret = media_entity_pads_init(&sd->entity, FIMC_SD_PADS_NUM,
1899 fimc->vid_cap.sd_pads);
1903 sd->entity.ops = &fimc_sd_media_ops;
1904 sd->internal_ops = &fimc_capture_sd_internal_ops;
1905 v4l2_set_subdevdata(sd, fimc);
1909 void fimc_unregister_capture_subdev(struct fimc_dev *fimc)
1911 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1913 v4l2_device_unregister_subdev(sd);
1914 media_entity_cleanup(&sd->entity);
1915 v4l2_set_subdevdata(sd, NULL);