hal: add support for app type config in HFP call

Add support for HFP calibration with two ASM loopbacks
according to app type configuration.

Change-Id: Ie3e7a75ef204556fa230f2767a791587c6b80e6a
diff --git a/hal/Android.mk b/hal/Android.mk
index fc2be7d..781e573 100644
--- a/hal/Android.mk
+++ b/hal/Android.mk
@@ -60,6 +60,7 @@
 	platform_info.c \
 	audio_extn/ext_speaker.c \
 	audio_extn/audio_extn.c \
+	audio_extn/utils.c \
 	$(AUDIO_PLATFORM)/platform.c
 
 ifdef MULTIPLE_HW_VARIANTS_ENABLED
diff --git a/hal/audio_extn/audio_extn.h b/hal/audio_extn/audio_extn.h
index 5ce3093..4758fc8 100644
--- a/hal/audio_extn/audio_extn.h
+++ b/hal/audio_extn/audio_extn.h
@@ -105,6 +105,9 @@
                          bool benable);
 #endif
 
+void audio_extn_utils_send_default_app_type_cfg(void *platform, struct mixer *mixer);
+int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
+                                       struct audio_usecase *usecase);
 #ifndef HWDEP_CAL_ENABLED
 #define  audio_extn_hwdep_cal_send(snd_card, acdb_handle) (0)
 #else
