Merge branch 'v4l_for_linus' into staging/for_v3.6
[sfrench/cifs-2.6.git] / drivers / media / video / s5p-fimc / fimc-core.c
index e184e650022a8a6b526b0eb0a254ebf9ed446bc0..d083ec21f29a644fc904ee4292f8511f39986e08 100644 (file)
@@ -1,8 +1,8 @@
 /*
- * Samsung S5P/EXYNOS4 SoC series camera interface (video postprocessor) driver
+ * Samsung S5P/EXYNOS4 SoC series FIMC (CAMIF) driver
  *
- * Copyright (C) 2010-2011 Samsung Electronics Co., Ltd.
- * Contact: Sylwester Nawrocki, <s.nawrocki@samsung.com>
+ * Copyright (C) 2010-2012 Samsung Electronics Co., Ltd.
+ * Sylwester Nawrocki <s.nawrocki@samsung.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published
@@ -28,6 +28,7 @@
 #include <media/videobuf2-dma-contig.h>
 
 #include "fimc-core.h"
+#include "fimc-reg.h"
 #include "fimc-mdevice.h"
 
 static char *fimc_clocks[MAX_FIMC_CLOCKS] = {
@@ -39,7 +40,7 @@ static struct fimc_fmt fimc_formats[] = {
                .name           = "RGB565",
                .fourcc         = V4L2_PIX_FMT_RGB565,
                .depth          = { 16 },
-               .color          = S5P_FIMC_RGB565,
+               .color          = FIMC_FMT_RGB565,
                .memplanes      = 1,
                .colplanes      = 1,
                .flags          = FMT_FLAGS_M2M,
@@ -47,7 +48,7 @@ static struct fimc_fmt fimc_formats[] = {
                .name           = "BGR666",
                .fourcc         = V4L2_PIX_FMT_BGR666,
                .depth          = { 32 },
-               .color          = S5P_FIMC_RGB666,
+               .color          = FIMC_FMT_RGB666,
                .memplanes      = 1,
                .colplanes      = 1,
                .flags          = FMT_FLAGS_M2M,
@@ -55,7 +56,7 @@ static struct fimc_fmt fimc_formats[] = {
                .name           = "ARGB8888, 32 bpp",
                .fourcc         = V4L2_PIX_FMT_RGB32,
                .depth          = { 32 },
-               .color          = S5P_FIMC_RGB888,
+               .color          = FIMC_FMT_RGB888,
                .memplanes      = 1,
                .colplanes      = 1,
                .flags          = FMT_FLAGS_M2M | FMT_HAS_ALPHA,
@@ -63,7 +64,7 @@ static struct fimc_fmt fimc_formats[] = {
                .name           = "ARGB1555",
                .fourcc         = V4L2_PIX_FMT_RGB555,
                .depth          = { 16 },
-               .color          = S5P_FIMC_RGB555,
+               .color          = FIMC_FMT_RGB555,
                .memplanes      = 1,
                .colplanes      = 1,
                .flags          = FMT_FLAGS_M2M_OUT | FMT_HAS_ALPHA,
@@ -71,7 +72,7 @@ static struct fimc_fmt fimc_formats[] = {
                .name           = "ARGB4444",
                .fourcc         = V4L2_PIX_FMT_RGB444,
                .depth          = { 16 },
-               .color          = S5P_FIMC_RGB444,
+               .color          = FIMC_FMT_RGB444,
                .memplanes      = 1,
                .colplanes      = 1,
                .flags          = FMT_FLAGS_M2M_OUT | FMT_HAS_ALPHA,
@@ -79,7 +80,7 @@ static struct fimc_fmt fimc_formats[] = {
                .name           = "YUV 4:2:2 packed, YCbYCr",
                .fourcc         = V4L2_PIX_FMT_YUYV,
                .depth          = { 16 },
-               .color          = S5P_FIMC_YCBYCR422,
+               .color          = FIMC_FMT_YCBYCR422,
                .memplanes      = 1,
                .colplanes      = 1,
                .mbus_code      = V4L2_MBUS_FMT_YUYV8_2X8,
@@ -88,7 +89,7 @@ static struct fimc_fmt fimc_formats[] = {
                .name           = "YUV 4:2:2 packed, CbYCrY",
                .fourcc         = V4L2_PIX_FMT_UYVY,
                .depth          = { 16 },
-               .color          = S5P_FIMC_CBYCRY422,
+               .color          = FIMC_FMT_CBYCRY422,
                .memplanes      = 1,
                .colplanes      = 1,
                .mbus_code      = V4L2_MBUS_FMT_UYVY8_2X8,
@@ -97,7 +98,7 @@ static struct fimc_fmt fimc_formats[] = {
                .name           = "YUV 4:2:2 packed, CrYCbY",
                .fourcc         = V4L2_PIX_FMT_VYUY,
                .depth          = { 16 },
-               .color          = S5P_FIMC_CRYCBY422,
+               .color          = FIMC_FMT_CRYCBY422,
                .memplanes      = 1,
                .colplanes      = 1,
                .mbus_code      = V4L2_MBUS_FMT_VYUY8_2X8,
@@ -106,7 +107,7 @@ static struct fimc_fmt fimc_formats[] = {
                .name           = "YUV 4:2:2 packed, YCrYCb",
                .fourcc         = V4L2_PIX_FMT_YVYU,
                .depth          = { 16 },
-               .color          = S5P_FIMC_YCRYCB422,
+               .color          = FIMC_FMT_YCRYCB422,
                .memplanes      = 1,
                .colplanes      = 1,
                .mbus_code      = V4L2_MBUS_FMT_YVYU8_2X8,
@@ -115,7 +116,7 @@ static struct fimc_fmt fimc_formats[] = {
                .name           = "YUV 4:2:2 planar, Y/Cb/Cr",
                .fourcc         = V4L2_PIX_FMT_YUV422P,
                .depth          = { 12 },
-               .color          = S5P_FIMC_YCBYCR422,
+               .color          = FIMC_FMT_YCBYCR422,
                .memplanes      = 1,
                .colplanes      = 3,
                .flags          = FMT_FLAGS_M2M,
@@ -123,7 +124,7 @@ static struct fimc_fmt fimc_formats[] = {
                .name           = "YUV 4:2:2 planar, Y/CbCr",
                .fourcc         = V4L2_PIX_FMT_NV16,
                .depth          = { 16 },
-               .color          = S5P_FIMC_YCBYCR422,
+               .color          = FIMC_FMT_YCBYCR422,
                .memplanes      = 1,
                .colplanes      = 2,
                .flags          = FMT_FLAGS_M2M,
@@ -131,7 +132,7 @@ static struct fimc_fmt fimc_formats[] = {
                .name           = "YUV 4:2:2 planar, Y/CrCb",
                .fourcc         = V4L2_PIX_FMT_NV61,
                .depth          = { 16 },
-               .color          = S5P_FIMC_YCRYCB422,
+               .color          = FIMC_FMT_YCRYCB422,
                .memplanes      = 1,
                .colplanes      = 2,
                .flags          = FMT_FLAGS_M2M,
@@ -139,7 +140,7 @@ static struct fimc_fmt fimc_formats[] = {
                .name           = "YUV 4:2:0 planar, YCbCr",
                .fourcc         = V4L2_PIX_FMT_YUV420,
                .depth          = { 12 },
-               .color          = S5P_FIMC_YCBCR420,
+               .color          = FIMC_FMT_YCBCR420,
                .memplanes      = 1,
                .colplanes      = 3,
                .flags          = FMT_FLAGS_M2M,
@@ -147,14 +148,14 @@ static struct fimc_fmt fimc_formats[] = {
                .name           = "YUV 4:2:0 planar, Y/CbCr",
                .fourcc         = V4L2_PIX_FMT_NV12,
                .depth          = { 12 },
-               .color          = S5P_FIMC_YCBCR420,
+               .color          = FIMC_FMT_YCBCR420,
                .memplanes      = 1,
                .colplanes      = 2,
                .flags          = FMT_FLAGS_M2M,
        }, {
                .name           = "YUV 4:2:0 non-contiguous 2-planar, Y/CbCr",
                .fourcc         = V4L2_PIX_FMT_NV12M,
-               .color          = S5P_FIMC_YCBCR420,
+               .color          = FIMC_FMT_YCBCR420,
                .depth          = { 8, 4 },
                .memplanes      = 2,
                .colplanes      = 2,
@@ -162,7 +163,7 @@ static struct fimc_fmt fimc_formats[] = {
        }, {
                .name           = "YUV 4:2:0 non-contiguous 3-planar, Y/Cb/Cr",
                .fourcc         = V4L2_PIX_FMT_YUV420M,
-               .color          = S5P_FIMC_YCBCR420,
+               .color          = FIMC_FMT_YCBCR420,
                .depth          = { 8, 2, 2 },
                .memplanes      = 3,
                .colplanes      = 3,
@@ -170,7 +171,7 @@ static struct fimc_fmt fimc_formats[] = {
        }, {
                .name           = "YUV 4:2:0 non-contiguous 2-planar, Y/CbCr, tiled",
                .fourcc         = V4L2_PIX_FMT_NV12MT,
-               .color          = S5P_FIMC_YCBCR420,
+               .color          = FIMC_FMT_YCBCR420,
                .depth          = { 8, 4 },
                .memplanes      = 2,
                .colplanes      = 2,
@@ -178,7 +179,7 @@ static struct fimc_fmt fimc_formats[] = {
        }, {
                .name           = "JPEG encoded data",
                .fourcc         = V4L2_PIX_FMT_JPEG,
-               .color          = S5P_FIMC_JPEG,
+               .color          = FIMC_FMT_JPEG,
                .depth          = { 8 },
                .memplanes      = 1,
                .colplanes      = 1,
@@ -187,12 +188,12 @@ static struct fimc_fmt fimc_formats[] = {
        },
 };
 
-static unsigned int get_m2m_fmt_flags(unsigned int stream_type)
+struct fimc_fmt *fimc_get_format(unsigned int index)
 {
-       if (stream_type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
-               return FMT_FLAGS_M2M_IN;
-       else
-               return FMT_FLAGS_M2M_OUT;
+       if (index >= ARRAY_SIZE(fimc_formats))
+               return NULL;
+
+       return &fimc_formats[index];
 }
 
 int fimc_check_scaler_ratio(struct fimc_ctx *ctx, int sw, int sh,
@@ -230,7 +231,7 @@ static int fimc_get_scaler_factor(u32 src, u32 tar, u32 *ratio, u32 *shift)
 
 int fimc_set_scaler_info(struct fimc_ctx *ctx)
 {
-       struct samsung_fimc_variant *variant = ctx->fimc_dev->variant;
+       struct fimc_variant *variant = ctx->fimc_dev->variant;
        struct device *dev = &ctx->fimc_dev->pdev->dev;
        struct fimc_scaler *sc = &ctx->scaler;
        struct fimc_frame *s_frame = &ctx->s_frame;
@@ -293,126 +294,9 @@ int fimc_set_scaler_info(struct fimc_ctx *ctx)
        return 0;
 }
 
-static void fimc_m2m_job_finish(struct fimc_ctx *ctx, int vb_state)
-{
-       struct vb2_buffer *src_vb, *dst_vb;
-
-       if (!ctx || !ctx->m2m_ctx)
-               return;
-
-       src_vb = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
-       dst_vb = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
-
-       if (src_vb && dst_vb) {
-               v4l2_m2m_buf_done(src_vb, vb_state);
-               v4l2_m2m_buf_done(dst_vb, vb_state);
-               v4l2_m2m_job_finish(ctx->fimc_dev->m2m.m2m_dev,
-                                   ctx->m2m_ctx);
-       }
-}
-
-/* Complete the transaction which has been scheduled for execution. */
-static int fimc_m2m_shutdown(struct fimc_ctx *ctx)
-{
-       struct fimc_dev *fimc = ctx->fimc_dev;
-       int ret;
-
-       if (!fimc_m2m_pending(fimc))
-               return 0;
-
-       fimc_ctx_state_lock_set(FIMC_CTX_SHUT, ctx);
-
-       ret = wait_event_timeout(fimc->irq_queue,
-                          !fimc_ctx_state_is_set(FIMC_CTX_SHUT, ctx),
-                          FIMC_SHUTDOWN_TIMEOUT);
-
-       return ret == 0 ? -ETIMEDOUT : ret;
-}
-
-static int start_streaming(struct vb2_queue *q, unsigned int count)
-{
-       struct fimc_ctx *ctx = q->drv_priv;
-       int ret;
-
-       ret = pm_runtime_get_sync(&ctx->fimc_dev->pdev->dev);
-       return ret > 0 ? 0 : ret;
-}
-
-static int stop_streaming(struct vb2_queue *q)
-{
-       struct fimc_ctx *ctx = q->drv_priv;
-       int ret;
-
-       ret = fimc_m2m_shutdown(ctx);
-       if (ret == -ETIMEDOUT)
-               fimc_m2m_job_finish(ctx, VB2_BUF_STATE_ERROR);
-
-       pm_runtime_put(&ctx->fimc_dev->pdev->dev);
-       return 0;
-}
-
-void fimc_capture_irq_handler(struct fimc_dev *fimc, bool final)
-{
-       struct fimc_vid_cap *cap = &fimc->vid_cap;
-       struct fimc_vid_buffer *v_buf;
-       struct timeval *tv;
-       struct timespec ts;
-
-       if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
-               wake_up(&fimc->irq_queue);
-               return;
-       }
-
-       if (!list_empty(&cap->active_buf_q) &&
-           test_bit(ST_CAPT_RUN, &fimc->state) && final) {
-               ktime_get_real_ts(&ts);
-
-               v_buf = fimc_active_queue_pop(cap);
-
-               tv = &v_buf->vb.v4l2_buf.timestamp;
-               tv->tv_sec = ts.tv_sec;
-               tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
-               v_buf->vb.v4l2_buf.sequence = cap->frame_count++;
-
-               vb2_buffer_done(&v_buf->vb, VB2_BUF_STATE_DONE);
-       }
-
-       if (!list_empty(&cap->pending_buf_q)) {
-
-               v_buf = fimc_pending_queue_pop(cap);
-               fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
-               v_buf->index = cap->buf_index;
-
-               /* Move the buffer to the capture active queue */
-               fimc_active_queue_add(cap, v_buf);
-
-               dbg("next frame: %d, done frame: %d",
-                   fimc_hw_get_frame_index(fimc), v_buf->index);
-
-               if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
-                       cap->buf_index = 0;
-       }
-
-       if (cap->active_buf_cnt == 0) {
-               if (final)
-                       clear_bit(ST_CAPT_RUN, &fimc->state);
-
-               if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
-                       cap->buf_index = 0;
-       } else {
-               set_bit(ST_CAPT_RUN, &fimc->state);
-       }
-
-       fimc_capture_config_update(cap->ctx);
-
-       dbg("frame: %d, active_buf_cnt: %d",
-           fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
-}
-
 static irqreturn_t fimc_irq_handler(int irq, void *priv)
 {
        struct fimc_dev *fimc = priv;
-       struct fimc_vid_cap *cap = &fimc->vid_cap;
        struct fimc_ctx *ctx;
 
        fimc_hw_clear_irq(fimc);
@@ -430,21 +314,16 @@ static irqreturn_t fimc_irq_handler(int irq, void *priv)
                        spin_unlock(&fimc->slock);
                        fimc_m2m_job_finish(ctx, VB2_BUF_STATE_DONE);
 
-                       spin_lock(&ctx->slock);
                        if (ctx->state & FIMC_CTX_SHUT) {
                                ctx->state &= ~FIMC_CTX_SHUT;
                                wake_up(&fimc->irq_queue);
                        }
-                       spin_unlock(&ctx->slock);
+                       return IRQ_HANDLED;
                }
