camera: intel: ipu6: Detect the same sensor based on availableSensors

1. Detect the same sensor based on availableSensors
2. Detect VCM dynamically based on i2c bus.
3. Use flush mode for tnr buffer whend disable compression
4. Enable touch AE

BUG=b:149068439, b:149068672
TEST=Full tested pass for camera functions.

Change-Id: Id54bc200f2aac010717c7bab34b764272aa99900
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/camera/+/2772290
Tested-by: Zong Li <zong.li@intel.corp-partner.google.com>
Tested-by: Ren-Pei Zeng <kamesan@chromium.org>
Reviewed-by: Ren-Pei Zeng <kamesan@chromium.org>
Commit-Queue: Ren-Pei Zeng <kamesan@chromium.org>
diff --git a/hal/intel/ipu6/aal/MetadataConvert.cpp b/hal/intel/ipu6/aal/MetadataConvert.cpp
index 8ec98d3..edf0de9 100644
--- a/hal/intel/ipu6/aal/MetadataConvert.cpp
+++ b/hal/intel/ipu6/aal/MetadataConvert.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017-2020 Intel Corporation
+ * Copyright (C) 2017-2021 Intel Corporation
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -756,6 +756,7 @@
     icamera::camera_window_list_t windows;
     if (entry.count > 0) {
         if (convertToHalWindow(entry.data.i32, entry.count, &windows) == 0) {
+            parameter->setBlcAreaMode(icamera::BLC_AREA_MODE_ON);
             parameter->setAeRegions(windows);
         }
     }
diff --git a/hal/intel/ipu6/src/core/psysprocessor/PGCommon.cpp b/hal/intel/ipu6/src/core/psysprocessor/PGCommon.cpp
index e15f3ce..4cf3cd7 100644
--- a/hal/intel/ipu6/src/core/psysprocessor/PGCommon.cpp
+++ b/hal/intel/ipu6/src/core/psysprocessor/PGCommon.cpp
@@ -981,9 +981,9 @@
 
         CIPR::Buffer* ciprBuf = nullptr;
         /* use flush mode for still stream tnr buffers, since still stream will copy data buffer
-         * from video stream as its refer buffer, flush the cache after the copy to fix
-         * garbage image in compression mode */
-        if (mStreamId == STILL_STREAM_ID && isCompression)
+         * from video stream as its refer buffer.
+         */
+        if (mStreamId == STILL_STREAM_ID)
             ciprBuf = registerUserBuffer(size, buffer, true);
         else
             ciprBuf = registerUserBuffer(size, buffer);
diff --git a/hal/intel/ipu6/src/platformdata/CameraParser.cpp b/hal/intel/ipu6/src/platformdata/CameraParser.cpp
index 3f3bd52..94c3678 100644
--- a/hal/intel/ipu6/src/platformdata/CameraParser.cpp
+++ b/hal/intel/ipu6/src/platformdata/CameraParser.cpp
@@ -145,6 +145,48 @@
 }
 
 /**
+* Get CSI port and I2C bus address according to current sensor name
+*
+* \param profiles: the pointer of the CameraParser.
+*/
+void CameraParser::getCsiPortAndI2CBus(CameraParser *profiles)
+{
+    std::string fullSensorName = profiles->pCurrentCam->sensorName;
+    if (fullSensorName.empty()) {
+        LOGXML("@%s, Faild to find sensorName", __func__);
+        return;
+    }
+
+    for (auto availableSensorTmp : profiles->mAvailableSensor) {
+        AvailableSensorInfo *sensorInfo = &availableSensorTmp.second;
+        if ((availableSensorTmp.first.find(fullSensorName) != string::npos) &&
+            (sensorInfo->sensorFlag != true)) {
+            /* parameters information format example:
+               sinkEntityName is "Intel IPU6 CSI-2 1"
+               profiles->pCurrentCam->sensorName is "ov8856-wf" or "ov8856"
+               sensorName is "ov8856"
+            */
+            string sinkEntityName = sensorInfo->sinkEntityName;
+            sensorInfo->sensorFlag = true;
+            profiles->mCsiPort =
+                            sinkEntityName.substr(sinkEntityName.find_last_of(' ') + 1);
+
+            string sensorName = fullSensorName;
+            if (sensorName.find_first_of('-') != string::npos)
+                sensorName = fullSensorName.substr(0, (sensorName.find_first_of('-')));
+
+            profiles->mMC->getI2CBusAddress(sensorName, sinkEntityName, &profiles->mI2CBus);
+
+            LOGXML("@%s, mI2CBus:%s, cisPort:%s", __func__, profiles->mI2CBus.c_str(),
+                   profiles->mCsiPort.c_str());
+            break;
+        }
+    }
+
+    return;
+}
+
+/**
  * This function will check which field that the parser parses to.
  *
  * The field is set to 3 types.
@@ -181,24 +223,7 @@
                 idx += 2;
             }
 
-            if (!profiles->pCurrentCam->sensorName.empty() &&
-                (profiles->mAvailableSensor.find(profiles->pCurrentCam->sensorName) !=
-                 profiles->mAvailableSensor.end())) {
-                /* parameters information format example:
-                   sinkEntityName is "Intel IPU6 CSI-2 1"
-                   profiles->pCurrentCam->sensorName is "ov8856-wf"
-                   sensorName is "ov8856"
-                */
-                string sinkEntityName = profiles->mAvailableSensor[profiles->pCurrentCam->sensorName];
-                profiles->mCsiPort = sinkEntityName.substr(sinkEntityName.find_last_of(' ') + 1);
-                string sensorName = profiles->pCurrentCam->sensorName;
-                sensorName = sensorName.substr(0, (sensorName.find_last_of('-')));
-                profiles->mMC->getI2CBusAddress(sensorName, sinkEntityName, &profiles->mI2CBus);
-
-                LOGXML("@%s, mI2CBus:%s, cisPort:%s", __func__,
-                       profiles->mI2CBus.c_str(), profiles->mCsiPort.c_str());
-             }
-
+            getCsiPortAndI2CBus(profiles);
             profiles->mMetadata.clear();
             profiles->mCurrentDataField = FIELD_SENSOR;
 