diff --git a/hal/audio_extn/utils.c b/hal/audio_extn/utils.c
new file mode 100644
index 0000000..d2bb42c
--- /dev/null
+++ b/hal/audio_extn/utils.c
@@ -0,0 +1,179 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "audio_hw_utils"
+//#define LOG_NDEBUG 0
+
+#include <errno.h>
+#include <cutils/properties.h>
+#include <cutils/config_utils.h>
+#include <stdlib.h>
+#include <dlfcn.h>
+#include <cutils/str_parms.h>
+#include <cutils/log.h>
+#include <cutils/misc.h>
+
+#include "audio_hw.h"
+#include "platform.h"
+#include "platform_api.h"
+#include "audio_extn.h"
+
+#define MAX_LENGTH_MIXER_CONTROL_IN_INT 128
+
+static int set_mixer_ctrl(struct audio_device *adev,
+                          int pcm_device_id, int app_type,
+                          int acdb_dev_id, int sample_rate, int stream_type)
+{
+
+    char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
+    struct mixer_ctl *ctl;
+    int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
+
+    if (stream_type == PCM_PLAYBACK) {
+        snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
+             "Audio Stream %d App Type Cfg", pcm_device_id);
+    } else if (stream_type == PCM_CAPTURE) {
+        snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
+             "Audio Stream Capture %d App Type Cfg", pcm_device_id);
+    }
+
+    ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
+    if (!ctl) {
+        ALOGE("%s: Could not get ctl for mixer cmd - %s",
+             __func__, mixer_ctl_name);
+        rc = -EINVAL;
+        goto exit;
+    }
+    app_type_cfg[len++] = app_type;
+    app_type_cfg[len++] = acdb_dev_id;
+    app_type_cfg[len++] = sample_rate;
+    ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d sample rate %d",
+          __func__, stream_type, app_type, acdb_dev_id, sample_rate);
+    mixer_ctl_set_array(ctl, app_type_cfg, len);
+
+exit:
+    return rc;
+}
+
+void audio_extn_utils_send_default_app_type_cfg(void *platform, struct mixer *mixer)
+{
+    int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {-1};
+    int length = 0, app_type = 0,rc = 0;
+    struct mixer_ctl *ctl = NULL;
+    const char *mixer_ctl_name = "App Type Config";
+
+    ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
+    if (!ctl) {
+        ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
+        return;
+    }
+    rc = platform_get_default_app_type_v2(platform, PCM_PLAYBACK, &app_type);
+    if (rc == 0) {
+        app_type_cfg[length++] = 1;
+        app_type_cfg[length++] = app_type;
+        app_type_cfg[length++] = 48000;
+        app_type_cfg[length++] = 16;
+        mixer_ctl_set_array(ctl, app_type_cfg, length);
+    }
+    return;
+}
+
+int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
+                                       struct audio_usecase *usecase)
+{
+    struct mixer_ctl *ctl;
+    int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
+    int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
+    int app_type = 0, rc = 0;
+
+    ALOGV("%s", __func__);
+
+    if (usecase->type != PCM_HFP_CALL) {
+        ALOGV("%s: not a playback or HFP path, no need to cfg app type", __func__);
+        rc = 0;
+        goto exit_send_app_type_cfg;
+    }
+    if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
+        (usecase->id != USECASE_AUDIO_HFP_SCO_WB)) {
+        ALOGV("%s: a playback path where app type cfg is not required", __func__);
+        rc = 0;
+        goto exit_send_app_type_cfg;
+    }
+
+    snd_device = usecase->out_snd_device;
+    pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
+
+    snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
+                 audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device;
+    acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
+    if (acdb_dev_id < 0) {
+        ALOGE("%s: Couldn't get the acdb dev id", __func__);
+        rc = -EINVAL;
+        goto exit_send_app_type_cfg;
+    }
+
+    if (usecase->type == PCM_HFP_CALL) {
+
+        /* config HFP session:1 playback path */
+        rc = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK, &app_type);
+        if (rc < 0)
+            goto exit_send_app_type_cfg;
+
+        sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
+        rc = set_mixer_ctrl(adev, pcm_device_id, app_type,
+               acdb_dev_id, sample_rate, PCM_PLAYBACK);
+        if (rc < 0)
+            goto exit_send_app_type_cfg;
+        /* config HFP session:1 capture path */
+        rc = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE, &app_type);
+
+        if (rc == 0) {
+            rc = set_mixer_ctrl(adev, pcm_device_id, app_type,
+                   acdb_dev_id, sample_rate, PCM_CAPTURE);
+            if (rc < 0)
+                goto exit_send_app_type_cfg;
+        }
+        /* config HFP session:2 capture path */
+        pcm_device_id = HFP_ASM_RX_TX;
+        snd_device = usecase->in_snd_device;
+        acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
+        if (acdb_dev_id <= 0) {
+            ALOGE("%s: Couldn't get the acdb dev id", __func__);
+            rc = -EINVAL;
+            goto exit_send_app_type_cfg;
+        }
+        rc = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE, &app_type);
+        if (rc == 0) {
+            rc = set_mixer_ctrl(adev, pcm_device_id, app_type,
+                   acdb_dev_id, sample_rate, PCM_CAPTURE);
+            if (rc < 0)
+                goto exit_send_app_type_cfg;
+        }
+
+        /* config HFP session:2 playback path */
+        rc = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK, &app_type);
+        if (rc == 0) {
+            rc = set_mixer_ctrl(adev, pcm_device_id, app_type,
+                   acdb_dev_id, sample_rate, PCM_PLAYBACK);
+            if (rc < 0)
+                goto exit_send_app_type_cfg;
+        }
+    }
+
+    rc = 0;
+exit_send_app_type_cfg:
+    return rc;
+}
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 8585a76..411ee25 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -547,6 +547,7 @@
     else
         snd_device = usecase->out_snd_device;
 
+    audio_extn_utils_send_app_type_cfg(adev, usecase);
     strcpy(mixer_path, use_case_table[usecase->id]);
     platform_add_backend_name(adev->platform, mixer_path, snd_device);
     ALOGD("%s: usecase(%d) apply and update mixer path: %s", __func__,  usecase->id, mixer_path);
@@ -3704,6 +3705,7 @@
         }
     }
 
+    audio_extn_utils_send_default_app_type_cfg(adev->platform, adev->mixer);
     audio_device_ref_count++;
 
     if (property_get("audio_hal.period_multiplier", value, NULL) > 0) {
diff --git a/hal/msm8916/platform.c b/hal/msm8916/platform.c
index a5a2c9b..354faa3 100644
--- a/hal/msm8916/platform.c
+++ b/hal/msm8916/platform.c
@@ -56,6 +56,10 @@
 /* EDID format ID for LPCM audio */
 #define EDID_FORMAT_LPCM    1
 
+/* fallback app type if the default app type from acdb loader fails */
+#define DEFAULT_APP_TYPE_RX_PATH  0x11130
+#define DEFAULT_APP_TYPE_TX_PATH  0x11132
+
 /* Retry for delay in FW loading*/
 #define RETRY_NUMBER 20
 #define RETRY_US 500000
@@ -89,6 +93,11 @@
     CAL_MODE_RTAC           = 0x4
 };
 
