Merge "hal: Backend port specific channel mapping for afe-loopback"
diff --git a/configs/sdm660/sdm660.mk b/configs/sdm660/sdm660.mk
index aef78b1..4863d2d 100644
--- a/configs/sdm660/sdm660.mk
+++ b/configs/sdm660/sdm660.mk
@@ -236,6 +236,18 @@
 PRODUCT_PROPERTY_OVERRIDES += \
 ro.af.client_heap_size_kbyte=7168
 
+#Set HAL buffer size to samples equal to 3 ms
+PRODUCT_PROPERTY_OVERRIDES += \
+vendor.audio_hal.in_period_size=144
+
+#Set HAL buffer size to 3 ms
+PRODUCT_PROPERTY_OVERRIDES += \
+vendor.audio_hal.period_multiplier=3
+
+#ADM Buffering size in ms
+PRODUCT_PROPERTY_OVERRIDES += \
+vendor.audio.adm.buffering.ms=2
+
 PRODUCT_PROPERTY_OVERRIDES += \
 persist.vendor.audio.hw.binder.size_kbyte=1024
 
diff --git a/hal/Android.mk b/hal/Android.mk
index cfe4a07..245679b 100644
--- a/hal/Android.mk
+++ b/hal/Android.mk
@@ -453,6 +453,7 @@
     LOCAL_SRC_FILES += audio_extn/ffv.c
 endif
 
+LOCAL_CFLAGS += -D_GNU_SOURCE
 LOCAL_CFLAGS += -Wall -Werror
 
 LOCAL_COPY_HEADERS_TO   := mm-audio
diff --git a/hal/Makefile.am b/hal/Makefile.am
index 995622d..07397aa 100644
--- a/hal/Makefile.am
+++ b/hal/Makefile.am
@@ -228,5 +228,5 @@
 audio_primary_default_la_CFLAGS += -DINT_MAX=0x7fffffff
 audio_primary_default_la_CFLAGS += -D__unused=__attribute__\(\(__unused__\)\)
 audio_primary_default_la_CFLAGS += -DLINUX_ENABLED $(TARGET_CFLAGS) -DAUDIO_EXTN_FORMATS_ENABLED
-audio_primary_default_la_CFLAGS += -DNDEBUG
+audio_primary_default_la_CFLAGS += -DNDEBUG -D_GNU_SOURCE
 audio_primary_default_la_LDFLAGS = -module -shared -avoid-version
diff --git a/hal/audio_extn/audio_extn.c b/hal/audio_extn/audio_extn.c
index 07ca504..9ecbd68 100644
--- a/hal/audio_extn/audio_extn.c
+++ b/hal/audio_extn/audio_extn.c
@@ -46,7 +46,7 @@
 #include <cutils/properties.h>
 #include <cutils/log.h>
 #include <unistd.h>
-
+#include <sched.h>
 #include "audio_hw.h"
 #include "audio_extn.h"
 #include "audio_defs.h"
@@ -605,6 +605,25 @@
 }
 #endif
 
+/* Affine AHAL thread to CPU core */
+void audio_extn_set_cpu_affinity()
+{
+    cpu_set_t cpuset;
+    struct sched_param sched_param;
+    int policy = SCHED_FIFO, rc = 0;
+
+    ALOGV("%s: Set CPU affinity for read thread", __func__);
+    CPU_ZERO(&cpuset);
+    if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0)
+        ALOGE("%s: CPU Affinity allocation failed for Capture thread",
+               __func__);
+
+    sched_param.sched_priority = sched_get_priority_min(policy);
+    rc = sched_setscheduler(0, policy, &sched_param);
+    if (rc != 0)
+         ALOGE("%s: Failed to set realtime priority", __func__);
+}
+
 #ifdef HIFI_AUDIO_ENABLED
 bool audio_extn_is_hifi_audio_enabled(void)
 {
diff --git a/hal/audio_extn/audio_extn.h b/hal/audio_extn/audio_extn.h
index 20aef60..efb7d7d 100644
--- a/hal/audio_extn/audio_extn.h
+++ b/hal/audio_extn/audio_extn.h
@@ -1143,4 +1143,5 @@
 void audio_extn_set_custom_mtmx_params(struct audio_device *adev,
                                         struct audio_usecase *usecase,
                                         bool enable);
+void audio_extn_set_cpu_affinity();
 #endif /* AUDIO_EXTN_H */
diff --git a/hal/audio_extn/ffv.c b/hal/audio_extn/ffv.c
index fac40c5..f0309c2 100644
--- a/hal/audio_extn/ffv.c
+++ b/hal/audio_extn/ffv.c
@@ -46,6 +46,7 @@
 #include <pthread.h>
 #include <sys/resource.h>
 #include <unistd.h>
+#include <system/thread_defs.h>
 
 #include "audio_hw.h"
 #include "audio_extn.h"
@@ -602,6 +603,7 @@
     int param_size = 0;
     FfvStatusType status_type;
     int ret = 0;
+    ffv_quadrx_use_dwnmix_param_t quad_downmix;
 
     ALOGV("%s: entry", __func__);
     /* notify library to reset AEC during each start */
@@ -623,6 +625,20 @@
         return -ENOMEM;
     }
 
