ov5645: Fix OpenCV related issues

b/128424247

- OpenCV loses information about the resolution because it doesn't
  call G_PARM before calling S_PARM to set framerate and for OV5645
  the resolution is encoded in the capturemode.
- The way the csi host driver was implemented did not conform with
  the v4l2 spec and this patch fixes that, but only for the one
  csi camera case, which completely breaks the two camera case even
  when STREAMON/STREAMOFF come in a well coreographed pattern.
  TODO: Fix the two camera case in mxc-mipi-csi2-yav.c

Change-Id: I2d11a17f4f0574ff577dd1c3b30725fa0bd09a8a
diff --git a/drivers/media/platform/imx8/mxc-mipi-csi2_yav.c b/drivers/media/platform/imx8/mxc-mipi-csi2_yav.c
index 98829be..b365991 100644
--- a/drivers/media/platform/imx8/mxc-mipi-csi2_yav.c
+++ b/drivers/media/platform/imx8/mxc-mipi-csi2_yav.c
@@ -336,6 +336,10 @@
 	dev_dbg(&csi2dev->pdev->dev, "%s: %d, csi2dev: 0x%x\n",
 		__func__, enable, csi2dev->flags);
 
+	// TOOO: The below code only works for one CSI camera attached. The old
+	// code was broken for OpenCV, and not according to V4L2 spec, both for the
+	// one and two camera cases and this is a temp fix that takes care of the
+	// one camera case See b/128424247
 	if (enable) {
 		if (!csi2dev->running) {
 			pm_runtime_get_sync(dev);
@@ -345,16 +349,13 @@
 			mxc_mipi_csi2_reg_dump(csi2dev);
 		}
 		v4l2_subdev_call(sensor_sd, video, s_stream, true);
-		csi2dev->running++;
+		csi2dev->running = 1;
 
-	} else {
-
+	} else if (csi2dev->running){
 		v4l2_subdev_call(sensor_sd, video, s_stream, false);
-		csi2dev->running--;
-		if (!csi2dev->running) {
-			pm_runtime_put(dev);
-			mxc_mipi_csi2_disable(csi2dev);
-		}
+		csi2dev->running = 0;
+		pm_runtime_put(dev);
+		mxc_mipi_csi2_disable(csi2dev);
 	}
 
 	return ret;
diff --git a/drivers/media/platform/mxc/capture/ov5645_mipi_v2.c b/drivers/media/platform/mxc/capture/ov5645_mipi_v2.c
index 6c47f66..3c0e3d2 100644
--- a/drivers/media/platform/mxc/capture/ov5645_mipi_v2.c
+++ b/drivers/media/platform/mxc/capture/ov5645_mipi_v2.c
@@ -3149,6 +3149,13 @@
 			return -EINVAL;
 		}
 
+		// This check is necessary because OpenCV doesn't call G_PARM on the
+		// zeroed structure before setting FPS through S_PARM
+		if ((u32)a->parm.capture.capturemode == 0) {
+			a->parm.capture.capturemode = get_capturemode(ov5645_data.pix.width,
+				ov5645_data.pix.height);
+		}
+
 		orig_mode = ov5645_data.loaded_mode;
 		if ((orig_mode != (u32)a->parm.capture.capturemode) ||
 				(frame_rate != ov5645_data.loaded_fps)) {