Merge branch 'staging-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[pandora-kernel.git] / drivers / media / video / s5p-fimc / fimc-core.c
index 09bfac4..dc91a85 100644 (file)
@@ -200,24 +200,21 @@ static struct v4l2_queryctrl *get_ctrl(int id)
        return NULL;
 }
 
-int fimc_check_scaler_ratio(struct v4l2_rect *r, struct fimc_frame *f)
+int fimc_check_scaler_ratio(int sw, int sh, int dw, int dh, int rot)
 {
-       if (r->width > f->width) {
-               if (f->width > (r->width * SCALER_MAX_HRATIO))
-                       return -EINVAL;
-       } else {
-               if ((f->width * SCALER_MAX_HRATIO) < r->width)
-                       return -EINVAL;
-       }
+       int tx, ty;
 
-       if (r->height > f->height) {
-               if (f->height > (r->height * SCALER_MAX_VRATIO))
-                       return -EINVAL;
+       if (rot == 90 || rot == 270) {
+               ty = dw;
+               tx = dh;
        } else {
-               if ((f->height * SCALER_MAX_VRATIO) < r->height)
-                       return -EINVAL;
+               tx = dw;
+               ty = dh;
        }
 
+       if ((sw >= SCALER_MAX_HRATIO * tx) || (sh >= SCALER_MAX_VRATIO * ty))
+               return -EINVAL;
+
        return 0;
 }
 
@@ -310,13 +307,74 @@ int fimc_set_scaler_info(struct fimc_ctx *ctx)
        return 0;
 }
 
-static void fimc_capture_handler(struct fimc_dev *fimc)
+static void fimc_m2m_job_finish(struct fimc_ctx *ctx, int vb_state)
+{
+       struct vb2_buffer *src_vb, *dst_vb;
+       struct fimc_dev *fimc = ctx->fimc_dev;
+
+       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(fimc->m2m.m2m_dev, ctx->m2m_ctx);
+       }
+}
+
+/* Complete the transaction which has been scheduled for execution. */
+static void fimc_m2m_shutdown(struct fimc_ctx *ctx)
+{
+       struct fimc_dev *fimc = ctx->fimc_dev;
+       int ret;
+
+       if (!fimc_m2m_pending(fimc))
+               return;
+
+       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);
+       /*
+        * In case of a timeout the buffers are not released in the interrupt
+        * handler so return them here with the error flag set, if there are
+        * any on the queue.
+        */
+       if (ret == 0)
+               fimc_m2m_job_finish(ctx, VB2_BUF_STATE_ERROR);
+}
+
+static int stop_streaming(struct vb2_queue *q)
+{
+       struct fimc_ctx *ctx = q->drv_priv;
+
+       fimc_m2m_shutdown(ctx);
+
+       return 0;
+}
+
+static void fimc_capture_irq_handler(struct fimc_dev *fimc)
 {
        struct fimc_vid_cap *cap = &fimc->vid_cap;
-       struct fimc_vid_buffer *v_buf = NULL;
+       struct fimc_vid_buffer *v_buf;
+       struct timeval *tv;
+       struct timespec ts;
+
+       if (!list_empty(&cap->active_buf_q) &&
+           test_bit(ST_CAPT_RUN, &fimc->state)) {
+               ktime_get_real_ts(&ts);
 
-       if (!list_empty(&cap->active_buf_q)) {
                v_buf = 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);
        }
 
@@ -331,9 +389,6 @@ static void fimc_capture_handler(struct fimc_dev *fimc)
                fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
                v_buf->index = cap->buf_index;
 
-               dbg("hw ptr: %d, sw ptr: %d",
-                   fimc_hw_get_frame_index(fimc), cap->buf_index);
-
                /* Move the buffer to the capture active queue */
                active_queue_add(cap, v_buf);
 
@@ -342,52 +397,56 @@ static void fimc_capture_handler(struct fimc_dev *fimc)
 
                if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
                        cap->buf_index = 0;
+       }
+
+       if (cap->active_buf_cnt == 0) {
+               clear_bit(ST_CAPT_RUN, &fimc->state);
 
-       } else if (test_and_clear_bit(ST_CAPT_STREAM, &fimc->state) &&
-                  cap->active_buf_cnt <= 1) {
-               fimc_deactivate_capture(fimc);
+               if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
+                       cap->buf_index = 0;
+       } else {
+               set_bit(ST_CAPT_RUN, &fimc->state);
        }
 