@@ -302,7 +327,13 @@
     } else if (strcmp(name, "enableFrameSyncCheck") == 0) {
         pCurrentCam->mFrameSyncCheckEnabled = strcmp(atts[1], "true") == 0;
     } else if (strcmp(name, "lensName") == 0) {
-        profiles->mMC->getVCMI2CAddr(atts[1], &pCurrentCam->mLensName);
+        string vcmName = atts[1];
+        if (!profiles->mI2CBus.empty()) {
+            int i2cBusId = atoi(profiles->mI2CBus.c_str());
+            vcmName.append(" ");
+            vcmName.append(std::to_string(i2cBusId));
+        }
+        profiles->mMC->getVCMI2CAddr(vcmName.c_str(), &pCurrentCam->mLensName);
     } else if (strcmp(name, "lensHwType") == 0) {
         if (strcmp(atts[1], "LENS_VCM_HW") == 0) {
             pCurrentCam->mLensHwType = LENS_VCM_HW;
@@ -1828,8 +1859,10 @@
     LOGXML("@%s", __func__);
 
     std::string nvmPath("/sys/bus/i2c/devices/i2c-");
-    // attach i2c adaptor id
-    nvmPath += profiles->mI2CBus.substr(0,2);
+    // attach i2c adaptor id, like 18-0010
+    std::size_t found = profiles->mI2CBus.find("-");
+    CheckError(found == std::string::npos, VOID_VALUE, "Failed to get adaptor id");
+    nvmPath += profiles->mI2CBus.substr(0,found);
     nvmPath += "/firmware_node/";
     DIR* dir = opendir(nvmPath.c_str());
     if (dir) {
@@ -1903,11 +1936,11 @@
 
         bool ret = mMC->checkAvailableSensor(sensorName, sensorSinkNameTmp);
         if (ret) {
-            std::string sensorNameTmp = srcSensor.substr(0, srcSensor.find_last_of('-'));
-            availableSensors.push_back(sensorNameTmp);
-            mAvailableSensor[sensorNameTmp] = sensorSinkNameTmp;
+            AvailableSensorInfo sensorInfo = {sensorSinkNameTmp, false};
+            availableSensors.push_back(srcSensor);
+            mAvailableSensor[srcSensor] = sensorInfo;
             LOGXML("@%s, The availabel sensor name:%s, sensorSinkNameTmp:%s",
-                   __func__, sensorNameTmp.c_str(), sensorSinkNameTmp.c_str());
+                   __func__, srcSensor.c_str(), sensorSinkNameTmp.c_str());
         }
     }
 
@@ -1938,7 +1971,12 @@
 
     for (auto sensor : allSensors) {
         string sensorName = "sensors/";
-        sensorName.append(sensor);
+        if ((sensor.find("-wf-") != string::npos) ||
+            (sensor.find("-uf-") != string::npos)) {
+            sensorName.append(sensor.substr(0, sensor.find_last_of('-')));
+        } else {
+            sensorName.append(sensor);
+        }
         sensorName.append(".xml");
         int ret = getDataFromXmlFile(sensorName);
         CheckError(ret != OK, VOID_VALUE, "Failed to get sensor profile data from %s", sensorName.c_str());
diff --git a/hal/intel/ipu6/src/platformdata/CameraParser.h b/hal/intel/ipu6/src/platformdata/CameraParser.h
index af69586..25ece2e 100644
--- a/hal/intel/ipu6/src/platformdata/CameraParser.h
+++ b/hal/intel/ipu6/src/platformdata/CameraParser.h
@@ -63,7 +63,11 @@
     int mCurrentSensor;
     std::string mI2CBus;
     std::string mCsiPort;
-    std::unordered_map<std::string, std::string> mAvailableSensor;
+    struct AvailableSensorInfo {
+        std::string sinkEntityName;
+        bool sensorFlag;
+    };
+    std::unordered_map<std::string, AvailableSensorInfo> mAvailableSensor;
     PlatformData::StaticCfg::CameraInfo *pCurrentCam;
     bool mInMediaCtlCfg;
     bool mInStaticMetadata;
@@ -88,6 +92,7 @@
     std::vector<std::string> getAvailableSensors(const std::string &ipuName,
                                                  const std::vector<std::string> &sensorsList);
     void getSensorDataFromXmlFile(void);
+    void getCsiPortAndI2CBus(CameraParser *profiles);
     void checkField(CameraParser *profiles, const char *name, const char **atts);
 
     void handleSensor(CameraParser *profiles, const char *name, const char **atts);