+    if (in_snd_device == SND_DEVICE_IN_EC_REF_LOOPBACK_QUAD) {
+        quad_downmix.quadrx_dwnmix_enable = true;
+        ALOGD("%s: set param for 4 ch ec, handle %p", __func__, ffvmod.handle);
+        status_type = ffv_set_param_fn(ffvmod.handle,
+            (char *)&quad_downmix,
+            FFV_QUADRX_USE_DWNMIX_PARAM,
+            sizeof(ffv_quadrx_use_dwnmix_param_t));
+        if (status_type) {
+            ALOGE("%s: ERROR. ffv_set_param_fn for quad channel ec ref %d",
+                __func__, status_type);
+            return -EINVAL;
+        }
+    }
+
     pthread_mutex_lock(&ffvmod.init_lock);
     uc_info_tx->id = USECASE_AUDIO_EC_REF_LOOPBACK;
     uc_info_tx->type = PCM_CAPTURE;
@@ -778,6 +794,8 @@
                 return status;
             }
         }
+        audio_extn_set_cpu_affinity();
+        setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
         ffvmod.capture_started = true;
     }
 
diff --git a/hal/audio_extn/utils.c b/hal/audio_extn/utils.c
index 433743e..3b70c2b 100644
--- a/hal/audio_extn/utils.c
+++ b/hal/audio_extn/utils.c
@@ -2136,8 +2136,9 @@
     void *hw_info = NULL;
     struct mixer *mixer = NULL;
     int retry_num = 0;
-    int snd_card_num = 0, min_snd_card_num = 0;
+    int snd_card_num = 0;
     char* snd_card_name = NULL;
+    int snd_card_detection_info[MAX_SND_CARD] = {0};
 
     if (!mixer_handle) {
         ALOGE("invalid mixer handle");
@@ -2145,26 +2146,29 @@
     }
     *mixer_handle = NULL;
     /*
-    * Try with all the sound cards ( 0 to 8 ) and if none of them were detected
+    * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
     * sleep for 1 sec and try detections with sound card 0 again.
     * If sound card gets detected, check if it is relevant, if not check with the
     * other sound cards. To ensure that the irrelevant sound card is not check again,
     * we maintain it in min_snd_card_num.
     */
     while (retry_num < RETRY_NUMBER) {
-
-        while (snd_card_num <= MAX_SND_CARD) {
-           mixer = mixer_open(snd_card_num);
-           if (!mixer)
-              snd_card_num++;
-           else
-              break;
+        snd_card_num = 0;
+        while (snd_card_num < MAX_SND_CARD) {
+            if (snd_card_detection_info[snd_card_num] == 0) {
+                mixer = mixer_open(snd_card_num);
+                if (!mixer)
+                    snd_card_num++;
+                else
+                    break;
+            } else
+                snd_card_num++;
         }
 
         if (!mixer) {
            usleep(RETRY_US);
-           snd_card_num = min_snd_card_num;
            retry_num++;
+           ALOGD("%s: retry, retry_num %d", __func__, retry_num);
            continue;
         }
 
@@ -2175,18 +2179,13 @@
             return -1;
         }
         ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
-
+        snd_card_detection_info[snd_card_num] = 1;
         hw_info = hw_info_init(snd_card_name);
         if (hw_info) {
             ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
             break;
         }
-        ALOGE("%s: Failed to init hardware info", __func__);
-        min_snd_card_num++;
-        snd_card_num = min_snd_card_num;
-
-        if (snd_card_num >= MAX_SND_CARD)
-            break;
+        ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
 
         free(snd_card_name);
         snd_card_name = NULL;
@@ -2200,7 +2199,7 @@
     if (hw_info)
         hw_info_deinit(hw_info);
 
-    if ((snd_card_num >= MAX_SND_CARD) || (retry_num >= RETRY_NUMBER)) {
+    if (retry_num >= RETRY_NUMBER) {
         ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
         return -1;
     }
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index b0d10e0..8f27e86 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -1059,34 +1059,37 @@
         return -EINVAL;
     }
 
-    adev->snd_dev_ref_cnt[snd_device]++;
-
-    if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
+    if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
         ALOGE("%s: Invalid sound device returned", __func__);
         return -EINVAL;
     }