-       dbg("frame: %d, active_buf_cnt= %d",
+       dbg("frame: %d, active_buf_cnt: %d",
            fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
 }
 
 static irqreturn_t fimc_isr(int irq, void *priv)
 {
        struct fimc_dev *fimc = priv;
+       struct fimc_vid_cap *cap = &fimc->vid_cap;
+       struct fimc_ctx *ctx;
 
-       BUG_ON(!fimc);
        fimc_hw_clear_irq(fimc);
 
-       spin_lock(&fimc->slock);
-
        if (test_and_clear_bit(ST_M2M_PEND, &fimc->state)) {
-               struct vb2_buffer *src_vb, *dst_vb;
-               struct fimc_ctx *ctx = v4l2_m2m_get_curr_priv(fimc->m2m.m2m_dev);
-
-               if (!ctx || !ctx->m2m_ctx)
-                       goto isr_unlock;
-
-               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, VB2_BUF_STATE_DONE);
-                       v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
-                       v4l2_m2m_job_finish(fimc->m2m.m2m_dev, ctx->m2m_ctx);
+               ctx = v4l2_m2m_get_curr_priv(fimc->m2m.m2m_dev);
+               if (ctx != NULL) {
+                       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);
                }
-               goto isr_unlock;
 
+               return IRQ_HANDLED;
        }
 
-       if (test_bit(ST_CAPT_RUN, &fimc->state))
-               fimc_capture_handler(fimc);
+       spin_lock(&fimc->slock);
 
-       if (test_and_clear_bit(ST_CAPT_PEND, &fimc->state)) {
-               set_bit(ST_CAPT_RUN, &fimc->state);
-               wake_up(&fimc->irq_queue);
+       if (test_bit(ST_CAPT_PEND, &fimc->state)) {
+               fimc_capture_irq_handler(fimc);
+
+               if (cap->active_buf_cnt == 1) {
+                       fimc_deactivate_capture(fimc);
+                       clear_bit(ST_CAPT_STREAM, &fimc->state);
+               }
        }
 
-isr_unlock:
        spin_unlock(&fimc->slock);
        return IRQ_HANDLED;
 }
@@ -591,26 +650,26 @@ static void fimc_dma_run(void *priv)
 
        ctx->state |= (FIMC_SRC_ADDR | FIMC_DST_ADDR);
        ret = fimc_prepare_config(ctx, ctx->state);
-       if (ret) {
-               err("Wrong parameters");
+       if (ret)
                goto dma_unlock;
-       }
+
        /* Reconfigure hardware if the context has changed. */
        if (fimc->m2m.ctx != ctx) {
                ctx->state |= FIMC_PARAMS;
                fimc->m2m.ctx = ctx;
        }
 
+       spin_lock(&fimc->slock);
        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);
-               if (fimc_set_scaler_info(ctx)) {
-                       err("Scaler setup error");
+               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);
@@ -630,6 +689,7 @@ static void fimc_dma_run(void *priv)
        ctx->state &= (FIMC_CTX_M2M | FIMC_CTX_CAP |
                       FIMC_SRC_FMT | FIMC_DST_FMT);
        fimc_hw_activate_input_dma(fimc, true);
+       spin_unlock(&fimc->slock);
 
 dma_unlock:
        spin_unlock_irqrestore(&ctx->slock, flags);
@@ -637,7 +697,7 @@ dma_unlock:
 
 static void fimc_job_abort(void *priv)
 {
-       /* Nothing done in job_abort. */
+       fimc_m2m_shutdown(priv);
 }
 
 static int fimc_queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