-               return IRQ_HANDLED;
        } else if (test_bit(ST_CAPT_PEND, &fimc->state)) {
-               fimc_capture_irq_handler(fimc,
-                                !test_bit(ST_CAPT_JPEG, &fimc->state));
-               if (cap->active_buf_cnt == 1) {
-                       fimc_deactivate_capture(fimc);
-                       clear_bit(ST_CAPT_STREAM, &fimc->state);
-               }
+               int last_buf = test_bit(ST_CAPT_JPEG, &fimc->state) &&
+                               fimc->vid_cap.reqbufs_count == 1;
+               fimc_capture_irq_handler(fimc, !last_buf);
        }
 out:
        spin_unlock(&fimc->slock);
@@ -482,7 +361,7 @@ int fimc_prepare_addr(struct fimc_ctx *ctx, struct vb2_buffer *vb,
                case 3:
                        paddr->cb = (u32)(paddr->y + pix_size);
                        /* decompose Y into Y/Cb/Cr */
-                       if (S5P_FIMC_YCBCR420 == frame->fmt->color)
+                       if (FIMC_FMT_YCBCR420 == frame->fmt->color)
                                paddr->cr = (u32)(paddr->cb
                                                + (pix_size >> 2));
                        else /* 422 */
@@ -510,40 +389,40 @@ int fimc_prepare_addr(struct fimc_ctx *ctx, struct vb2_buffer *vb,
 void fimc_set_yuv_order(struct fimc_ctx *ctx)
 {
        /* The one only mode supported in SoC. */
-       ctx->in_order_2p = S5P_FIMC_LSB_CRCB;
-       ctx->out_order_2p = S5P_FIMC_LSB_CRCB;
+       ctx->in_order_2p = FIMC_REG_CIOCTRL_ORDER422_2P_LSB_CRCB;
+       ctx->out_order_2p = FIMC_REG_CIOCTRL_ORDER422_2P_LSB_CRCB;
 
        /* Set order for 1 plane input formats. */
        switch (ctx->s_frame.fmt->color) {
-       case S5P_FIMC_YCRYCB422:
-               ctx->in_order_1p = S5P_MSCTRL_ORDER422_CBYCRY;
+       case FIMC_FMT_YCRYCB422:
+               ctx->in_order_1p = FIMC_REG_MSCTRL_ORDER422_CBYCRY;
                break;
-       case S5P_FIMC_CBYCRY422:
-               ctx->in_order_1p = S5P_MSCTRL_ORDER422_YCRYCB;
+       case FIMC_FMT_CBYCRY422:
+               ctx->in_order_1p = FIMC_REG_MSCTRL_ORDER422_YCRYCB;
                break;
-       case S5P_FIMC_CRYCBY422:
-               ctx->in_order_1p = S5P_MSCTRL_ORDER422_YCBYCR;
+       case FIMC_FMT_CRYCBY422:
+               ctx->in_order_1p = FIMC_REG_MSCTRL_ORDER422_YCBYCR;
                break;
-       case S5P_FIMC_YCBYCR422:
+       case FIMC_FMT_YCBYCR422:
        default:
-               ctx->in_order_1p = S5P_MSCTRL_ORDER422_CRYCBY;
+               ctx->in_order_1p = FIMC_REG_MSCTRL_ORDER422_CRYCBY;
                break;
        }
        dbg("ctx->in_order_1p= %d", ctx->in_order_1p);
 
        switch (ctx->d_frame.fmt->color) {
-       case S5P_FIMC_YCRYCB422:
-               ctx->out_order_1p = S5P_CIOCTRL_ORDER422_CBYCRY;
+       case FIMC_FMT_YCRYCB422:
+               ctx->out_order_1p = FIMC_REG_CIOCTRL_ORDER422_CBYCRY;
                break;
-       case S5P_FIMC_CBYCRY422:
-               ctx->out_order_1p = S5P_CIOCTRL_ORDER422_YCRYCB;
+       case FIMC_FMT_CBYCRY422:
+               ctx->out_order_1p = FIMC_REG_CIOCTRL_ORDER422_YCRYCB;
                break;
-       case S5P_FIMC_CRYCBY422:
-               ctx->out_order_1p = S5P_CIOCTRL_ORDER422_YCBYCR;
+       case FIMC_FMT_CRYCBY422:
+               ctx->out_order_1p = FIMC_REG_CIOCTRL_ORDER422_YCBYCR;
                break;
-       case S5P_FIMC_YCBYCR422:
+       case FIMC_FMT_YCBYCR422:
        default:
-               ctx->out_order_1p = S5P_CIOCTRL_ORDER422_CRYCBY;
+               ctx->out_order_1p = FIMC_REG_CIOCTRL_ORDER422_CRYCBY;
                break;
        }
        dbg("ctx->out_order_1p= %d", ctx->out_order_1p);
@@ -551,7 +430,7 @@ void fimc_set_yuv_order(struct fimc_ctx *ctx)
 
 void fimc_prepare_dma_offset(struct fimc_ctx *ctx, struct fimc_frame *f)
 {
-       struct samsung_fimc_variant *variant = ctx->fimc_dev->variant;
+       struct fimc_variant *variant = ctx->fimc_dev->variant;
        u32 i, depth = 0;
 
        for (i = 0; i < f->fmt->colplanes; i++)
@@ -574,7 +453,7 @@ void fimc_prepare_dma_offset(struct fimc_ctx *ctx, struct fimc_frame *f)
                        f->dma_offset.cb_h >>= 1;
                        f->dma_offset.cr_h >>= 1;
                }
-               if (f->fmt->color == S5P_FIMC_YCBCR420) {
+               if (f->fmt->color == FIMC_FMT_YCBCR420) {
                        f->dma_offset.cb_v >>= 1;
                        f->dma_offset.cr_v >>= 1;
                }
@@ -584,203 +463,58 @@ void fimc_prepare_dma_offset(struct fimc_ctx *ctx, struct fimc_frame *f)
            f->fmt->color, f->dma_offset.y_h, f->dma_offset.y_v);
 }
 
-/**
- * fimc_prepare_config - check dimensions, operation and color mode
- *                      and pre-calculate offset and the scaling coefficients.
- *
- * @ctx: hardware context information
- * @flags: flags indicating which parameters to check/update
- *
- * Return: 0 if dimensions are valid or non zero otherwise.
- */
-int fimc_prepare_config(struct fimc_ctx *ctx, u32 flags)
-{
-       struct fimc_frame *s_frame, *d_frame;
-       struct vb2_buffer *vb = NULL;
-       int ret = 0;
-
-       s_frame = &ctx->s_frame;
-       d_frame = &ctx->d_frame;
-
-       if (flags & FIMC_PARAMS) {
-               /* Prepare the DMA offset ratios for scaler. */
-               fimc_prepare_dma_offset(ctx, &ctx->s_frame);
-               fimc_prepare_dma_offset(ctx, &ctx->d_frame);
-
-               if (s_frame->height > (SCALER_MAX_VRATIO * d_frame->height) ||
-                   s_frame->width > (SCALER_MAX_HRATIO * d_frame->width)) {
-                       err("out of scaler range");
-                       return -EINVAL;
-               }
-               fimc_set_yuv_order(ctx);
-       }
-
-       if (flags & FIMC_SRC_ADDR) {
-               vb = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
-               ret = fimc_prepare_addr(ctx, vb, s_frame, &s_frame->paddr);
-               if (ret)
-                       return ret;
-       }
-
-       if (flags & FIMC_DST_ADDR) {
-               vb = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
-               ret = fimc_prepare_addr(ctx, vb, d_frame, &d_frame->paddr);
-       }
-
-       return ret;
-}
-
-static void fimc_dma_run(void *priv)
+static int fimc_set_color_effect(struct fimc_ctx *ctx, enum v4l2_colorfx colorfx)
 {
-       struct fimc_ctx *ctx = priv;
-       struct fimc_dev *fimc;
-       unsigned long flags;
-       u32 ret;
-
-       if (WARN(!ctx, "null hardware context\n"))
-               return;
-
-       fimc = ctx->fimc_dev;
-       spin_lock_irqsave(&fimc->slock, flags);
-       set_bit(ST_M2M_PEND, &fimc->state);
-
-       spin_lock(&ctx->slock);
-       ctx->state |= (FIMC_SRC_ADDR | FIMC_DST_ADDR);
-       ret = fimc_prepare_config(ctx, ctx->state);
-       if (ret)
-               goto dma_unlock;
+       struct fimc_effect *effect = &ctx->effect;
 
-       /* Reconfigure hardware if the context has changed. */
-       if (fimc->m2m.ctx != ctx) {
-               ctx->state |= FIMC_PARAMS;
-               fimc->m2m.ctx = ctx;
-       }
-       fimc_hw_set_input_addr(fimc, &ctx->s_frame.paddr);
-
-       if (ctx->state & FIMC_PARAMS) {
-               fimc_hw_set_input_path(ctx);
-               fimc_hw_set_in_dma(ctx);
-               ret = fimc_set_scaler_info(ctx);
-               if (ret) {
-                       spin_unlock(&fimc->slock);
-                       goto dma_unlock;
-               }
-               fimc_hw_set_prescaler(ctx);
-               fimc_hw_set_mainscaler(ctx);
-               fimc_hw_set_target_format(ctx);
-               fimc_hw_set_rotation(ctx);
-               fimc_hw_set_effect(ctx, false);
-       }
-
-       fimc_hw_set_output_path(ctx);
-       if (ctx->state & (FIMC_DST_ADDR | FIMC_PARAMS))
-               fimc_hw_set_output_addr(fimc, &ctx->d_frame.paddr, -1);
-
-       if (ctx->state & FIMC_PARAMS) {
-               fimc_hw_set_out_dma(ctx);
-               if (fimc->variant->has_alpha)
-                       fimc_hw_set_rgb_alpha(ctx);
-       }
-
-       fimc_activate_capture(ctx);
-
-       ctx->state &= (FIMC_CTX_M2M | FIMC_CTX_CAP |
-                      FIMC_SRC_FMT | FIMC_DST_FMT);
-       fimc_hw_activate_input_dma(fimc, true);
-dma_unlock:
-       spin_unlock(&ctx->slock);
-       spin_unlock_irqrestore(&fimc->slock, flags);
-}
-
-static void fimc_job_abort(void *priv)
-{
-       fimc_m2m_shutdown(priv);
-}
-
-static int fimc_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
-                           unsigned int *num_buffers, unsigned int *num_planes,
-                           unsigned int sizes[], void *allocators[])
-{
-       struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
-       struct fimc_frame *f;
-       int i;
-
-       f = ctx_get_frame(ctx, vq->type);
-       if (IS_ERR(f))
-               return PTR_ERR(f);
-       /*
-        * Return number of non-contigous planes (plane buffers)
-        * depending on the configured color format.
-        */
-       if (!f->fmt)
+       switch (colorfx) {
+       case V4L2_COLORFX_NONE:
+               effect->type = FIMC_REG_CIIMGEFF_FIN_BYPASS;
+               break;
+       case V4L2_COLORFX_BW:
+               effect->type = FIMC_REG_CIIMGEFF_FIN_ARBITRARY;
+               effect->pat_cb = 128;
+               effect->pat_cr = 128;
+               break;
+       case V4L2_COLORFX_SEPIA:
+               effect->type = FIMC_REG_CIIMGEFF_FIN_ARBITRARY;
+               effect->pat_cb = 115;
+               effect->pat_cr = 145;
+               break;
+       case V4L2_COLORFX_NEGATIVE:
+               effect->type = FIMC_REG_CIIMGEFF_FIN_NEGATIVE;
+               break;
+       case V4L2_COLORFX_EMBOSS:
+               effect->type = FIMC_REG_CIIMGEFF_FIN_EMBOSSING;
+               break;
+       case V4L2_COLORFX_ART_FREEZE:
+               effect->type = FIMC_REG_CIIMGEFF_FIN_ARTFREEZE;
+               break;
+       case V4L2_COLORFX_SILHOUETTE:
+               effect->type = FIMC_REG_CIIMGEFF_FIN_SILHOUETTE;
+               break;
+       case V4L2_COLORFX_SET_CBCR:
+               effect->type = FIMC_REG_CIIMGEFF_FIN_ARBITRARY;
+               effect->pat_cb = ctx->ctrls.colorfx_cbcr->val >> 8;
+               effect->pat_cr = ctx->ctrls.colorfx_cbcr->val & 0xff;
+               break;
+       default:
                return -EINVAL;
-
-       *num_planes = f->fmt->memplanes;
-       for (i = 0; i < f->fmt->memplanes; i++) {
-               sizes[i] = (f->f_width * f->f_height * f->fmt->depth[i]) / 8;
-               allocators[i] = ctx->fimc_dev->alloc_ctx;
        }
-       return 0;
-}
-
-static int fimc_buf_prepare(struct vb2_buffer *vb)
-{
-       struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
-       struct fimc_frame *frame;
-       int i;
-
-       frame = ctx_get_frame(ctx, vb->vb2_queue->type);
-       if (IS_ERR(frame))
-               return PTR_ERR(frame);
-
-       for (i = 0; i < frame->fmt->memplanes; i++)
-               vb2_set_plane_payload(vb, i, frame->payload[i]);
 
        return 0;
 }
 
-static void fimc_buf_queue(struct vb2_buffer *vb)
-{
-       struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
-
-       dbg("ctx: %p, ctx->state: 0x%x", ctx, ctx->state);
-
-       if (ctx->m2m_ctx)
-               v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
-}
-
-static void fimc_lock(struct vb2_queue *vq)
-{
-       struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
-       mutex_lock(&ctx->fimc_dev->lock);
-}
-
-static void fimc_unlock(struct vb2_queue *vq)
-{
-       struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
-       mutex_unlock(&ctx->fimc_dev->lock);
-}
-
-static struct vb2_ops fimc_qops = {
-       .queue_setup     = fimc_queue_setup,
-       .buf_prepare     = fimc_buf_prepare,
-       .buf_queue       = fimc_buf_queue,
-       .wait_prepare    = fimc_unlock,
-       .wait_finish     = fimc_lock,
-       .stop_streaming  = stop_streaming,
-       .start_streaming = start_streaming,
-};
-
 /*
  * V4L2 controls handling
  */
 #define ctrl_to_ctx(__ctrl) \
-       container_of((__ctrl)->handler, struct fimc_ctx, ctrl_handler)
+       container_of((__ctrl)->handler, struct fimc_ctx, ctrls.handler)
 
 static int __fimc_s_ctrl(struct fimc_ctx *ctx, struct v4l2_ctrl *ctrl)
 {
        struct fimc_dev *fimc = ctx->fimc_dev;
-       struct samsung_fimc_variant *variant = fimc->variant;
+       struct fimc_variant *variant = fimc->variant;
        unsigned int flags = FIMC_DST_FMT | FIMC_SRC_FMT;
        int ret = 0;
 
@@ -815,7 +549,14 @@ static int __fimc_s_ctrl(struct fimc_ctx *ctx, struct v4l2_ctrl *ctrl)
        case V4L2_CID_ALPHA_COMPONENT:
                ctx->d_frame.alpha = ctrl->val;
                break;
+
+       case V4L2_CID_COLORFX:
+               ret = fimc_set_color_effect(ctx, ctrl->val);
+               if (ret)
+                       return ret;
+               break;
        }
+
        ctx->state |= FIMC_PARAMS;
        set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
        return 0;
@@ -827,9 +568,9 @@ static int fimc_s_ctrl(struct v4l2_ctrl *ctrl)
        unsigned long flags;
        int ret;
 
-       spin_lock_irqsave(&ctx->slock, flags);
+       spin_lock_irqsave(&ctx->fimc_dev->slock, flags);
        ret = __fimc_s_ctrl(ctx, ctrl);
-       spin_unlock_irqrestore(&ctx->slock, flags);
+       spin_unlock_irqrestore(&ctx->fimc_dev->slock, flags);
 
        return ret;
 }
@@ -840,71 +581,93 @@ static const struct v4l2_ctrl_ops fimc_ctrl_ops = {
 
 int fimc_ctrls_create(struct fimc_ctx *ctx)
 {
-       struct samsung_fimc_variant *variant = ctx->fimc_dev->variant;
+       struct fimc_variant *variant = ctx->fimc_dev->variant;
        unsigned int max_alpha = fimc_get_alpha_mask(ctx->d_frame.fmt);
+       struct fimc_ctrls *ctrls = &ctx->ctrls;
+       struct v4l2_ctrl_handler *handler = &ctrls->handler;
 
-       if (ctx->ctrls_rdy)
+       if (ctx->ctrls.ready)
                return 0;
-       v4l2_ctrl_handler_init(&ctx->ctrl_handler, 4);
 
-       ctx->ctrl_rotate = v4l2_ctrl_new_std(&ctx->ctrl_handler, &fimc_ctrl_ops,
+       v4l2_ctrl_handler_init(handler, 6);
+
+       ctrls->rotate = v4l2_ctrl_new_std(handler, &fimc_ctrl_ops,
                                        V4L2_CID_ROTATE, 0, 270, 90, 0);
-       ctx->ctrl_hflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &fimc_ctrl_ops,
+       ctrls->hflip = v4l2_ctrl_new_std(handler, &fimc_ctrl_ops,
                                        V4L2_CID_HFLIP, 0, 1, 1, 0);
-       ctx->ctrl_vflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &fimc_ctrl_ops,
+       ctrls->vflip = v4l2_ctrl_new_std(handler, &fimc_ctrl_ops,
                                        V4L2_CID_VFLIP, 0, 1, 1, 0);
+
        if (variant->has_alpha)
-               ctx->ctrl_alpha = v4l2_ctrl_new_std(&ctx->ctrl_handler,
-                                   &fimc_ctrl_ops, V4L2_CID_ALPHA_COMPONENT,
-                                   0, max_alpha, 1, 0);
+               ctrls->alpha = v4l2_ctrl_new_std(handler, &fimc_ctrl_ops,
+                                       V4L2_CID_ALPHA_COMPONENT,
+                                       0, max_alpha, 1, 0);
        else
-               ctx->ctrl_alpha = NULL;
+               ctrls->alpha = NULL;
 
-       ctx->ctrls_rdy = ctx->ctrl_handler.error == 0;
+       ctrls->colorfx = v4l2_ctrl_new_std_menu(handler, &fimc_ctrl_ops,
+                               V4L2_CID_COLORFX, V4L2_COLORFX_SET_CBCR,
+                               ~0x983f, V4L2_COLORFX_NONE);
 
-       return ctx->ctrl_handler.error;
+       ctrls->colorfx_cbcr = v4l2_ctrl_new_std(handler, &fimc_ctrl_ops,
+                               V4L2_CID_COLORFX_CBCR, 0, 0xffff, 1, 0);
+
+       ctx->effect.type = FIMC_REG_CIIMGEFF_FIN_BYPASS;
+
+       if (!handler->error) {
+               v4l2_ctrl_cluster(2, &ctrls->colorfx);
+               ctrls->ready = true;
+       }
+
+       return handler->error;
 }
 
 void fimc_ctrls_delete(struct fimc_ctx *ctx)
 {
-       if (ctx->ctrls_rdy) {
-               v4l2_ctrl_handler_free(&ctx->ctrl_handler);
-               ctx->ctrls_rdy = false;
-               ctx->ctrl_alpha = NULL;
+       struct fimc_ctrls *ctrls = &ctx->ctrls;
+
+       if (ctrls->ready) {
+               v4l2_ctrl_handler_free(&ctrls->handler);
+               ctrls->ready = false;
+               ctrls->alpha = NULL;
        }
 }
 
 void fimc_ctrls_activate(struct fimc_ctx *ctx, bool active)
 {
        unsigned int has_alpha = ctx->d_frame.fmt->flags & FMT_HAS_ALPHA;
+       struct fimc_ctrls *ctrls = &ctx->ctrls;
 
-       if (!ctx->ctrls_rdy)
+       if (!ctrls->ready)
                return;
 
-       mutex_lock(&ctx->ctrl_handler.lock);
-       v4l2_ctrl_activate(ctx->ctrl_rotate, active);
-       v4l2_ctrl_activate(ctx->ctrl_hflip, active);
-       v4l2_ctrl_activate(ctx->ctrl_vflip, active);
-       if (ctx->ctrl_alpha)
-               v4l2_ctrl_activate(ctx->ctrl_alpha, active && has_alpha);
+       mutex_lock(&ctrls->handler.lock);
+       v4l2_ctrl_activate(ctrls->rotate, active);
+       v4l2_ctrl_activate(ctrls->hflip, active);
+       v4l2_ctrl_activate(ctrls->vflip, active);
+       v4l2_ctrl_activate(ctrls->colorfx, active);
+       if (ctrls->alpha)
+               v4l2_ctrl_activate(ctrls->alpha, active && has_alpha);
 
        if (active) {
-               ctx->rotation = ctx->ctrl_rotate->val;
-               ctx->hflip    = ctx->ctrl_hflip->val;
-               ctx->vflip    = ctx->ctrl_vflip->val;
+               fimc_set_color_effect(ctx, ctrls->colorfx->cur.val);
+               ctx->rotation = ctrls->rotate->val;
+               ctx->hflip    = ctrls->hflip->val;
+               ctx->vflip    = ctrls->vflip->val;
        } else {
+               ctx->effect.type = FIMC_REG_CIIMGEFF_FIN_BYPASS;
                ctx->rotation = 0;
                ctx->hflip    = 0;
                ctx->vflip    = 0;
        }
-       mutex_unlock(&ctx->ctrl_handler.lock);
+       mutex_unlock(&ctrls->handler.lock);
 }
 
 /* Update maximum value of the alpha color control */
 void fimc_alpha_ctrl_update(struct fimc_ctx *ctx)
 {
        struct fimc_dev *fimc = ctx->fimc_dev;
-       struct v4l2_ctrl *ctrl = ctx->ctrl_alpha;
+       struct v4l2_ctrl *ctrl = ctx->ctrls.alpha;
 
        if (ctrl == NULL || !fimc->variant->has_alpha)
                return;
@@ -918,39 +681,6 @@ void fimc_alpha_ctrl_update(struct fimc_ctx *ctx)
        v4l2_ctrl_unlock(ctrl);
 }
 
-/*
- * V4L2 ioctl handlers
- */
-static int fimc_m2m_querycap(struct file *file, void *fh,
-                            struct v4l2_capability *cap)
-{
-       struct fimc_ctx *ctx = fh_to_ctx(fh);
-       struct fimc_dev *fimc = ctx->fimc_dev;
-
-       strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
-       strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
-       cap->bus_info[0] = 0;
-       cap->capabilities = V4L2_CAP_STREAMING |
-               V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
-
-       return 0;
-}
-
-static int fimc_m2m_enum_fmt_mplane(struct file *file, void *priv,
-                                   struct v4l2_fmtdesc *f)
-{
-       struct fimc_fmt *fmt;
-
-       fmt = fimc_find_format(NULL, NULL, get_m2m_fmt_flags(f->type),
-                              f->index);
-       if (!fmt)
-               return -EINVAL;
-
-       strncpy(f->description, fmt->name, sizeof(f->description) - 1);
-       f->pixelformat = fmt->fourcc;
-       return 0;
-}
-
 int fimc_fill_format(struct fimc_frame *frame, struct v4l2_format *f)
 {
        struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
@@ -1029,18 +759,6 @@ void fimc_adjust_mplane_format(struct fimc_fmt *fmt, u32 width, u32 height,
        }
 }
 
-static int fimc_m2m_g_fmt_mplane(struct file *file, void *fh,
-                                struct v4l2_format *f)
-{
-       struct fimc_ctx *ctx = fh_to_ctx(fh);
-       struct fimc_frame *frame = ctx_get_frame(ctx, f->type);
-
-       if (IS_ERR(frame))
-               return PTR_ERR(frame);
-
-       return fimc_fill_format(frame, f);
-}
-
 /**
  * fimc_find_format - lookup fimc color format by fourcc or media bus format
  * @pixelformat: fourcc to match, ignored if null
@@ -1048,14 +766,14 @@ static int fimc_m2m_g_fmt_mplane(struct file *file, void *fh,
  * @mask: the color flags to match
  * @index: offset in the fimc_formats array, ignored if negative
  */
-struct fimc_fmt *fimc_find_format(u32 *pixelformat, u32 *mbus_code,
+struct fimc_fmt *fimc_find_format(const u32 *pixelformat, const u32 *mbus_code,
                                  unsigned int mask, int index)
 {
        struct fimc_fmt *fmt, *def_fmt = NULL;
        unsigned int i;
        int id = 0;
 
-       if (index >= ARRAY_SIZE(fimc_formats))
+       if (index >= (int)ARRAY_SIZE(fimc_formats))
                return NULL;
 
        for (i = 0; i < ARRAY_SIZE(fimc_formats); ++i) {
@@ -1073,535 +791,10 @@ struct fimc_fmt *fimc_find_format(u32 *pixelformat, u32 *mbus_code,
        return def_fmt;
 }
 
-static int fimc_try_fmt_mplane(struct fimc_ctx *ctx, struct v4l2_format *f)
-{
-       struct fimc_dev *fimc = ctx->fimc_dev;
-       struct samsung_fimc_variant *variant = fimc->variant;
-       struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
-       struct fimc_fmt *fmt;
-       u32 max_w, mod_x, mod_y;
-
-       if (!IS_M2M(f->type))
-               return -EINVAL;
-
-       dbg("w: %d, h: %d", pix->width, pix->height);
-
-       fmt = fimc_find_format(&pix->pixelformat, NULL,
-                              get_m2m_fmt_flags(f->type), 0);
-       if (WARN(fmt == NULL, "Pixel format lookup failed"))
-               return -EINVAL;
-
-       if (pix->field == V4L2_FIELD_ANY)
-               pix->field = V4L2_FIELD_NONE;
-       else if (pix->field != V4L2_FIELD_NONE)
-               return -EINVAL;
-
-       if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
-               max_w = variant->pix_limit->scaler_dis_w;
-               mod_x = ffs(variant->min_inp_pixsize) - 1;
-       } else {
-               max_w = variant->pix_limit->out_rot_dis_w;
-               mod_x = ffs(variant->min_out_pixsize) - 1;
-       }
-
-       if (tiled_fmt(fmt)) {
-               mod_x = 6; /* 64 x 32 pixels tile */
-               mod_y = 5;
-       } else {
-               if (variant->min_vsize_align == 1)
-                       mod_y = fimc_fmt_is_rgb(fmt->color) ? 0 : 1;
-               else
-                       mod_y = ffs(variant->min_vsize_align) - 1;
-       }
-
-       v4l_bound_align_image(&pix->width, 16, max_w, mod_x,
-               &pix->height, 8, variant->pix_limit->scaler_dis_w, mod_y, 0);
-
-       fimc_adjust_mplane_format(fmt, pix->width, pix->height, &f->fmt.pix_mp);
-       return 0;
-}
-
-static int fimc_m2m_try_fmt_mplane(struct file *file, void *fh,
-                                  struct v4l2_format *f)
-{
-       struct fimc_ctx *ctx = fh_to_ctx(fh);
-
-       return fimc_try_fmt_mplane(ctx, f);
-}
-
-static int fimc_m2m_s_fmt_mplane(struct file *file, void *fh,
-                                struct v4l2_format *f)
-{
-       struct fimc_ctx *ctx = fh_to_ctx(fh);
-       struct fimc_dev *fimc = ctx->fimc_dev;
-       struct vb2_queue *vq;
-       struct fimc_frame *frame;
-       struct v4l2_pix_format_mplane *pix;
-       int i, ret = 0;
-
-       ret = fimc_try_fmt_mplane(ctx, f);
-       if (ret)
-               return ret;
-
-       vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
-
-       if (vb2_is_busy(vq)) {
-               v4l2_err(fimc->m2m.vfd, "queue (%d) busy\n", f->type);
-               return -EBUSY;
-       }
-
-       if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
-               frame = &ctx->s_frame;
-       else
-               frame = &ctx->d_frame;
-
-       pix = &f->fmt.pix_mp;
-       frame->fmt = fimc_find_format(&pix->pixelformat, NULL,
-                                     get_m2m_fmt_flags(f->type), 0);
-       if (!frame->fmt)
-               return -EINVAL;
-
-       /* Update RGB Alpha control state and value range */
-       fimc_alpha_ctrl_update(ctx);
-
-       for (i = 0; i < frame->fmt->colplanes; i++) {
-               frame->payload[i] =
-                       (pix->width * pix->height * frame->fmt->depth[i]) / 8;
-       }
-
-       fimc_fill_frame(frame, f);
-
-       ctx->scaler.enabled = 1;
-
-       if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
-               fimc_ctx_state_lock_set(FIMC_PARAMS | FIMC_DST_FMT, ctx);
-       else
-               fimc_ctx_state_lock_set(FIMC_PARAMS | FIMC_SRC_FMT, ctx);
-
-       dbg("f_w: %d, f_h: %d", frame->f_width, frame->f_height);
-
-       return 0;
-}
-
-static int fimc_m2m_reqbufs(struct file *file, void *fh,
-                           struct v4l2_requestbuffers *reqbufs)
-{
-       struct fimc_ctx *ctx = fh_to_ctx(fh);
-
-       return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
-}
-
-static int fimc_m2m_querybuf(struct file *file, void *fh,
-                            struct v4l2_buffer *buf)
-{
-       struct fimc_ctx *ctx = fh_to_ctx(fh);
-
-       return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
-}
-
-static int fimc_m2m_qbuf(struct file *file, void *fh,
-                        struct v4l2_buffer *buf)
-{
-       struct fimc_ctx *ctx = fh_to_ctx(fh);
-
-       return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
-}
-
-static int fimc_m2m_dqbuf(struct file *file, void *fh,
-                         struct v4l2_buffer *buf)
-{
-       struct fimc_ctx *ctx = fh_to_ctx(fh);
-
-       return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
-}
-
-static int fimc_m2m_streamon(struct file *file, void *fh,
-                            enum v4l2_buf_type type)
-{
-       struct fimc_ctx *ctx = fh_to_ctx(fh);
-
-       /* The source and target color format need to be set */
-       if (V4L2_TYPE_IS_OUTPUT(type)) {
-               if (!fimc_ctx_state_is_set(FIMC_SRC_FMT, ctx))
-                       return -EINVAL;
-       } else if (!fimc_ctx_state_is_set(FIMC_DST_FMT, ctx)) {
-               return -EINVAL;
-       }
-
-       return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
-}
-
-static int fimc_m2m_streamoff(struct file *file, void *fh,
-                           enum v4l2_buf_type type)
-{
-       struct fimc_ctx *ctx = fh_to_ctx(fh);
-
-       return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
-}
-
-static int fimc_m2m_cropcap(struct file *file, void *fh,
-                           struct v4l2_cropcap *cr)
-{
-       struct fimc_ctx *ctx = fh_to_ctx(fh);
-       struct fimc_frame *frame;
-
-       frame = ctx_get_frame(ctx, cr->type);
-       if (IS_ERR(frame))
-               return PTR_ERR(frame);
-
-       cr->bounds.left         = 0;
-       cr->bounds.top          = 0;
-       cr->bounds.width        = frame->o_width;
-       cr->bounds.height       = frame->o_height;
-       cr->defrect             = cr->bounds;
-
-       return 0;
-}
-
-static int fimc_m2m_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
-{
-       struct fimc_ctx *ctx = fh_to_ctx(fh);
-       struct fimc_frame *frame;
-
-       frame = ctx_get_frame(ctx, cr->type);
-       if (IS_ERR(frame))
-               return PTR_ERR(frame);
-
-       cr->c.left = frame->offs_h;
-       cr->c.top = frame->offs_v;
-       cr->c.width = frame->width;
-       cr->c.height = frame->height;
-
-       return 0;
-}
-
-static int fimc_m2m_try_crop(struct fimc_ctx *ctx, struct v4l2_crop *cr)
-{
-       struct fimc_dev *fimc = ctx->fimc_dev;
-       struct fimc_frame *f;
-       u32 min_size, halign, depth = 0;
-       int i;
-
-       if (cr->c.top < 0 || cr->c.left < 0) {
-               v4l2_err(fimc->m2m.vfd,
-                       "doesn't support negative values for top & left\n");
-               return -EINVAL;
-       }
-       if (cr->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
-               f = &ctx->d_frame;
-       else if (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
-               f = &ctx->s_frame;
-       else
-               return -EINVAL;
-
-       min_size = (f == &ctx->s_frame) ?
-               fimc->variant->min_inp_pixsize : fimc->variant->min_out_pixsize;
-
-       /* Get pixel alignment constraints. */
-       if (fimc->variant->min_vsize_align == 1)
-               halign = fimc_fmt_is_rgb(f->fmt->color) ? 0 : 1;
-       else
-               halign = ffs(fimc->variant->min_vsize_align) - 1;
-
-       for (i = 0; i < f->fmt->colplanes; i++)
-               depth += f->fmt->depth[i];
-
-       v4l_bound_align_image(&cr->c.width, min_size, f->o_width,
-                             ffs(min_size) - 1,
-                             &cr->c.height, min_size, f->o_height,
-                             halign, 64/(ALIGN(depth, 8)));
-
-       /* adjust left/top if cropping rectangle is out of bounds */
-       if (cr->c.left + cr->c.width > f->o_width)
-               cr->c.left = f->o_width - cr->c.width;
-       if (cr->c.top + cr->c.height > f->o_height)
-               cr->c.top = f->o_height - cr->c.height;
-
-       cr->c.left = round_down(cr->c.left, min_size);
-       cr->c.top  = round_down(cr->c.top, fimc->variant->hor_offs_align);
-
-       dbg("l:%d, t:%d, w:%d, h:%d, f_w: %d, f_h: %d",
-           cr->c.left, cr->c.top, cr->c.width, cr->c.height,
-           f->f_width, f->f_height);
-
-       return 0;
-}
-
-static int fimc_m2m_s_crop(struct file *file, void *fh, struct v4l2_crop *cr)
-{
-       struct fimc_ctx *ctx = fh_to_ctx(fh);
-       struct fimc_dev *fimc = ctx->fimc_dev;
-       struct fimc_frame *f;
-       int ret;
-
-       ret = fimc_m2m_try_crop(ctx, cr);
-       if (ret)
-               return ret;
-
-       f = (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) ?
-               &ctx->s_frame : &ctx->d_frame;
-
-       /* Check to see if scaling ratio is within supported range */
-       if (fimc_ctx_state_is_set(FIMC_DST_FMT | FIMC_SRC_FMT, ctx)) {
-               if (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
-                       ret = fimc_check_scaler_ratio(ctx, cr->c.width,
-                                       cr->c.height, ctx->d_frame.width,
-                                       ctx->d_frame.height, ctx->rotation);
-               } else {
-                       ret = fimc_check_scaler_ratio(ctx, ctx->s_frame.width,
-                                       ctx->s_frame.height, cr->c.width,
-                                       cr->c.height, ctx->rotation);
-               }
-               if (ret) {
-                       v4l2_err(fimc->m2m.vfd, "Out of scaler range\n");
-                       return -EINVAL;
-               }
-       }
-
-       f->offs_h = cr->c.left;
-       f->offs_v = cr->c.top;
-       f->width  = cr->c.width;
-       f->height = cr->c.height;
-
-       fimc_ctx_state_lock_set(FIMC_PARAMS, ctx);
-
-       return 0;
-}
-
-static const struct v4l2_ioctl_ops fimc_m2m_ioctl_ops = {
-       .vidioc_querycap                = fimc_m2m_querycap,
-
-       .vidioc_enum_fmt_vid_cap_mplane = fimc_m2m_enum_fmt_mplane,
-       .vidioc_enum_fmt_vid_out_mplane = fimc_m2m_enum_fmt_mplane,
-
-       .vidioc_g_fmt_vid_cap_mplane    = fimc_m2m_g_fmt_mplane,
-       .vidioc_g_fmt_vid_out_mplane    = fimc_m2m_g_fmt_mplane,
-
-       .vidioc_try_fmt_vid_cap_mplane  = fimc_m2m_try_fmt_mplane,
-       .vidioc_try_fmt_vid_out_mplane  = fimc_m2m_try_fmt_mplane,
-
-       .vidioc_s_fmt_vid_cap_mplane    = fimc_m2m_s_fmt_mplane,
-       .vidioc_s_fmt_vid_out_mplane    = fimc_m2m_s_fmt_mplane,
-
-       .vidioc_reqbufs                 = fimc_m2m_reqbufs,
-       .vidioc_querybuf                = fimc_m2m_querybuf,
-
-       .vidioc_qbuf                    = fimc_m2m_qbuf,
-       .vidioc_dqbuf                   = fimc_m2m_dqbuf,
-
-       .vidioc_streamon                = fimc_m2m_streamon,
-       .vidioc_streamoff               = fimc_m2m_streamoff,
-
-       .vidioc_g_crop                  = fimc_m2m_g_crop,
-       .vidioc_s_crop                  = fimc_m2m_s_crop,
-       .vidioc_cropcap                 = fimc_m2m_cropcap
-
-};
-
-static int queue_init(void *priv, struct vb2_queue *src_vq,
-                     struct vb2_queue *dst_vq)
-{
-       struct fimc_ctx *ctx = priv;
-       int ret;
-
-       memset(src_vq, 0, sizeof(*src_vq));
-       src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
-       src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
-       src_vq->drv_priv = ctx;
-       src_vq->ops = &fimc_qops;
-       src_vq->mem_ops = &vb2_dma_contig_memops;
-       src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
-
-       ret = vb2_queue_init(src_vq);
-       if (ret)
-               return ret;
-
-       memset(dst_vq, 0, sizeof(*dst_vq));
-       dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
-       dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
-       dst_vq->drv_priv = ctx;
-       dst_vq->ops = &fimc_qops;
-       dst_vq->mem_ops = &vb2_dma_contig_memops;
-       dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
-
-       return vb2_queue_init(dst_vq);
-}
-
-static int fimc_m2m_open(struct file *file)
-{
-       struct fimc_dev *fimc = video_drvdata(file);
-       struct fimc_ctx *ctx;
-       int ret;
-
-       dbg("pid: %d, state: 0x%lx, refcnt: %d",
-               task_pid_nr(current), fimc->state, fimc->vid_cap.refcnt);
-
-       /*
-        * Return if the corresponding video capture node
-        * is already opened.
-        */
-       if (fimc->vid_cap.refcnt > 0)
-               return -EBUSY;
-
-       ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
-       if (!ctx)
-               return -ENOMEM;
-       v4l2_fh_init(&ctx->fh, fimc->m2m.vfd);
-       ctx->fimc_dev = fimc;
-
-       /* Default color format */
-       ctx->s_frame.fmt = &fimc_formats[0];
-       ctx->d_frame.fmt = &fimc_formats[0];
-
-       ret = fimc_ctrls_create(ctx);
-       if (ret)
-               goto error_fh;
-
-       /* Use separate control handler per file handle */
-       ctx->fh.ctrl_handler = &ctx->ctrl_handler;
-       file->private_data = &ctx->fh;
-       v4l2_fh_add(&ctx->fh);
-
-       /* Setup the device context for memory-to-memory mode */
-       ctx->state = FIMC_CTX_M2M;
-       ctx->flags = 0;
-       ctx->in_path = FIMC_DMA;
-       ctx->out_path = FIMC_DMA;
-       spin_lock_init(&ctx->slock);
-
-       ctx->m2m_ctx = v4l2_m2m_ctx_init(fimc->m2m.m2m_dev, ctx, queue_init);
-       if (IS_ERR(ctx->m2m_ctx)) {
-               ret = PTR_ERR(ctx->m2m_ctx);
-               goto error_c;
-       }
-
-       if (fimc->m2m.refcnt++ == 0)
-               set_bit(ST_M2M_RUN, &fimc->state);
-       return 0;
-
-error_c:
-       fimc_ctrls_delete(ctx);
-error_fh:
-       v4l2_fh_del(&ctx->fh);
-       v4l2_fh_exit(&ctx->fh);
-       kfree(ctx);
-       return ret;
-}
-
-static int fimc_m2m_release(struct file *file)
-{
-       struct fimc_ctx *ctx = fh_to_ctx(file->private_data);
-       struct fimc_dev *fimc = ctx->fimc_dev;
-
-       dbg("pid: %d, state: 0x%lx, refcnt= %d",
-               task_pid_nr(current), fimc->state, fimc->m2m.refcnt);
-
-       v4l2_m2m_ctx_release(ctx->m2m_ctx);
-       fimc_ctrls_delete(ctx);
-       v4l2_fh_del(&ctx->fh);
-       v4l2_fh_exit(&ctx->fh);
-
-       if (--fimc->m2m.refcnt <= 0)
-               clear_bit(ST_M2M_RUN, &fimc->state);
-       kfree(ctx);
-       return 0;
-}
-
-static unsigned int fimc_m2m_poll(struct file *file,
-                                 struct poll_table_struct *wait)
-{
-       struct fimc_ctx *ctx = fh_to_ctx(file->private_data);
-
-       return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
-}
-
-
-static int fimc_m2m_mmap(struct file *file, struct vm_area_struct *vma)
-{
-       struct fimc_ctx *ctx = fh_to_ctx(file->private_data);
-
-       return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
-}
-
-static const struct v4l2_file_operations fimc_m2m_fops = {
-       .owner          = THIS_MODULE,
-       .open           = fimc_m2m_open,
-       .release        = fimc_m2m_release,
-       .poll           = fimc_m2m_poll,
-       .unlocked_ioctl = video_ioctl2,
-       .mmap           = fimc_m2m_mmap,
-};
-
-static struct v4l2_m2m_ops m2m_ops = {
-       .device_run     = fimc_dma_run,
-       .job_abort      = fimc_job_abort,
-};
-
-int fimc_register_m2m_device(struct fimc_dev *fimc,
-                            struct v4l2_device *v4l2_dev)
-{
-       struct video_device *vfd;
-       struct platform_device *pdev;
-       int ret = 0;
-
-       if (!fimc)
-               return -ENODEV;
-
-       pdev = fimc->pdev;
-       fimc->v4l2_dev = v4l2_dev;
-
-       vfd = video_device_alloc();
-       if (!vfd) {
-               v4l2_err(v4l2_dev, "Failed to allocate video device\n");
-               return -ENOMEM;
-       }
-
-       vfd->fops       = &fimc_m2m_fops;
-       vfd->ioctl_ops  = &fimc_m2m_ioctl_ops;
-       vfd->v4l2_dev   = v4l2_dev;
-       vfd->minor      = -1;
-       vfd->release    = video_device_release;
-       vfd->lock       = &fimc->lock;
-
-       snprintf(vfd->name, sizeof(vfd->name), "%s.m2m", dev_name(&pdev->dev));
-       video_set_drvdata(vfd, fimc);
-
-       fimc->m2m.vfd = vfd;
-       fimc->m2m.m2m_dev = v4l2_m2m_init(&m2m_ops);
-       if (IS_ERR(fimc->m2m.m2m_dev)) {
-               v4l2_err(v4l2_dev, "failed to initialize v4l2-m2m device\n");
-               ret = PTR_ERR(fimc->m2m.m2m_dev);
-               goto err_init;
-       }
-
-       ret = media_entity_init(&vfd->entity, 0, NULL, 0);
-       if (!ret)
-               return 0;
-
-       v4l2_m2m_release(fimc->m2m.m2m_dev);
-err_init:
-       video_device_release(fimc->m2m.vfd);
-       return ret;
-}
-
-void fimc_unregister_m2m_device(struct fimc_dev *fimc)
-{
-       if (!fimc)
-               return;
-
-       if (fimc->m2m.m2m_dev)
-               v4l2_m2m_release(fimc->m2m.m2m_dev);
-       if (fimc->m2m.vfd) {
-               media_entity_cleanup(&fimc->m2m.vfd->entity);
-               /* Can also be called if video device wasn't registered */
-               video_unregister_device(fimc->m2m.vfd);
-       }
-}
-
 static void fimc_clk_put(struct fimc_dev *fimc)
 {
        int i;
-       for (i = 0; i < fimc->num_clocks; i++) {
+       for (i = 0; i < MAX_FIMC_CLOCKS; i++) {
                if (IS_ERR_OR_NULL(fimc->clock[i]))
                        continue;
                clk_unprepare(fimc->clock[i]);
@@ -1614,7 +807,7 @@ static int fimc_clk_get(struct fimc_dev *fimc)
 {
        int i, ret;
 
-       for (i = 0; i < fimc->num_clocks; i++) {
+       for (i = 0; i < MAX_FIMC_CLOCKS; i++) {
                fimc->clock[i] = clk_get(&fimc->pdev->dev, fimc_clocks[i]);
                if (IS_ERR(fimc->clock[i]))
                        goto err;
@@ -1672,15 +865,12 @@ static int fimc_m2m_resume(struct fimc_dev *fimc)
 
 static int fimc_probe(struct platform_device *pdev)
 {
+       struct fimc_drvdata *drv_data = fimc_get_drvdata(pdev);
+       struct s5p_platform_fimc *pdata;
        struct fimc_dev *fimc;
        struct resource *res;
-       struct samsung_fimc_driverdata *drv_data;
-       struct s5p_platform_fimc *pdata;
        int ret = 0;
 
-       drv_data = (struct samsung_fimc_driverdata *)
-               platform_get_device_id(pdev)->driver_data;
-
        if (pdev->id >= drv_data->num_entities) {
                dev_err(&pdev->dev, "Invalid platform device id: %d\n",
                        pdev->id);
@@ -1714,28 +904,29 @@ static int fimc_probe(struct platform_device *pdev)
                dev_err(&pdev->dev, "Failed to get IRQ resource\n");
                return -ENXIO;
        }
-       fimc->irq = res->start;
 
-       fimc->num_clocks = MAX_FIMC_CLOCKS;
        ret = fimc_clk_get(fimc);
        if (ret)
                return ret;
        clk_set_rate(fimc->clock[CLK_BUS], drv_data->lclk_frequency);
        clk_enable(fimc->clock[CLK_BUS]);
 
-       platform_set_drvdata(pdev, fimc);
-
-       ret = devm_request_irq(&pdev->dev, fimc->irq, fimc_irq_handler,
-                              0, pdev->name, fimc);
+       ret = devm_request_irq(&pdev->dev, res->start, fimc_irq_handler,
+                              0, dev_name(&pdev->dev), fimc);
        if (ret) {
                dev_err(&pdev->dev, "failed to install irq (%d)\n", ret);
                goto err_clk;
        }
 
+       ret = fimc_initialize_capture_subdev(fimc);
+       if (ret)
+               goto err_clk;
+
+       platform_set_drvdata(pdev, fimc);
        pm_runtime_enable(&pdev->dev);
        ret = pm_runtime_get_sync(&pdev->dev);
        if (ret < 0)
-               goto err_clk;
+               goto err_sd;
        /* Initialize contiguous memory allocator */
        fimc->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
        if (IS_ERR(fimc->alloc_ctx)) {
@@ -1747,9 +938,10 @@ static int fimc_probe(struct platform_device *pdev)
 
        pm_runtime_put(&pdev->dev);
        return 0;
-
 err_pm:
        pm_runtime_put(&pdev->dev);
+err_sd:
+       fimc_unregister_capture_subdev(fimc);
 err_clk:
        fimc_clk_put(fimc);
        return ret;
@@ -1834,6 +1026,7 @@ static int __devexit fimc_remove(struct platform_device *pdev)
        pm_runtime_disable(&pdev->dev);
        pm_runtime_set_suspended(&pdev->dev);
 
+       fimc_unregister_capture_subdev(fimc);
        vb2_dma_contig_cleanup_ctx(fimc->alloc_ctx);
 
        clk_disable(fimc->clock[CLK_BUS]);
@@ -1879,7 +1072,7 @@ static struct fimc_pix_limit s5p_pix_limit[4] = {
        },
 };
 
-static struct samsung_fimc_variant fimc0_variant_s5p = {
+static struct fimc_variant fimc0_variant_s5p = {
        .has_inp_rot     = 1,
        .has_out_rot     = 1,
        .has_cam_if      = 1,
@@ -1891,17 +1084,17 @@ static struct samsung_fimc_variant fimc0_variant_s5p = {
        .pix_limit       = &s5p_pix_limit[0],
 };
 
-static struct samsung_fimc_variant fimc2_variant_s5p = {
+static struct fimc_variant fimc2_variant_s5p = {
        .has_cam_if      = 1,
        .min_inp_pixsize = 16,
        .min_out_pixsize = 16,
        .hor_offs_align  = 8,
        .min_vsize_align = 16,
        .out_buf_count   = 4,
-       .pix_limit = &s5p_pix_limit[1],
+       .pix_limit       = &s5p_pix_limit[1],
 };
 
-static struct samsung_fimc_variant fimc0_variant_s5pv210 = {
+static struct fimc_variant fimc0_variant_s5pv210 = {
        .pix_hoff        = 1,
        .has_inp_rot     = 1,
        .has_out_rot     = 1,
@@ -1914,7 +1107,7 @@ static struct samsung_fimc_variant fimc0_variant_s5pv210 = {
        .pix_limit       = &s5p_pix_limit[1],
 };
 
-static struct samsung_fimc_variant fimc1_variant_s5pv210 = {
+static struct fimc_variant fimc1_variant_s5pv210 = {
        .pix_hoff        = 1,
        .has_inp_rot     = 1,
        .has_out_rot     = 1,
@@ -1928,7 +1121,7 @@ static struct samsung_fimc_variant fimc1_variant_s5pv210 = {
        .pix_limit       = &s5p_pix_limit[2],
 };
 
-static struct samsung_fimc_variant fimc2_variant_s5pv210 = {
+static struct fimc_variant fimc2_variant_s5pv210 = {
        .has_cam_if      = 1,
        .pix_hoff        = 1,
        .min_inp_pixsize = 16,
@@ -1939,7 +1132,7 @@ static struct samsung_fimc_variant fimc2_variant_s5pv210 = {
        .pix_limit       = &s5p_pix_limit[2],
 };
 
-static struct samsung_fimc_variant fimc0_variant_exynos4 = {
+static struct fimc_variant fimc0_variant_exynos4 = {
        .pix_hoff        = 1,
        .has_inp_rot     = 1,
        .has_out_rot     = 1,
@@ -1955,7 +1148,7 @@ static struct samsung_fimc_variant fimc0_variant_exynos4 = {
        .pix_limit       = &s5p_pix_limit[1],
 };
 
-static struct samsung_fimc_variant fimc3_variant_exynos4 = {
+static struct fimc_variant fimc3_variant_exynos4 = {
        .pix_hoff        = 1,
        .has_cam_if      = 1,
        .has_cistatus2   = 1,
@@ -1970,7 +1163,7 @@ static struct samsung_fimc_variant fimc3_variant_exynos4 = {
 };
 
 /* S5PC100 */
-static struct samsung_fimc_driverdata fimc_drvdata_s5p = {
+static struct fimc_drvdata fimc_drvdata_s5p = {
        .variant = {
                [0] = &fimc0_variant_s5p,
                [1] = &fimc0_variant_s5p,
@@ -1981,7 +1174,7 @@ static struct samsung_fimc_driverdata fimc_drvdata_s5p = {
 };
 
 /* S5PV210, S5PC110 */
-static struct samsung_fimc_driverdata fimc_drvdata_s5pv210 = {
+static struct fimc_drvdata fimc_drvdata_s5pv210 = {
        .variant = {
                [0] = &fimc0_variant_s5pv210,
                [1] = &fimc1_variant_s5pv210,
@@ -1991,8 +1184,8 @@ static struct samsung_fimc_driverdata fimc_drvdata_s5pv210 = {
        .lclk_frequency = 166000000UL,
 };
 
-/* S5PV310, S5PC210 */
-static struct samsung_fimc_driverdata fimc_drvdata_exynos4 = {
+/* EXYNOS4210, S5PV310, S5PC210 */
+static struct fimc_drvdata fimc_drvdata_exynos4 = {
        .variant = {
                [0] = &fimc0_variant_exynos4,
                [1] = &fimc0_variant_exynos4,
@@ -2036,7 +1229,7 @@ static struct platform_driver fimc_driver = {
 
 int __init fimc_register_driver(void)
 {
-       return platform_driver_probe(&fimc_driver, fimc_probe);
+       return platform_driver_register(&fimc_driver);
 }
 
 void __exit fimc_unregister_driver(void)