-    if (adev->snd_dev_ref_cnt[snd_device] > 1) {
+
+    adev->snd_dev_ref_cnt[snd_device]++;
+
+    if ((adev->snd_dev_ref_cnt[snd_device] > 1) &&
+            (platform_split_snd_device(adev->platform,
+                snd_device,
+                &num_devices,
+                new_snd_devices) != 0)) {
         ALOGV("%s: snd_device(%d: %s) is already active",
               __func__, snd_device, device_name);
         return 0;
     }
 
-
     if (audio_extn_spkr_prot_is_enabled())
          audio_extn_spkr_prot_calib_cancel(adev);
 
     if (platform_can_enable_spkr_prot_on_device(snd_device) &&
          audio_extn_spkr_prot_is_enabled()) {
-       if (platform_get_spkr_prot_acdb_id(snd_device) < 0) {
-           adev->snd_dev_ref_cnt[snd_device]--;
-           return -EINVAL;
-       }
-       audio_extn_dev_arbi_acquire(snd_device);
-       if (audio_extn_spkr_prot_start_processing(snd_device)) {
+        if (platform_get_spkr_prot_acdb_id(snd_device) < 0) {
+            goto err;
+        }
+        audio_extn_dev_arbi_acquire(snd_device);
+        if (audio_extn_spkr_prot_start_processing(snd_device)) {
             ALOGE("%s: spkr_start_processing failed", __func__);
             audio_extn_dev_arbi_release(snd_device);
-            return -EINVAL;
-        }
+            goto err;
+         }
     } else if (platform_split_snd_device(adev->platform,
                                          snd_device,
                                          &num_devices,
@@ -1098,11 +1101,11 @@
         ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
 
 
-       if ((SND_DEVICE_OUT_BT_A2DP == snd_device) &&
-           (audio_extn_a2dp_start_playback() < 0)) {
-           ALOGE(" fail to configure A2dp control path ");
-           return -EINVAL;
-       }
+        if ((SND_DEVICE_OUT_BT_A2DP == snd_device) &&
+            (audio_extn_a2dp_start_playback() < 0)) {
+            ALOGE(" fail to configure A2dp control path ");
+            goto err;
+        }
 
         /* due to the possibility of calibration overwrite between listen
             and audio, notify listen hal before audio calibration is sent */
@@ -1111,12 +1114,11 @@
         audio_extn_listen_update_device_status(snd_device,
                                         LISTEN_EVENT_SND_DEVICE_BUSY);
         if (platform_get_snd_device_acdb_id(snd_device) < 0) {
-            adev->snd_dev_ref_cnt[snd_device]--;
             audio_extn_sound_trigger_update_device_status(snd_device,
                                             ST_EVENT_SND_DEVICE_FREE);
             audio_extn_listen_update_device_status(snd_device,
                                         LISTEN_EVENT_SND_DEVICE_FREE);
-            return -EINVAL;
+            goto err;
         }
         audio_extn_dev_arbi_acquire(snd_device);
         audio_route_apply_and_update_path(adev->audio_route, device_name);
@@ -1138,6 +1140,9 @@
         }
     }
     return 0;
+err:
+    adev->snd_dev_ref_cnt[snd_device]--;
+    return -EINVAL;
 }
 
 int disable_snd_device(struct audio_device *adev,
@@ -1152,6 +1157,12 @@
         ALOGE("%s: Invalid sound device %d", __func__, snd_device);
         return -EINVAL;
     }
+
+    if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
+        ALOGE("%s: Invalid sound device returned", __func__);
+        return -EINVAL;
+    }
+
     if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
         ALOGE("%s: device ref cnt is already 0", __func__);
         return -EINVAL;
@@ -1159,11 +1170,6 @@
 
     adev->snd_dev_ref_cnt[snd_device]--;
 
