MLK-19913 media: csi: fix corrupted image issue when restart CSI bridge

Corrupted image is observed on the capture when do the resolution switch.
This usually happens with base address switch error (BIT_ADDR_CH_ERR_INT).
skip the frame if the frame index does not correspond to the requested one
to resolve it.

This patch was proposed by Tom Zheng <haidong.zheng@nxp.com>

Change-Id: Ifdf9474496c56861dc300bb8ee2105ec968b7784
Signed-off-by: Robby Cai <robby.cai@nxp.com>
(cherry picked from commit 91083be310df6595bf606cc12d0501b7cdad832e)
diff --git a/drivers/media/platform/mxc/capture/mx6s_capture.c b/drivers/media/platform/mxc/capture/mx6s_capture.c
index 47867cd..1de67a9 100644
--- a/drivers/media/platform/mxc/capture/mx6s_capture.c
+++ b/drivers/media/platform/mxc/capture/mx6s_capture.c
@@ -320,6 +320,8 @@
 	void __iomem *regbase;
 	int irq;
 
+	u32      nextfb;
+	u32      skipframe;
 	u32	 type;
 	u32 bytesperline;
 	v4l2_std_id std;
@@ -749,6 +751,7 @@
 	unsigned long val;
 	int timeout, timeout2;
 
+	csi_dev->skipframe = 0;
 	csisw_reset(csi_dev);
 
 	if (pix->field == V4L2_FIELD_INTERLACED)
@@ -943,6 +946,8 @@
 	mx6s_update_csi_buf(csi_dev, phys, buf->internal.bufnum);
 	list_move_tail(csi_dev->capture.next, &csi_dev->active_bufs);
 
+	csi_dev->nextfb = 0;
+
 	spin_unlock_irqrestore(&csi_dev->slock, flags);
 
 	return mx6s_csi_enable(csi_dev);
@@ -1048,6 +1053,7 @@
 	}
 
 	csi_dev->frame_count++;
+	csi_dev->nextfb = (bufnum == 0 ? 1: 0);
 
 	/* Config discard buffer to active_bufs */
 	if (list_empty(&csi_dev->capture)) {
@@ -1130,6 +1136,7 @@
 		cr18 |= BIT_CSI_ENABLE;
 		csi_write(csi_dev, cr18, CSI_CSICR18);
 
+		csi_dev->skipframe = 1;
 		pr_debug("base address switching Change Err.\n");
 	}
 
@@ -1144,9 +1151,22 @@
 		 * PDM TKT230775 */
 		pr_debug("Skip two frames\n");
 	} else if (status & BIT_DMA_TSF_DONE_FB1) {
-		mx6s_csi_frame_done(csi_dev, 0, false);
+		if (csi_dev->nextfb == 0) {
+			if (csi_dev->skipframe > 0)
+				csi_dev->skipframe--;
+			else
+				mx6s_csi_frame_done(csi_dev, 0, false);
+		} else
+			pr_warn("skip frame 0 \n");
+
 	} else if (status & BIT_DMA_TSF_DONE_FB2) {
-		mx6s_csi_frame_done(csi_dev, 1, false);
+		if (csi_dev->nextfb == 1) {
+			if (csi_dev->skipframe > 0)
+				csi_dev->skipframe--;
+			else
+				mx6s_csi_frame_done(csi_dev, 1, false);
+		} else
+			pr_warn("skip frame 1 \n");
 	}
 
 	spin_unlock(&csi_dev->slock);