@@ -708,12 +768,13 @@ static void fimc_unlock(struct vb2_queue *vq)
        mutex_unlock(&ctx->fimc_dev->lock);
 }
 
-struct vb2_ops fimc_qops = {
+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,
 };
 
 static int fimc_m2m_querycap(struct file *file, void *priv,
@@ -753,15 +814,33 @@ int fimc_vidioc_g_fmt_mplane(struct file *file, void *priv,
 {
        struct fimc_ctx *ctx = priv;
        struct fimc_frame *frame;
+       struct v4l2_pix_format_mplane *pixm;
+       int i;
 
        frame = ctx_get_frame(ctx, f->type);
        if (IS_ERR(frame))
                return PTR_ERR(frame);
 
-       f->fmt.pix.width        = frame->width;
-       f->fmt.pix.height       = frame->height;
-       f->fmt.pix.field        = V4L2_FIELD_NONE;
-       f->fmt.pix.pixelformat  = frame->fmt->fourcc;
+       pixm = &f->fmt.pix_mp;
+
+       pixm->width             = frame->width;
+       pixm->height            = frame->height;
+       pixm->field             = V4L2_FIELD_NONE;
+       pixm->pixelformat       = frame->fmt->fourcc;
+       pixm->colorspace        = V4L2_COLORSPACE_JPEG;
+       pixm->num_planes        = frame->fmt->memplanes;
+
+       for (i = 0; i < pixm->num_planes; ++i) {
+               int bpl = frame->o_width;
+
+               if (frame->fmt->colplanes == 1) /* packed formats */
+                       bpl = (bpl * frame->fmt->depth[0]) / 8;
+
+               pixm->plane_fmt[i].bytesperline = bpl;
+
+               pixm->plane_fmt[i].sizeimage = (frame->o_width *
+                       frame->o_height * frame->fmt->depth[i]) / 8;
+       }
 
        return 0;
 }
@@ -810,7 +889,7 @@ int fimc_vidioc_try_fmt_mplane(struct file *file, void *priv,
 
 
        if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
-               if (ctx->state & FIMC_CTX_CAP)
+               if (fimc_ctx_state_is_set(FIMC_CTX_CAP, ctx))
                        return -EINVAL;
                is_output = 1;
        } else if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
@@ -856,24 +935,25 @@ int fimc_vidioc_try_fmt_mplane(struct file *file, void *priv,
                &pix->height, 8, variant->pix_limit->scaler_dis_w, mod_y, 0);
 
        pix->num_planes = fmt->memplanes;
+       pix->colorspace = V4L2_COLORSPACE_JPEG;
 
-       for (i = 0; i < pix->num_planes; ++i) {
-               int bpl = pix->plane_fmt[i].bytesperline;
 
-               dbg("[%d] bpl: %d, depth: %d, w: %d, h: %d",
-                   i, bpl, fmt->depth[i], pix->width, pix->height);
+       for (i = 0; i < pix->num_planes; ++i) {
+               u32 bpl = pix->plane_fmt[i].bytesperline;
+               u32 *sizeimage = &pix->plane_fmt[i].sizeimage;
 
-               if (!bpl || (bpl * 8 / fmt->depth[i]) > pix->width)
-                       bpl = (pix->width * fmt->depth[0]) >> 3;
+               if (fmt->colplanes > 1 && (bpl == 0 || bpl < pix->width))
+                       bpl = pix->width; /* Planar */
 
-               if (!pix->plane_fmt[i].sizeimage)
-                       pix->plane_fmt[i].sizeimage = pix->height * bpl;
+               if (fmt->colplanes == 1 && /* Packed */
+                   (bpl == 0 || ((bpl * 8) / fmt->depth[i]) < pix->width))
+                       bpl = (pix->width * fmt->depth[0]) / 8;
 
-               pix->plane_fmt[i].bytesperline = bpl;
+               if (i == 0) /* Same bytesperline for each plane. */
+                       mod_x = bpl;
 
-               dbg("[%d]: bpl: %d, sizeimage: %d",
-                   i, pix->plane_fmt[i].bytesperline,
-                   pix->plane_fmt[i].sizeimage);
+               pix->plane_fmt[i].bytesperline = mod_x;
+               *sizeimage = (pix->width * pix->height * fmt->depth[i]) / 8;
        }
 
        return 0;
@@ -887,9 +967,7 @@ static int fimc_m2m_s_fmt_mplane(struct file *file, void *priv,
        struct vb2_queue *vq;
        struct fimc_frame *frame;
        struct v4l2_pix_format_mplane *pix;
-       unsigned long flags;
        int i, ret = 0;
-       u32 tmp;
 
        ret = fimc_vidioc_try_fmt_mplane(file, priv, f);
        if (ret)
@@ -897,7 +975,7 @@ static int fimc_m2m_s_fmt_mplane(struct file *file, void *priv,
 
        vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
 
-       if (vb2_is_streaming(vq)) {
+       if (vb2_is_busy(vq)) {
                v4l2_err(&fimc->m2m.v4l2_dev, "queue (%d) busy\n", f->type);
                return -EBUSY;
        }
@@ -917,8 +995,10 @@ static int fimc_m2m_s_fmt_mplane(struct file *file, void *priv,
        if (!frame->fmt)
                return -EINVAL;
 
-       for (i = 0; i < frame->fmt->colplanes; i++)
-               frame->payload[i] = pix->plane_fmt[i].bytesperline * pix->height;
+       for (i = 0; i < frame->fmt->colplanes; i++) {
+               frame->payload[i] =
+                       (pix->width * pix->height * frame->fmt->depth[i]) / 8;
+       }
 
        frame->f_width  = pix->plane_fmt[0].bytesperline * 8 /
                frame->fmt->depth[0];
@@ -930,10 +1010,10 @@ static int fimc_m2m_s_fmt_mplane(struct file *file, void *priv,
        frame->offs_h   = 0;
        frame->offs_v   = 0;
 
-       spin_lock_irqsave(&ctx->slock, flags);
-       tmp = (frame == &ctx->d_frame) ? FIMC_DST_FMT : FIMC_SRC_FMT;
-       ctx->state |= FIMC_PARAMS | tmp;
-       spin_unlock_irqrestore(&ctx->slock, flags);
+       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);
 
@@ -976,9 +1056,9 @@ static int fimc_m2m_streamon(struct file *file, void *priv,
 
        /* The source and target color format need to be set */
        if (V4L2_TYPE_IS_OUTPUT(type)) {
-               if (~ctx->state & FIMC_SRC_FMT)
+               if (!fimc_ctx_state_is_set(FIMC_SRC_FMT, ctx))
                        return -EINVAL;
-       } else if (~ctx->state & FIMC_DST_FMT) {
+       } else if (!fimc_ctx_state_is_set(FIMC_DST_FMT, ctx)) {
                return -EINVAL;
        }
 
@@ -1005,7 +1085,7 @@ int fimc_vidioc_queryctrl(struct file *file, void *priv,
                return 0;
        }
 
-       if (ctx->state & FIMC_CTX_CAP) {
+       if (fimc_ctx_state_is_set(FIMC_CTX_CAP, ctx)) {
                return v4l2_subdev_call(ctx->fimc_dev->vid_cap.sd,
                                        core, queryctrl, qc);
        }
@@ -1029,12 +1109,11 @@ int fimc_vidioc_g_ctrl(struct file *file, void *priv,
                ctrl->value = ctx->rotation;
                break;
        default:
-               if (ctx->state & FIMC_CTX_CAP) {
+               if (fimc_ctx_state_is_set(FIMC_CTX_CAP, ctx)) {
                        return v4l2_subdev_call(fimc->vid_cap.sd, core,
                                                g_ctrl, ctrl);
                } else {
-                       v4l2_err(&fimc->m2m.v4l2_dev,
-                                "Invalid control\n");
+                       v4l2_err(&fimc->m2m.v4l2_dev, "Invalid control\n");
                        return -EINVAL;
                }
        }
@@ -1064,9 +1143,7 @@ int fimc_s_ctrl(struct fimc_ctx *ctx, struct v4l2_control *ctrl)
 {
        struct samsung_fimc_variant *variant = ctx->fimc_dev->variant;
        struct fimc_dev *fimc = ctx->fimc_dev;
-       unsigned long flags;
-
-       spin_lock_irqsave(&ctx->slock, flags);
+       int ret = 0;
 
        switch (ctrl->id) {
        case V4L2_CID_HFLIP:
@@ -1084,23 +1161,30 @@ int fimc_s_ctrl(struct fimc_ctx *ctx, struct v4l2_control *ctrl)
                break;
 
        case V4L2_CID_ROTATE:
+               if (fimc_ctx_state_is_set(FIMC_DST_FMT | FIMC_SRC_FMT, ctx)) {
+                       ret = fimc_check_scaler_ratio(ctx->s_frame.width,
+                                       ctx->s_frame.height, ctx->d_frame.width,
+                                       ctx->d_frame.height, ctrl->value);
+               }
+
+               if (ret) {
+                       v4l2_err(&fimc->m2m.v4l2_dev, "Out of scaler range\n");
+                       return -EINVAL;
+               }
+
                /* Check for the output rotator availability */
                if ((ctrl->value == 90 || ctrl->value == 270) &&
-                   (ctx->in_path == FIMC_DMA && !variant->has_out_rot)) {
-                       spin_unlock_irqrestore(&ctx->slock, flags);
+                   (ctx->in_path == FIMC_DMA && !variant->has_out_rot))
                        return -EINVAL;
-               } else {
-                       ctx->rotation = ctrl->value;
-               }
+               ctx->rotation = ctrl->value;
                break;
 
        default:
-               spin_unlock_irqrestore(&ctx->slock, flags);
                v4l2_err(&fimc->m2m.v4l2_dev, "Invalid control\n");
                return -EINVAL;
        }
-       ctx->state |= FIMC_PARAMS;
-       spin_unlock_irqrestore(&ctx->slock, flags);
+
+       fimc_ctx_state_lock_set(FIMC_PARAMS, ctx);
 
        return 0;
 }
@@ -1160,6 +1244,7 @@ int fimc_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;
+       bool is_capture_ctx;
        int i;
 
        if (cr->c.top < 0 || cr->c.left < 0) {
@@ -1168,10 +1253,12 @@ int fimc_try_crop(struct fimc_ctx *ctx, struct v4l2_crop *cr)
                return -EINVAL;
        }
 
+       is_capture_ctx = fimc_ctx_state_is_set(FIMC_CTX_CAP, ctx);
+
        if (cr->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
-               f = (ctx->state & FIMC_CTX_CAP) ? &ctx->s_frame : &ctx->d_frame;
+               f = is_capture_ctx ? &ctx->s_frame : &ctx->d_frame;
        else if (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
-                ctx->state & FIMC_CTX_M2M)
+                !is_capture_ctx)
                f = &ctx->s_frame;
        else
                return -EINVAL;
@@ -1179,15 +1266,15 @@ int fimc_try_crop(struct fimc_ctx *ctx, struct v4l2_crop *cr)
        min_size = (f == &ctx->s_frame) ?
                fimc->variant->min_inp_pixsize : fimc->variant->min_out_pixsize;
 
-       if (ctx->state & FIMC_CTX_M2M) {
+       /* Get pixel alignment constraints. */
+       if (is_capture_ctx) {
+               min_size = 16;
+               halign = 4;
+       } else {
                if (fimc->id == 1 && fimc->variant->pix_hoff)
                        halign = fimc_fmt_is_rgb(f->fmt->color) ? 0 : 1;
                else
                        halign = ffs(min_size) - 1;
-       /* there are more strict aligment requirements at camera interface */
-       } else {
-               min_size = 16;
-               halign = 4;
        }
 
        for (i = 0; i < f->fmt->colplanes; i++)
@@ -1205,8 +1292,7 @@ int fimc_try_crop(struct fimc_ctx *ctx, struct v4l2_crop *cr)
                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,
-                               ctx->state & FIMC_CTX_M2M ? 8 : 16);
+       cr->c.top  = round_down(cr->c.top, is_capture_ctx ? 16 : 8);
 
        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,
@@ -1215,12 +1301,10 @@ int fimc_try_crop(struct fimc_ctx *ctx, struct v4l2_crop *cr)
        return 0;
 }
 
-
 static int fimc_m2m_s_crop(struct file *file, void *fh, struct v4l2_crop *cr)
 {
        struct fimc_ctx *ctx = file->private_data;
        struct fimc_dev *fimc = ctx->fimc_dev;
-       unsigned long flags;
        struct fimc_frame *f;
        int ret;
 
@@ -1231,27 +1315,32 @@ static int fimc_m2m_s_crop(struct file *file, void *fh, struct v4l2_crop *cr)
        f = (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) ?
                &ctx->s_frame : &ctx->d_frame;
 
-       spin_lock_irqsave(&ctx->slock, flags);
-       if (~ctx->state & (FIMC_SRC_FMT | FIMC_DST_FMT)) {
-               /* Check to see if scaling ratio is within supported range */
-               if (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
-                       ret = fimc_check_scaler_ratio(&cr->c, &ctx->d_frame);
-               else
-                       ret = fimc_check_scaler_ratio(&cr->c, &ctx->s_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(cr->c.width, cr->c.height,
+                                                     ctx->d_frame.width,
+                                                     ctx->d_frame.height,
+                                                     ctx->rotation);
+               } else {
+                       ret = fimc_check_scaler_ratio(ctx->s_frame.width,
+                                                     ctx->s_frame.height,
+                                                     cr->c.width, cr->c.height,
+                                                     ctx->rotation);
+               }
                if (ret) {
-                       v4l2_err(&fimc->m2m.v4l2_dev, "Out of scaler range");
-                       spin_unlock_irqrestore(&ctx->slock, flags);
+                       v4l2_err(&fimc->m2m.v4l2_dev, "Out of scaler range\n");
                        return -EINVAL;
                }
        }
-       ctx->state |= FIMC_PARAMS;
 
        f->offs_h = cr->c.left;
        f->offs_v = cr->c.top;
        f->width  = cr->c.width;
        f->height = cr->c.height;
 
-       spin_unlock_irqrestore(&ctx->slock, flags);
+       fimc_ctx_state_lock_set(FIMC_PARAMS, ctx);
+
        return 0;
 }
 
@@ -1519,6 +1608,7 @@ static int fimc_probe(struct platform_device *pdev)
        struct fimc_dev *fimc;
        struct resource *res;
        struct samsung_fimc_driverdata *drv_data;
+       struct s5p_platform_fimc *pdata;
        int ret = 0;
        int cap_input_index = -1;
 
@@ -1540,7 +1630,8 @@ static int fimc_probe(struct platform_device *pdev)
        fimc->id = pdev->id;
        fimc->variant = drv_data->variant[fimc->id];
        fimc->pdev = pdev;
-       fimc->pdata = pdev->dev.platform_data;
+       pdata = pdev->dev.platform_data;
+       fimc->pdata = pdata;
        fimc->state = ST_IDLE;
 
        init_waitqueue_head(&fimc->irq_queue);
@@ -1571,19 +1662,11 @@ static int fimc_probe(struct platform_device *pdev)
        }
 
        fimc->num_clocks = MAX_FIMC_CLOCKS - 1;
-       /*
-        * Check if vide capture node needs to be registered for this device
-        * instance.
-        */
-       if (fimc->pdata) {
-               int i;
-               for (i = 0; i < FIMC_MAX_CAMIF_CLIENTS; ++i)
-                       if (fimc->pdata->isp_info[i])
-                               break;
-               if (i < FIMC_MAX_CAMIF_CLIENTS) {
-                       cap_input_index = i;
-                       fimc->num_clocks++;
-               }
+
+       /* Check if a video capture node needs to be registered. */
+       if (pdata && pdata->num_clients > 0) {
+               cap_input_index = 0;
+               fimc->num_clocks++;
        }
 
        ret = fimc_clk_get(fimc);
@@ -1679,7 +1762,7 @@ static int __devexit fimc_remove(struct platform_device *pdev)
 }
 
 /* Image pixel limits, similar across several FIMC HW revisions. */
-static struct fimc_pix_limit s5p_pix_limit[3] = {
+static struct fimc_pix_limit s5p_pix_limit[4] = {
        [0] = {
                .scaler_en_w    = 3264,
                .scaler_dis_w   = 8192,
@@ -1704,6 +1787,14 @@ static struct fimc_pix_limit s5p_pix_limit[3] = {
                .out_rot_en_w   = 1280,
                .out_rot_dis_w  = 1920,
        },
+       [3] = {
+               .scaler_en_w    = 1920,
+               .scaler_dis_w   = 8192,
+               .in_rot_en_h    = 1366,
+               .in_rot_dis_w   = 8192,
+               .out_rot_en_w   = 1366,
+               .out_rot_dis_w  = 1920,
+       },
 };
 
 static struct samsung_fimc_variant fimc0_variant_s5p = {
@@ -1756,7 +1847,7 @@ static struct samsung_fimc_variant fimc2_variant_s5pv210 = {
        .pix_limit       = &s5p_pix_limit[2],
 };
 
-static struct samsung_fimc_variant fimc0_variant_s5pv310 = {
+static struct samsung_fimc_variant fimc0_variant_exynos4 = {
        .pix_hoff        = 1,
        .has_inp_rot     = 1,
        .has_out_rot     = 1,
@@ -1769,7 +1860,7 @@ static struct samsung_fimc_variant fimc0_variant_s5pv310 = {
        .pix_limit       = &s5p_pix_limit[1],
 };
 
-static struct samsung_fimc_variant fimc2_variant_s5pv310 = {
+static struct samsung_fimc_variant fimc2_variant_exynos4 = {
        .pix_hoff        = 1,
        .has_cistatus2   = 1,
        .has_mainscaler_ext = 1,
@@ -1777,7 +1868,7 @@ static struct samsung_fimc_variant fimc2_variant_s5pv310 = {
        .min_out_pixsize = 16,
        .hor_offs_align  = 1,
        .out_buf_count   = 32,
-       .pix_limit       = &s5p_pix_limit[2],
+       .pix_limit       = &s5p_pix_limit[3],
 };
 
 /* S5PC100 */
@@ -1803,12 +1894,12 @@ static struct samsung_fimc_driverdata fimc_drvdata_s5pv210 = {
 };
 
 /* S5PV310, S5PC210 */
-static struct samsung_fimc_driverdata fimc_drvdata_s5pv310 = {
+static struct samsung_fimc_driverdata fimc_drvdata_exynos4 = {
        .variant = {
-               [0] = &fimc0_variant_s5pv310,
-               [1] = &fimc0_variant_s5pv310,
-               [2] = &fimc0_variant_s5pv310,
-               [3] = &fimc2_variant_s5pv310,
+               [0] = &fimc0_variant_exynos4,
+               [1] = &fimc0_variant_exynos4,
+               [2] = &fimc0_variant_exynos4,
+               [3] = &fimc2_variant_exynos4,
        },
        .num_entities = 4,
        .lclk_frequency = 166000000UL,
@@ -1822,8 +1913,8 @@ static struct platform_device_id fimc_driver_ids[] = {
                .name           = "s5pv210-fimc",
                .driver_data    = (unsigned long)&fimc_drvdata_s5pv210,
        }, {
-               .name           = "s5pv310-fimc",
-               .driver_data    = (unsigned long)&fimc_drvdata_s5pv310,
+               .name           = "exynos4-fimc",
+               .driver_data    = (unsigned long)&fimc_drvdata_exynos4,
        },
        {},
 };