-    if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
-        ALOGE("%s: Invalid sound device returned", __func__);
-        return -EINVAL;
-    }
-
     if (adev->snd_dev_ref_cnt[snd_device] == 0) {
         ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
 
@@ -1187,29 +1193,38 @@
 
         if (SND_DEVICE_OUT_BT_A2DP == snd_device)
             audio_extn_a2dp_stop_playback();
-
-        if (snd_device == SND_DEVICE_OUT_HDMI || snd_device == SND_DEVICE_OUT_DISPLAY_PORT)
+        else if (snd_device == SND_DEVICE_OUT_HDMI ||
+                snd_device == SND_DEVICE_OUT_DISPLAY_PORT)
             adev->is_channel_status_set = false;
         else if (SND_DEVICE_OUT_HEADPHONES == snd_device &&
-                 adev->native_playback_enabled) {
+                adev->native_playback_enabled) {
             ALOGD("%s: %d: napb: disabling native mode in hardware",
                   __func__, __LINE__);
             audio_route_reset_and_update_path(adev->audio_route,
                                               "true-native-mode");
             adev->native_playback_enabled = false;
         } else if (SND_DEVICE_OUT_HEADPHONES == snd_device &&
-                 adev->asrc_mode_enabled) {
+                adev->asrc_mode_enabled) {
             ALOGD("%s: %d: disabling asrc mode in hardware", __func__, __LINE__);
             disable_asrc_mode(adev);
             audio_route_apply_and_update_path(adev->audio_route, "hph-lowpower-mode");
-        }
-        if (((snd_device == SND_DEVICE_IN_HANDSET_6MIC) ||
-            (snd_device == SND_DEVICE_IN_HANDSET_QMIC)) &&
-            (audio_extn_ffv_get_stream() == adev->active_input)) {
+        } else if (((snd_device == SND_DEVICE_IN_HANDSET_6MIC) ||
+                    (snd_device == SND_DEVICE_IN_HANDSET_QMIC)) &&
+                (audio_extn_ffv_get_stream() == adev->active_input)) {
             ALOGD("%s: deinit ec ref loopback", __func__);
             audio_extn_ffv_deinit_ec_ref_loopback(adev, snd_device);
         }
+
         audio_extn_utils_release_snd_device(snd_device);
+    } else {
+        if (platform_split_snd_device(adev->platform,
+                                         snd_device,
+                                         &num_devices,
+                                         new_snd_devices) == 0) {
+            for (i = 0; i < num_devices; i++) {
+                adev->snd_dev_ref_cnt[new_snd_devices[i]]--;
+            }
+        }
     }
 
     return 0;
@@ -1441,14 +1456,10 @@
                 if (platform_split_snd_device(adev->platform, usecase->out_snd_device,
                                                &num_devices, split_snd_devices) == 0) {
                     adev->snd_dev_ref_cnt[usecase->out_snd_device]--;
-                    if (adev->snd_dev_ref_cnt[usecase->out_snd_device] == 0) {
-                        ALOGD("%s: disabling snd_device(%d)", __func__, usecase->out_snd_device);
-                        for (i = 0; i < num_devices; i++) {
-                            /* Disable devices that do not match with derived sound device */
-                            if (split_snd_devices[i] != derive_snd_device[usecase->id])
-                                disable_snd_device(adev, split_snd_devices[i]);
-                        }
-                        audio_extn_utils_release_snd_device(usecase->out_snd_device);
+                    for (i = 0; i < num_devices; i++) {
+                        /* Disable devices that do not match with derived sound device */
+                        if (split_snd_devices[i] != derive_snd_device[usecase->id])
+                            disable_snd_device(adev, split_snd_devices[i]);
                     }
                 } else {
                     disable_snd_device(adev, usecase->out_snd_device);
@@ -7074,6 +7085,7 @@
     struct listnode *node;
     struct audio_usecase *usecase = NULL;
     int ret = 0;
+
     pthread_mutex_lock(&adev->lock);
     if (adev->mode != mode) {
         ALOGD("%s: mode %d\n", __func__, mode);
@@ -7100,6 +7112,11 @@
             voice_stop_call(adev);
             platform_set_gsm_mode(adev->platform, false);
             adev->current_call_output = NULL;
+            // restore device for other active usecases after stop call
+            list_for_each(node, &adev->usecase_list) {
+                usecase = node_to_item(node, struct audio_usecase, list);
+                select_devices(adev, usecase->id);
+            }
         }
     }
     pthread_mutex_unlock(&adev->lock);
diff --git a/hal/msm8916/platform.c b/hal/msm8916/platform.c
index f5e77ea..27990a6 100644
--- a/hal/msm8916/platform.c
+++ b/hal/msm8916/platform.c
@@ -6029,7 +6029,7 @@
         my_data->current_backend_cfg[backend_idx].channels = channels;
 
         if (backend_idx == HDMI_RX_BACKEND)
-            platform_set_edid_channels_configuration(adev->platform, channels, HDMI_RX_BACKEND);
+            platform_set_edid_channels_configuration(adev->platform, channels, HDMI_RX_BACKEND, snd_device);
 
         ALOGD("%s:becf: afe: %s set to %s", __func__,
                my_data->current_backend_cfg[backend_idx].channels_mixer_ctl, channel_cnt_str);
@@ -6403,6 +6403,8 @@
 
         if (channels != my_data->current_backend_cfg[backend_idx].channels)
             channels_updated = true;
+
+        platform_set_edid_channels_configuration(adev->platform, channels, backend_idx, snd_device);
     }
 
     //check if mulitchannel clip needs to be down sampled to 48k