+enum {
+    BUFF_IDX_0 = 0,
+    BUFF_IDX_1 = 1,
+};
+
 #define PLATFORM_CONFIG_KEY_OPERATOR_INFO "operator_info"
 
 struct operator_info {
@@ -111,6 +120,7 @@
 typedef void (*acdb_deallocate_t)();
 typedef int  (*acdb_init_v2_cvd_t)(const char *, char *, int);
 typedef void (*acdb_send_audio_cal_t)(int, int);
+typedef void (*acdb_send_audio_cal_v3_t)(int, int, int , int, int);
 typedef void (*acdb_send_voice_cal_t)(int, int);
 typedef int (*acdb_reload_vocvoltable_t)(int);
 typedef int (*acdb_loader_get_calibration_t)(char *attr, int size, void *data);
@@ -131,6 +141,7 @@
     acdb_init_v2_cvd_t         acdb_init;
     acdb_deallocate_t          acdb_deallocate;
     acdb_send_audio_cal_t      acdb_send_audio_cal;
+    acdb_send_audio_cal_v3_t   acdb_send_audio_cal_v3;
     acdb_send_voice_cal_t      acdb_send_voice_cal;
     acdb_reload_vocvoltable_t  acdb_reload_vocvoltable;
     void *hw_info;
@@ -942,6 +953,12 @@
             ALOGE("%s: Could not find the symbol acdb_loader_deallocate_ACDB from %s",
                   __func__, LIB_ACDB_LOADER);
 
+        my_data->acdb_send_audio_cal_v3 = (acdb_send_audio_cal_t)dlsym(my_data->acdb_handle,
+                                                    "acdb_loader_send_audio_cal_v3");
+        if (!my_data->acdb_send_audio_cal_v3)
+            ALOGE("%s: Could not find the symbol acdb_send_audio_cal from %s",
+                  __func__, LIB_ACDB_LOADER);
+
         my_data->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(my_data->acdb_handle,
                                                     "acdb_loader_send_audio_cal");
         if (!my_data->acdb_send_audio_cal)
@@ -1226,6 +1243,20 @@
     return ret;
 }
 