@@ -7064,7 +7066,7 @@
                 return -1;
         }
     }
-    ret = platform_set_channel_map(platform, channels, channel_map, snd_id);
+    ret = platform_set_channel_map(platform, channels, channel_map, snd_id, -1);
     return ret;
 }
 
@@ -7204,7 +7206,7 @@
     return ret;
 }
 
-int platform_set_channel_map(void *platform, int ch_count, char *ch_map, int snd_id)
+int platform_set_channel_map(void *platform, int ch_count, char *ch_map, int snd_id, int be_idx __unused)
 {
     struct mixer_ctl *ctl;
     char mixer_ctl_name[44] = {0}; // max length of name is 44 as defined
@@ -7366,7 +7368,7 @@
     return false;
 }
 
-int platform_set_edid_channels_configuration(void *platform, int channels, int backend_idx __unused) {
+int platform_set_edid_channels_configuration(void *platform, int channels, int backend_idx __unused, snd_device_t snd_device __unused) {
 
     struct platform_data *my_data = (struct platform_data *)platform;
     struct audio_device *adev = my_data->adev;
@@ -7399,9 +7401,9 @@
              */
             if (adev_device_cfg_ptr->use_client_dev_cfg) {
                 platform_set_channel_map(platform, adev_device_cfg_ptr->dev_cfg_params.channels,
-                                   (char *)adev_device_cfg_ptr->dev_cfg_params.channel_map, -1);
+                                   (char *)adev_device_cfg_ptr->dev_cfg_params.channel_map, -1, -1);
             } else {
-                platform_set_channel_map(platform, channel_count, info->channel_map, -1);
+                platform_set_channel_map(platform, channel_count, info->channel_map, -1, -1);
             }
 
             if (adev_device_cfg_ptr->use_client_dev_cfg) {
@@ -7420,7 +7422,7 @@
                 default_channelMap[0] = PCM_CHANNEL_FL;
                 default_channelMap[1] = PCM_CHANNEL_FR;
             }
-            platform_set_channel_map(platform,2,default_channelMap,-1);
+            platform_set_channel_map(platform, 2, default_channelMap, -1, -1);
             platform_set_channel_allocation(platform,0);
         }
     }
diff --git a/hal/msm8974/platform.c b/hal/msm8974/platform.c
index ebf2369..948703f 100644
--- a/hal/msm8974/platform.c
+++ b/hal/msm8974/platform.c
@@ -4679,9 +4679,11 @@
         } else if (out_device &
                     (AUDIO_DEVICE_OUT_USB_DEVICE |
                      AUDIO_DEVICE_OUT_USB_HEADSET)) {
-          if (audio_extn_usb_is_capture_supported()) {
-              snd_device = SND_DEVICE_IN_VOICE_USB_HEADSET_MIC;
-          }
+            if (audio_extn_usb_is_capture_supported()) {
+                snd_device = SND_DEVICE_IN_VOICE_USB_HEADSET_MIC;
+            } else {
+                snd_device = SND_DEVICE_IN_HANDSET_MIC;
+            }
         }
     } else if (my_data->use_generic_handset == true &&  //     system prop is enabled
                (my_data->source_mic_type & SOURCE_QUAD_MIC) &&  // AND 4mic is available
@@ -6197,7 +6199,7 @@
 
         if ((backend_idx == HDMI_RX_BACKEND) ||
                 (backend_idx == DISP_PORT_RX_BACKEND))
-            platform_set_edid_channels_configuration(adev->platform, channels, backend_idx);
+            platform_set_edid_channels_configuration(adev->platform, channels, backend_idx, snd_device);
 
         ALOGD("%s:becf: afe: %s set to %s ", __func__,
                my_data->current_backend_cfg[backend_idx].channels_mixer_ctl,
@@ -6596,6 +6598,8 @@
 
         if (channels != my_data->current_backend_cfg[backend_idx].channels)
             channels_updated = true;
+
+        platform_set_edid_channels_configuration(adev->platform, channels, backend_idx, snd_device);
     }
 
     ALOGI("%s:becf: afe: Codec selected backend: %d updated bit width: %d and sample rate: %d",
@@ -7241,7 +7245,7 @@
                 return -1;
         }
     }
-    ret = platform_set_channel_map(platform, channels, channel_map, snd_id);
+    ret = platform_set_channel_map(platform, channels, channel_map, snd_id, -1);
     return ret;
 }
 
@@ -7355,13 +7359,15 @@
     return ret;
 }
 
-int platform_set_channel_map(void *platform, int ch_count, char *ch_map, int snd_id)
+int platform_set_channel_map(void *platform, int ch_count, char *ch_map, int snd_id, int be_idx)
 {
-    struct mixer_ctl *ctl;
+    struct mixer_ctl *ctl, *be_ctl = NULL;
     char mixer_ctl_name[44] = {0}; // max length of name is 44 as defined
+    char be_mixer_ctl_name[] = "Backend Device Channel Map";
     int ret;
     unsigned int i;
     long set_values[FCC_8] = {0};
+    long be_set_values[FCC_8 + 1] = {0};
     struct platform_data *my_data = (struct platform_data *)platform;
     struct audio_device *adev = my_data->adev;
     ALOGV("%s channel_count:%d",__func__, ch_count);
@@ -7377,7 +7383,19 @@
     if (snd_id >= 0) {
         snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Playback Channel Map%d", snd_id);
     } else {
-        strlcpy(mixer_ctl_name, "Playback Device Channel Map", sizeof(mixer_ctl_name));
+        if (be_idx >=0) {
+            be_ctl = mixer_get_ctl_by_name(adev->mixer, be_mixer_ctl_name);
+            if (!be_ctl) {
+                ALOGD("%s: Could not get ctl for mixer cmd - %s, using default control",
+                       __func__, be_mixer_ctl_name);
+                strlcpy(mixer_ctl_name, "Playback Device Channel Map", sizeof(mixer_ctl_name));
+                be_idx = -1;
+            } else {
+                strlcpy(mixer_ctl_name, "Backend Device Channel Map", sizeof(mixer_ctl_name));
+            }
+        } else {
+            strlcpy(mixer_ctl_name, "Playback Device Channel Map", sizeof(mixer_ctl_name));
+        }
     }
 
     ALOGD("%s mixer_ctl_name:%s", __func__, mixer_ctl_name);
@@ -7396,7 +7414,13 @@
         set_values[0], set_values[1], set_values[2], set_values[3], set_values[4],
         set_values[5], set_values[6], set_values[7], ch_count);
 
-    ret = mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
+    if (be_idx >= 0) {
+        be_set_values[0] = be_idx;
+        memcpy(&be_set_values[1], set_values, sizeof(long) * ch_count);
+        ret = mixer_ctl_set_array(ctl, be_set_values, ARRAY_SIZE(be_set_values));
+    } else {
+        ret = mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
+    }
     if (ret < 0) {
         ALOGE("%s: Could not set ctl, error:%d ch_count:%d",
               __func__, ret, ch_count);
@@ -7561,7 +7585,7 @@
     return 0;
 }
 