+int platform_get_default_app_type_v2(void *platform, usecase_type_t type, int *app_type)
+{
+    ALOGV("%s: platform: %p, type: %d", __func__, platform, type);
+    int rc = 0;
+    if (type == PCM_CAPTURE) {
+        *app_type = DEFAULT_APP_TYPE_TX_PATH;
+    } else if (type == PCM_PLAYBACK) {
+        *app_type =  DEFAULT_APP_TYPE_RX_PATH;
+    } else {
+        rc = -EINVAL;
+    }
+    return rc;
+}
+
 int platform_get_snd_device_acdb_id(snd_device_t snd_device)
 {
     if ((snd_device < SND_DEVICE_MIN) || (snd_device >= SND_DEVICE_MAX)) {
@@ -1243,6 +1274,7 @@
 {
     struct platform_data *my_data = (struct platform_data *)platform;
     int acdb_dev_id, acdb_dev_type;
+    int sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
 
     acdb_dev_id = acdb_device_table[audio_extn_get_spkr_prot_snd_device(snd_device)];
     if (acdb_dev_id < 0) {
@@ -1250,14 +1282,28 @@
               __func__, snd_device);
         return -EINVAL;
     }
-    if (my_data->acdb_send_audio_cal) {
         ALOGV("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
               __func__, snd_device, acdb_dev_id);
-        if (snd_device >= SND_DEVICE_OUT_BEGIN &&
-                snd_device < SND_DEVICE_OUT_END)
-            acdb_dev_type = ACDB_DEV_TYPE_OUT;
-        else
-            acdb_dev_type = ACDB_DEV_TYPE_IN;
+    if (snd_device >= SND_DEVICE_OUT_BEGIN && snd_device < SND_DEVICE_OUT_END)
+        acdb_dev_type = ACDB_DEV_TYPE_OUT;
+    else
+        acdb_dev_type = ACDB_DEV_TYPE_IN;
+
+    if ((my_data->acdb_send_audio_cal_v3) && (snd_device == SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP)) {
+            /* TX path calibration */
+            my_data->acdb_send_audio_cal_v3(acdb_dev_id, ACDB_DEV_TYPE_IN,
+                                DEFAULT_APP_TYPE_TX_PATH, sample_rate, BUFF_IDX_0);
+            my_data->acdb_send_audio_cal_v3(acdb_dev_id, ACDB_DEV_TYPE_OUT,
+                                DEFAULT_APP_TYPE_RX_PATH, sample_rate, BUFF_IDX_0);
+    } else if ((my_data->acdb_send_audio_cal_v3) && (snd_device == SND_DEVICE_OUT_VOICE_SPEAKER_HFP)) {
+            /* RX path calibration */
+            ALOGV("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
+                       __func__, snd_device, acdb_dev_id);
+            my_data->acdb_send_audio_cal_v3(acdb_dev_id, ACDB_DEV_TYPE_IN,
+                                DEFAULT_APP_TYPE_TX_PATH, sample_rate, BUFF_IDX_1);
+            my_data->acdb_send_audio_cal_v3(acdb_dev_id, ACDB_DEV_TYPE_OUT,
+                                DEFAULT_APP_TYPE_RX_PATH, sample_rate, BUFF_IDX_1);
+    } else if (my_data->acdb_send_audio_cal) {
         my_data->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type);
     }
     return 0;
diff --git a/hal/msm8960/platform.c b/hal/msm8960/platform.c
index d7fc84c..61a1a58 100644
--- a/hal/msm8960/platform.c
+++ b/hal/msm8960/platform.c
@@ -421,6 +421,13 @@
     return -ENODEV;
 }
 
+int platform_get_default_app_type_v2(void *platform, usecase_type_t type __unused,
+                                     int *app_type __unused)
+{
+    ALOGE("%s: Not implemented", __func__);
+    return -ENOSYS;
+}
+
 int platform_get_snd_device_acdb_id(snd_device_t snd_device __unused)
 {
     ALOGE("%s: Not implemented", __func__);
diff --git a/hal/msm8974/platform.c b/hal/msm8974/platform.c
index 5cdc394..39796f2 100644
--- a/hal/msm8974/platform.c
+++ b/hal/msm8974/platform.c
@@ -1525,6 +1525,13 @@
     return ret;
 }
 
+int platform_get_default_app_type_v2(void *platform, usecase_type_t type __unused,
+                                     int *app_type)
+{
+    ALOGE("%s: Not implemented", __func__);
+    return -ENOSYS;
+}
+
 int platform_get_snd_device_acdb_id(snd_device_t snd_device)
 {
     if ((snd_device < SND_DEVICE_MIN) || (snd_device >= SND_DEVICE_MAX)) {
diff --git a/hal/platform_api.h b/hal/platform_api.h
index 41e600e..f8af0f8 100644
--- a/hal/platform_api.h
+++ b/hal/platform_api.h
@@ -19,6 +19,7 @@
 
 #include "voice.h"
 #define MAX_VOLUME_CAL_STEPS 15
+#define CODEC_BACKEND_DEFAULT_SAMPLE_RATE 48000
 
 struct amp_db_and_gain_table {
     float amp;
@@ -41,6 +42,7 @@
 int platform_set_snd_device_acdb_id(snd_device_t snd_device, unsigned int acdb_id);
 int platform_get_snd_device_acdb_id(snd_device_t snd_device);
 int platform_send_audio_calibration(void *platform, snd_device_t snd_device);
+int platform_get_default_app_type_v2(void *platform, usecase_type_t type, int *app_type);
 int platform_switch_voice_call_device_pre(void *platform);
 int platform_switch_voice_call_enable_device_config(void *platform,
                                                     snd_device_t out_snd_device,