-int platform_set_edid_channels_configuration(void *platform, int channels, int backend_idx) {
+int platform_set_edid_channels_configuration(void *platform, int channels, int backend_idx, snd_device_t snd_device) {
 
     struct platform_data *my_data = (struct platform_data *)platform;
     struct audio_device *adev = my_data->adev;
@@ -7570,6 +7594,7 @@
     int i, ret;
     char default_channelMap[MAX_CHANNELS_SUPPORTED] = {0};
     struct audio_device_config_param *adev_device_cfg_ptr = adev->device_cfg_params;
+    int be_idx = -1;
 
     if ((backend_idx != HDMI_RX_BACKEND) &&
             (backend_idx != DISP_PORT_RX_BACKEND)) {
@@ -7577,6 +7602,7 @@
         return -EINVAL;
     }
 
+    be_idx = platform_get_snd_device_backend_index(snd_device);
     ret = platform_get_edid_info(platform);
     info = (edid_audio_info *)my_data->edid_info;
     adev_device_cfg_ptr += backend_idx;
@@ -7599,9 +7625,9 @@
              */
             if (adev_device_cfg_ptr->use_client_dev_cfg) {
                 platform_set_channel_map(platform, adev_device_cfg_ptr->dev_cfg_params.channels,
-                                   (char *)adev_device_cfg_ptr->dev_cfg_params.channel_map, -1);
+                                   (char *)adev_device_cfg_ptr->dev_cfg_params.channel_map, -1, be_idx);
             } else {
-                platform_set_channel_map(platform, channel_count, info->channel_map, -1);
+                platform_set_channel_map(platform, channel_count, info->channel_map, -1, be_idx);
             }
 
             if (adev_device_cfg_ptr->use_client_dev_cfg) {
@@ -7620,7 +7646,7 @@
                 default_channelMap[0] = PCM_CHANNEL_FL;
                 default_channelMap[1] = PCM_CHANNEL_FR;
             }
-            platform_set_channel_map(platform,2,default_channelMap,-1);
+            platform_set_channel_map(platform, 2, default_channelMap, -1, be_idx);
             platform_set_channel_allocation(platform,0);
         }
     }
diff --git a/hal/platform_api.h b/hal/platform_api.h
index 80138c8..17afefc 100644
--- a/hal/platform_api.h
+++ b/hal/platform_api.h
@@ -225,7 +225,7 @@
 int platform_get_edid_info(void *platform);
 int platform_get_supported_copp_sampling_rate(uint32_t stream_sr);
 int platform_set_channel_map(void *platform, int ch_count, char *ch_map,
-                             int snd_id);
+                             int snd_id, int be_idx);
 int platform_set_stream_channel_map(void *platform, audio_channel_mask_t channel_mask,
                                    int snd_id, uint8_t *input_channel_map);
 int platform_set_stream_pan_scale_params(void *platform,
@@ -235,7 +235,8 @@
                                        int snd_id,
                                        snd_device_t snd_device,
                                        struct mix_matrix_params mm_params);
-int platform_set_edid_channels_configuration(void *platform, int channels, int backend_idx);
+int platform_set_edid_channels_configuration(void *platform, int channels,
+                                             int backend_idx, snd_device_t snd_device);
 unsigned char platform_map_to_edid_format(int format);
 bool platform_is_edid_supported_format(void *platform, int format);
 bool platform_is_edid_supported_sample_rate(void *platform, int sample_rate);
diff --git a/post_proc/volume_listener.c b/post_proc/volume_listener.c
index d4c3753..9fc8377 100644
--- a/post_proc/volume_listener.c
+++ b/post_proc/volume_listener.c
@@ -779,9 +779,6 @@
     int status = -EINVAL;
     bool recompute_flag = false;
     int active_stream_count = 0;
-    uint32_t session_id;
-    uint32_t stream_type;
-    effect_uuid_t uuid;
 
     ALOGV("%s context %p", __func__, handle);
 
@@ -789,18 +786,12 @@
         return status;
     }
     pthread_mutex_lock(&vol_listner_init_lock);
-    session_id = recv_contex->session_id;
-    stream_type = recv_contex->stream_type;
-    uuid = recv_contex->desc->uuid;
 
     // check if the handle/context provided is valid
     list_for_each(node, &vol_effect_list) {
         context = node_to_item(node, struct vol_listener_context_s, effect_list_node);
-        if ((memcmp(&(context->desc->uuid), &uuid, sizeof(effect_uuid_t)) == 0)
-            && (context->session_id == session_id)
-            && (context->stream_type == stream_type)) {
+        if (context == recv_contex) {
             ALOGV("--- Found something to remove ---");
-            list_remove(node);
             PRINT_STREAM_TYPE(context->stream_type);
             if (verify_context(context)) {
                 recompute_flag = true;