Merge "DO NOT MERGE: Merge Oreo MR1 into master" am: 4ff40ccbfd  -s ours am: 690dbecf8a
am: f04cb090da  -s ours

Change-Id: I739aa6ff31ad0412b839b6ed5521a815ad19cf06
diff --git a/Android.mk b/Android.mk
index 65bf578..d6f03fb 100644
--- a/Android.mk
+++ b/Android.mk
@@ -1,6 +1,6 @@
 # TODO:  Find a better way to separate build configs for ADP vs non-ADP devices
 ifneq ($(TARGET_BOARD_AUTO),true)
-  ifneq ($(filter msm8960 msm8226 msm8x26 msm8x84 msm8084 msm8992 msm8994 msm8996 msm8909 msm8952 msm8998,$(TARGET_BOARD_PLATFORM)),)
+  ifneq ($(filter msm8960 msm8226 msm8x26 msm8x84 msm8084 msm8992 msm8994 msm8996 msm8909 msm8952 msm8998 sdm845,$(TARGET_BOARD_PLATFORM)),)
 
     MY_LOCAL_PATH := $(call my-dir)
 
diff --git a/hal/Android.mk b/hal/Android.mk
index 923879c..5e5813f 100644
--- a/hal/Android.mk
+++ b/hal/Android.mk
@@ -4,13 +4,27 @@
 
 include $(CLEAR_VARS)
 
+LOCAL_MODULE := libqcaudioperf
+
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)
+LOCAL_SHARED_LIBRARIES += libbase libhidlbase libhwbinder libutils android.hardware.power@1.2 liblog
+LOCAL_SRC_FILES := audio_perf.cpp
+LOCAL_MODULE_RELATIVE_PATH := hw
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_OWNER := qcom
+LOCAL_PROPRIETARY_MODULE := true
+
+include $(BUILD_SHARED_LIBRARY)
+
+include $(CLEAR_VARS)
+
 LOCAL_ARM_MODE := arm
 
 AUDIO_PLATFORM := $(TARGET_BOARD_PLATFORM)
 ifneq ($(filter msm8960,$(TARGET_BOARD_PLATFORM)),)
   LOCAL_CFLAGS += -DMAX_TARGET_SPECIFIC_CHANNEL_CNT="2"
 endif
-ifneq ($(filter msm8974 msm8226 msm8084 msm8992 msm8994 msm8996 msm8998,$(TARGET_BOARD_PLATFORM)),)
+ifneq ($(filter msm8974 msm8226 msm8084 msm8992 msm8994 msm8996 msm8998 sdm845,$(TARGET_BOARD_PLATFORM)),)
   # B-family platform uses msm8974 code base
   AUDIO_PLATFORM = msm8974
 ifneq ($(filter msm8974,$(TARGET_BOARD_PLATFORM)),)
@@ -44,6 +58,11 @@
 ifneq ($(filter msm8998,$(TARGET_BOARD_PLATFORM)),)
   LOCAL_CFLAGS := -DPLATFORM_MSM8998
   LOCAL_CFLAGS += -DMAX_TARGET_SPECIFIC_CHANNEL_CNT="4"
+  MULTIPLE_HW_VARIANTS_ENABLED := true
+endif
+ifneq ($(filter sdm845,$(TARGET_BOARD_PLATFORM)),)
+  LOCAL_CFLAGS := -DPLATFORM_SDM845
+  LOCAL_CFLAGS += -DMAX_TARGET_SPECIFIC_CHANNEL_CNT="4"
   LOCAL_CFLAGS += -DKPI_OPTIMIZE_ENABLED
   MULTIPLE_HW_VARIANTS_ENABLED := true
 endif
@@ -144,12 +163,17 @@
     LOCAL_SRC_FILES += audio_extn/spkr_protection.c
 endif
 
+ifeq ($(strip $(AUDIO_FEATURE_ENABLED_CIRRUS_SPKR_PROTECTION)),true)
+    LOCAL_CFLAGS += -DSPKR_PROT_ENABLED
+    LOCAL_SRC_FILES += audio_extn/cirrus_playback.c
+endif
+
 ifeq ($(strip $(AUDIO_FEATURE_ENABLED_DSM_FEEDBACK)),true)
     LOCAL_CFLAGS += -DDSM_FEEDBACK_ENABLED
     LOCAL_SRC_FILES += audio_extn/dsm_feedback.c
 endif
 
-ifneq ($(filter msm8992 msm8994 msm8996 msm8998,$(TARGET_BOARD_PLATFORM)),)
+ifneq ($(filter msm8992 msm8994 msm8996 msm8998 sdm845,$(TARGET_BOARD_PLATFORM)),)
   # push codec/mad calibration to HW dep node
   # applicable to msm8992/8994 or newer platforms
   LOCAL_CFLAGS += -DHWDEP_CAL_ENABLED
@@ -161,6 +185,8 @@
     LOCAL_SRC_FILES += audio_extn/sndmonitor.c
 endif
 
+LOCAL_SHARED_LIBRARIES += libqcaudioperf
+
 LOCAL_HEADER_LIBRARIES += libhardware_headers
 
 LOCAL_MODULE := audio.primary.$(TARGET_BOARD_PLATFORM)
diff --git a/hal/audio_extn/audio_extn.h b/hal/audio_extn/audio_extn.h
index c7d6768..2ca76fa 100644
--- a/hal/audio_extn/audio_extn.h
+++ b/hal/audio_extn/audio_extn.h
@@ -81,7 +81,7 @@
 #define audio_extn_usb_is_capture_supported()                          (false)
 #define audio_extn_usb_get_max_channels(dir)                           (0)
 #define audio_extn_usb_get_max_bit_width(dir)                          (0)
-#define audio_extn_usb_sup_sample_rates(t, s, l)                       (0)
+#define audio_extn_usb_sup_sample_rates(t, s, l)        ((t), (s), (l), 0) /* fix unused warn */
 #define audio_extn_usb_alive(adev)                                     (false)
 #else
 void audio_extn_usb_init(void *adev);
diff --git a/hal/audio_extn/cirrus_playback.c b/hal/audio_extn/cirrus_playback.c
new file mode 100644
index 0000000..1f7a9f7
--- /dev/null
+++ b/hal/audio_extn/cirrus_playback.c
@@ -0,0 +1,541 @@
+/*
+ * Copyright (C) 2017 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_cirrus_playback"
+/*#define LOG_NDEBUG 0*/
+
+#include <errno.h>
+#include <math.h>
+#include <cutils/log.h>
+#include <fcntl.h>
+#include "../audio_hw.h"
+#include "platform.h"
+#include "platform_api.h"
+#include <sys/stat.h>
+#include <linux/types.h>
+#include <linux/ioctl.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <dlfcn.h>
+#include <math.h>
+#include <time.h>
+#include <unistd.h>
+#include <cutils/properties.h>
+#include "audio_extn.h"
+
+struct cirrus_playback_session {
+    void *adev_handle;
+    pthread_mutex_t mutex_fb_prot;
+    pthread_mutex_t mutex_pcm_stream;
+    pthread_t pcm_stream_thread;
+    struct pcm *pcm_rx;
+    struct pcm *pcm_tx;
+    bool spkr_prot_enable;
+};
+
+struct crus_sp_ioctl_header {
+    uint32_t size;
+    uint32_t module_id;
+    uint32_t param_id;
+    uint32_t data_length;
+    void *data;
+};
+
+/* Payload struct for getting calibration result from DSP module */
+struct cirrus_cal_result_t {
+    int32_t status_l;
+    int32_t checksum_l;
+    int32_t z_l;
+    int32_t status_r;
+    int32_t checksum_r;
+    int32_t z_r;
+    int32_t atemp;
+};
+
+/* Payload struct for setting the RX and TX use cases */
+struct crus_rx_run_case_ctrl_t {
+    int32_t value;
+    int32_t status_l;
+    int32_t checksum_l;
+    int32_t z_l;
+    int32_t status_r;
+    int32_t checksum_r;
+    int32_t z_r;
+    int32_t atemp;
+};
+
+#define CRUS_SP_FILE "/dev/msm_cirrus_playback"
+#define CRUS_CAL_FILE "/persist/audio/audio.cal"
+
+#define CRUS_SP_USECASE_MIXER "Cirrus SP Usecase Config"
+#define CRUS_SP_EXT_CONFIG_MIXER "Cirrus SP EXT Config"
+
+#define CIRRUS_SP 0x10027053
+
+#define CRUS_MODULE_ID_TX 0x00000002
+#define CRUS_MODULE_ID_RX 0x00000001
+
+#define CRUS_PARAM_RX_SET_USECASE 0x00A1AF02
+#define CRUS_PARAM_TX_SET_USECASE 0x00A1BF0A
+
+#define CRUS_PARAM_RX_SET_CALIB 0x00A1AF03
+#define CRUS_PARAM_TX_SET_CALIB 0x00A1BF03
+
+#define CRUS_PARAM_RX_SET_EXT_CONFIG 0x00A1AF05
+#define CRUS_PARAM_TX_SET_EXT_CONFIG 0x00A1BF08
+
+#define CRUS_PARAM_RX_GET_TEMP 0x00A1AF07
+#define CRUS_PARAM_TX_GET_TEMP_CAL 0x00A1BF06
+// variables based on CSPL tuning file, max parameter length is 96 integers (384 bytes)
+#define CRUS_PARAM_TEMP_MAX_LENGTH 384
+
+#define CRUS_AFE_PARAM_ID_ENABLE 0x00010203
+
+#define CRUS_SP_IOCTL_MAGIC 'a'
+
+#define CRUS_SP_IOCTL_GET _IOWR(CRUS_SP_IOCTL_MAGIC, 219, void *)
+#define CRUS_SP_IOCTL_SET _IOWR(CRUS_SP_IOCTL_MAGIC, 220, void *)
+#define CRUS_SP_IOCTL_GET_CALIB _IOWR(CRUS_SP_IOCTL_MAGIC, 221, void *)
+#define CRUS_SP_IOCTL_SET_CALIB _IOWR(CRUS_SP_IOCTL_MAGIC, 222, void *)
+
+#define CRUS_SP_DEFAULT_AMBIENT_TEMP 23
+
+static struct pcm_config pcm_config_cirrus_tx = {
+    .channels = 2,
+    .rate = 48000,
+    .period_size = 320,
+    .period_count = 4,
+    .format = PCM_FORMAT_S16_LE,
+    .start_threshold = 0,
+    .stop_threshold = INT_MAX,
+    .avail_min = 0,
+};
+
+static struct pcm_config pcm_config_cirrus_rx = {
+    .channels = 2,
+    .rate = 48000,
+    .period_size = 320,
+    .period_count = 4,
+    .format = PCM_FORMAT_S16_LE,
+    .start_threshold = 0,
+    .stop_threshold = INT_MAX,
+    .avail_min = 0,
+};
+
+static struct cirrus_playback_session handle;
+
+static void *audio_extn_cirrus_run_calibration() {
+    struct audio_device *adev = handle.adev_handle;
+    struct crus_sp_ioctl_header header;
+    struct cirrus_cal_result_t result;
+    struct mixer_ctl *ctl;
+    FILE *cal_file;
+    int ret = 0, dev_file;
+    char *buffer = NULL;
+    uint32_t option = 1;
+    struct timespec timeout;
+
+    ALOGI("%s: Calibration thread", __func__);
+
+    timeout.tv_sec = 10;
+    timeout.tv_nsec = 0;
+
+    dev_file = open(CRUS_SP_FILE, O_RDWR | O_NONBLOCK);
+    if (dev_file < 0) {
+        ALOGE("%s: Failed to open Cirrus Playback IOCTL (%d)",
+              __func__, dev_file);
+        ret = -EINVAL;
+        goto exit;
+    }
+
+    buffer = calloc(1, CRUS_PARAM_TEMP_MAX_LENGTH);
+    if (!buffer) {
+        ret = -EINVAL;
+        goto exit;
+    }
+
+    cal_file = fopen(CRUS_CAL_FILE, "r");
+    if (cal_file) {
+        size_t bytes;
+        bytes = fread(&result, 1, sizeof(result), cal_file);
+        if (bytes < sizeof(result)) {
+            ALOGE("%s: Cirrus SP calibration file cannot be read (%d)",
+                  __func__, ret);
+            ret = -EINVAL;
+            fclose(cal_file);
+            goto exit;
+        }
+
+        fclose(cal_file);
+    } else {
+
+        ALOGV("%s: Calibrating...", __func__);
+
+        header.size = sizeof(header);
+        header.module_id = CRUS_MODULE_ID_RX;
+        header.param_id = CRUS_PARAM_RX_SET_CALIB;
+        header.data_length = sizeof(option);
+        header.data = &option;
+
+        ret = ioctl(dev_file, CRUS_SP_IOCTL_SET, &header);
+        if (ret < 0) {
+            ALOGE("%s: Cirrus SP calibration IOCTL failure (%d)",
+                  __func__, ret);
+            ret = -EINVAL;
+            goto exit;
+        }
+
+        header.size = sizeof(header);
+        header.module_id = CRUS_MODULE_ID_TX;
+        header.param_id = CRUS_PARAM_TX_SET_CALIB;
+        header.data_length = sizeof(option);
+        header.data = &option;
+
+        ret = ioctl(dev_file, CRUS_SP_IOCTL_SET, &header);
+        if (ret < 0) {
+            ALOGE("%s: Cirrus SP calibration IOCTL failure (%d)",
+                  __func__, ret);
+            ret = -EINVAL;
+            goto exit;
+        }
+
+        sleep(2);
+
+        header.size = sizeof(header);
+        header.module_id = CRUS_MODULE_ID_TX;
+        header.param_id = CRUS_PARAM_TX_GET_TEMP_CAL;
+        header.data_length = sizeof(result);
+        header.data = &result;
+
+        ret = ioctl(dev_file, CRUS_SP_IOCTL_GET, &header);
+        if (ret < 0) {
+            ALOGE("%s: Cirrus SP calibration IOCTL failure (%d)",
+                  __func__, ret);
+            ret = -EINVAL;
+            goto exit;
+        }
+
+        result.atemp = CRUS_SP_DEFAULT_AMBIENT_TEMP;
+
+        // TODO: Save calibrated data to file
+    }
+
+    header.size = sizeof(header);
+    header.module_id = CRUS_MODULE_ID_TX;
+    header.param_id = 0;
+    header.data_length = sizeof(result);
+    header.data = &result;
+
+    ret = ioctl(dev_file, CRUS_SP_IOCTL_SET_CALIB, &header);
+
+    if (ret < 0) {
+        ALOGE("%s: Cirrus SP calibration IOCTL failure (%d)", __func__, ret);
+        close(dev_file);
+        ret = -EINVAL;
+        goto exit;
+    }
+
+    ctl = mixer_get_ctl_by_name(adev->mixer,
+                                CRUS_SP_USECASE_MIXER);
+    if (!ctl) {
+        ALOGE("%s: Could not get ctl for mixer cmd - %s",
+              __func__, CRUS_SP_USECASE_MIXER);
+        ret = -EINVAL;
+        goto exit;
+    }
+
+    mixer_ctl_set_value(ctl, 0, 0); // Set RX external firmware config
+
+    sleep(1);
+
+    header.size = sizeof(header);
+    header.module_id = CRUS_MODULE_ID_RX;
+    header.param_id = CRUS_PARAM_RX_GET_TEMP;
+    header.data_length = sizeof(buffer);
+    header.data = buffer;
+
+    ret = ioctl(dev_file, CRUS_SP_IOCTL_GET, &header);
+    if (ret < 0) {
+        ALOGE("%s: Cirrus SP temperature IOCTL failure (%d)", __func__, ret);
+        ret = -EINVAL;
+        goto exit;
+    }
+
+    ALOGI("%s: Cirrus SP successfully calibrated", __func__);
+
+exit:
+    close(dev_file);
+    free(buffer);
+    ALOGV("%s: Exit", __func__);
+
+    return NULL;
+}
+
+
+static void *audio_extn_cirrus_pcm_stream_thread() {
+    struct audio_device *adev = handle.adev_handle;
+    struct audio_usecase *uc_info_rx = NULL;
+    int ret = 0;
+    int32_t pcm_dev_rx_id = 0;
+    uint32_t rx_use_case = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
+    uint32_t retries = 5;
+
+    pthread_mutex_lock(&handle.mutex_pcm_stream);
+
+    ALOGI("%s: PCM Stream thread", __func__);
+
+    while (!adev->platform && retries) {
+        sleep(1);
+        ALOGI("%s: Waiting...", __func__);
+        retries--;
+    }
+
+    uc_info_rx = (struct audio_usecase *)calloc(1, sizeof(*uc_info_rx));
+    if (!uc_info_rx) {
+        ret = -EINVAL;
+        goto exit;
+    }
+
+    uc_info_rx->id = rx_use_case;
+    uc_info_rx->type = PCM_PLAYBACK;
+    uc_info_rx->in_snd_device = SND_DEVICE_NONE;
+    uc_info_rx->stream.out = adev->primary_output;
+    uc_info_rx->out_snd_device = SND_DEVICE_OUT_SPEAKER;
+    list_add_tail(&adev->usecase_list, &uc_info_rx->list);
+
+    enable_snd_device(adev, SND_DEVICE_OUT_SPEAKER);
+    enable_audio_route(adev, uc_info_rx);
+
+    handle.pcm_rx = pcm_open(adev->snd_card, pcm_dev_rx_id,
+                             PCM_OUT, &pcm_config_cirrus_rx);
+
+    if (!handle.pcm_rx || !pcm_is_ready(handle.pcm_rx)) {
+        ALOGE("%s: PCM device not ready: %s", __func__,
+              pcm_get_error(handle.pcm_rx ? handle.pcm_rx : 0));
+        ret = -EINVAL;
+        goto close_stream;
+    }
+
+    if (pcm_start(handle.pcm_rx) < 0) {
+        ALOGE("%s: pcm start for RX failed; error = %s", __func__,
+              pcm_get_error(handle.pcm_rx));
+        ret = -EINVAL;
+        goto close_stream;
+    }
+
+    ALOGV("%s: PCM thread streaming", __func__);
+
+    audio_extn_cirrus_run_calibration();
+
+close_stream:
+    if (handle.pcm_rx) {
+        ALOGV("%s: pcm_rx_close", __func__);
+        pcm_close(handle.pcm_rx);
+        handle.pcm_rx = NULL;
+    }
+
+    disable_audio_route(adev, uc_info_rx);
+    disable_snd_device(adev, SND_DEVICE_OUT_SPEAKER);
+    list_remove(&uc_info_rx->list);
+    free(uc_info_rx);
+
+exit:
+    pthread_mutex_unlock(&handle.mutex_pcm_stream);
+    ALOGV("%s: Exit", __func__);
+
+    pthread_exit(0);
+    return NULL;
+}
+
+void audio_extn_spkr_prot_init(void *adev) {
+    ALOGI("%s: Initialize Cirrus Logic Playback module", __func__);
+
+    struct snd_card_split *snd_split_handle = NULL;
+
+    if (!adev) {
+        ALOGE("%s: Invalid params", __func__);
+        return;
+    }
+
+    memset(&handle, 0, sizeof(handle));
+
+    snd_split_handle = audio_extn_get_snd_card_split();
+
+    /* FIXME: REMOVE THIS AFTER B1C1 P1.0 SUPPORT */
+    if (!strcmp(snd_split_handle->form_factor, "tdm")) {
+        handle.spkr_prot_enable = true;
+    } else {
+        handle.spkr_prot_enable = false;
+    }
+
+    handle.adev_handle = adev;
+
+    pthread_mutex_init(&handle.mutex_fb_prot, NULL);
+    pthread_mutex_init(&handle.mutex_pcm_stream, NULL);
+
+    (void)pthread_create(&handle.pcm_stream_thread,
+                         (const pthread_attr_t *) NULL,
+                         audio_extn_cirrus_pcm_stream_thread, &handle);
+}
+
+int audio_extn_spkr_prot_start_processing(snd_device_t snd_device) {
+    struct audio_usecase *uc_info_tx;
+    struct audio_device *adev = handle.adev_handle;
+    int32_t pcm_dev_tx_id = -1, ret = 0;
+
+    ALOGV("%s: Entry", __func__);
+
+    if (!adev) {
+        ALOGE("%s: Invalid params", __func__);
+        return -EINVAL;
+    }
+
+    uc_info_tx = (struct audio_usecase *)calloc(1, sizeof(*uc_info_tx));
+    if (!uc_info_tx) {
+        return -ENOMEM;
+    }
+
+    audio_route_apply_and_update_path(adev->audio_route,
+                                      platform_get_snd_device_name(snd_device));
+
+    pthread_mutex_lock(&handle.mutex_fb_prot);
+    uc_info_tx->id = USECASE_AUDIO_SPKR_CALIB_TX;
+    uc_info_tx->type = PCM_CAPTURE;
+    uc_info_tx->in_snd_device = SND_DEVICE_IN_CAPTURE_VI_FEEDBACK;
+    uc_info_tx->out_snd_device = SND_DEVICE_NONE;
+    handle.pcm_tx = NULL;
+
+    list_add_tail(&adev->usecase_list, &uc_info_tx->list);
+
+    enable_snd_device(adev, SND_DEVICE_IN_CAPTURE_VI_FEEDBACK);
+    enable_audio_route(adev, uc_info_tx);
+
+    pcm_dev_tx_id = platform_get_pcm_device_id(uc_info_tx->id, PCM_CAPTURE);
+
+    if (pcm_dev_tx_id < 0) {
+        ALOGE("%s: Invalid pcm device for usecase (%d)",
+              __func__, uc_info_tx->id);
+        ret = -ENODEV;
+        goto exit;
+    }
+
+    handle.pcm_tx = pcm_open(adev->snd_card,
+                             pcm_dev_tx_id,
+                             PCM_IN, &pcm_config_cirrus_tx);
+
+    if (!handle.pcm_tx || !pcm_is_ready(handle.pcm_tx)) {
+        ALOGD("%s: PCM device not ready: %s", __func__,
+              pcm_get_error(handle.pcm_tx ? handle.pcm_tx : 0));
+        ret = -EIO;
+        goto exit;
+    }
+
+    if (pcm_start(handle.pcm_tx) < 0) {
+        ALOGI("%s: retrying pcm_start...", __func__);
+	usleep(500 * 1000);
+        if (pcm_start(handle.pcm_tx) < 0) {
+            ALOGI("%s: pcm start for TX failed; error = %s", __func__,
+                  pcm_get_error(handle.pcm_tx));
+            ret = -EINVAL;
+        }
+    }
+
+exit:
+    if (ret) {
+        ALOGI("%s: Disable and bail out", __func__);
+        if (handle.pcm_tx) {
+            ALOGI("%s: pcm_tx_close", __func__);
+            pcm_close(handle.pcm_tx);
+            handle.pcm_tx = NULL;
+        }
+
+        disable_audio_route(adev, uc_info_tx);
+        disable_snd_device(adev, SND_DEVICE_IN_CAPTURE_VI_FEEDBACK);
+        list_remove(&uc_info_tx->list);
+        free(uc_info_tx);
+    }
+
+    pthread_mutex_unlock(&handle.mutex_fb_prot);
+    ALOGV("%s: Exit", __func__);
+    return ret;
+}
+
+void audio_extn_spkr_prot_stop_processing(snd_device_t snd_device) {
+    struct audio_usecase *uc_info_tx;
+    struct audio_device *adev = handle.adev_handle;
+
+    ALOGV("%s: Entry", __func__);
+
+    pthread_mutex_lock(&handle.mutex_fb_prot);
+
+    uc_info_tx = get_usecase_from_list(adev, USECASE_AUDIO_SPKR_CALIB_TX);
+
+    if (uc_info_tx) {
+        if (handle.pcm_tx) {
+            ALOGI("%s: pcm_tx_close", __func__);
+            pcm_close(handle.pcm_tx);
+            handle.pcm_tx = NULL;
+        }
+
+        disable_audio_route(adev, uc_info_tx);
+        disable_snd_device(adev, SND_DEVICE_IN_CAPTURE_VI_FEEDBACK);
+        list_remove(&uc_info_tx->list);
+        free(uc_info_tx);
+
+        audio_route_reset_path(adev->audio_route,
+                               platform_get_snd_device_name(snd_device));
+    }
+
+    pthread_mutex_unlock(&handle.mutex_fb_prot);
+
+    ALOGV("%s: Exit", __func__);
+}
+
+bool audio_extn_spkr_prot_is_enabled() {
+    return handle.spkr_prot_enable;
+}
+
+int audio_extn_spkr_prot_get_acdb_id(snd_device_t snd_device) {
+    int acdb_id;
+
+    switch (snd_device) {
+    case SND_DEVICE_OUT_SPEAKER:
+        acdb_id = platform_get_snd_device_acdb_id(SND_DEVICE_OUT_SPEAKER_PROTECTED);
+        break;
+    case SND_DEVICE_OUT_VOICE_SPEAKER:
+        acdb_id = platform_get_snd_device_acdb_id(SND_DEVICE_OUT_VOICE_SPEAKER_PROTECTED);
+        break;
+    default:
+        acdb_id = -EINVAL;
+        break;
+    }
+    return acdb_id;
+}
+
+int audio_extn_get_spkr_prot_snd_device(snd_device_t snd_device) {
+    switch(snd_device) {
+    case SND_DEVICE_OUT_SPEAKER:
+        return SND_DEVICE_OUT_SPEAKER_PROTECTED;
+    case SND_DEVICE_OUT_VOICE_SPEAKER:
+        return SND_DEVICE_OUT_VOICE_SPEAKER_PROTECTED;
+    default:
+        return snd_device;
+    }
+}
+
+void audio_extn_spkr_prot_calib_cancel(__unused void *adev) {
+    // FIXME: wait or cancel audio_extn_cirrus_run_calibration
+}
diff --git a/hal/audio_extn/hfp.c b/hal/audio_extn/hfp.c
index 07a9711..ad1530a 100644
--- a/hal/audio_extn/hfp.c
+++ b/hal/audio_extn/hfp.c
@@ -438,10 +438,17 @@
     ret = str_parms_get_str(parms, AUDIO_PARAMETER_HFP_ENABLE, value,
                             sizeof(value));
     if (ret >= 0) {
-           if (!strncmp(value,"true",sizeof(value)))
-               ret = start_hfp(adev,parms);
-           else
-               stop_hfp(adev);
+           if (!strncmp(value,"true",sizeof(value))) {
+               if (!hfpmod.is_hfp_running)
+                   start_hfp(adev,parms);
+               else
+                   ALOGW("%s: HFP is already active.", __func__);
+           } else {
+               if (hfpmod.is_hfp_running)
+                   stop_hfp(adev);
+               else
+                   ALOGW("%s: ignore STOP, HFC not active", __func__);
+           }
     }
     memset(value, 0, sizeof(value));
     ret = str_parms_get_str(parms,AUDIO_PARAMETER_HFP_SET_SAMPLING_RATE, value,
diff --git a/hal/audio_extn/spkr_protection.c b/hal/audio_extn/spkr_protection.c
index 8afb0dc..22920b7 100644
--- a/hal/audio_extn/spkr_protection.c
+++ b/hal/audio_extn/spkr_protection.c
@@ -51,7 +51,7 @@
 #define MAX_RESISTANCE_SPKR_Q24 (40 * (1 << 24))
 
 /*Path where the calibration file will be stored*/
-#define CALIB_FILE "/data/misc/audio/audio.cal"
+#define CALIB_FILE "/data/vendor/audio/audio.cal"
 
 /*Time between retries for calibartion or intial wait time
   after boot up*/
@@ -304,6 +304,7 @@
     int32_t pcm_dev_rx_id = -1, pcm_dev_tx_id = -1;
     struct timespec ts;
     int retry_duration;
+    int app_type = 0;
 
     if (!adev) {
         ALOGE("%s: Invalid params", __func__);
@@ -472,7 +473,20 @@
     handle.pcm_tx = NULL;
 
     /* Clear TX calibration to handset mic */
-    platform_send_audio_calibration(adev->platform, SND_DEVICE_IN_HANDSET_MIC);
+    if (platform_supports_app_type_cfg()) {
+        ALOGD("%s: Platform supports APP type configuration, using V2\n", __func__);
+        if (uc_info_tx != NULL) {
+            ALOGD("%s: UC Info TX is not NULL, updating and sending calibration\n", __func__);
+            uc_info_tx->in_snd_device = SND_DEVICE_IN_HANDSET_MIC;
+            uc_info_tx->out_snd_device = SND_DEVICE_NONE;
+            platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE, &app_type);
+            platform_send_audio_calibration_v2(adev->platform, uc_info_tx,
+                                               app_type, 8000);
+        }
+    } else {
+        ALOGW("%s: Platform does NOT support APP type configuration, using V1\n", __func__);
+        platform_send_audio_calibration(adev->platform, SND_DEVICE_IN_HANDSET_MIC);
+    }
     if (!status.status) {
         protCfg.mode = MSM_SPKR_PROT_CALIBRATED;
         protCfg.r0[SP_V2_SPKR_1] = status.r0[SP_V2_SPKR_1];
@@ -804,6 +818,7 @@
     struct audio_usecase *uc_info_tx;
     struct audio_device *adev = handle.adev_handle;
     int32_t pcm_dev_tx_id = -1, ret = 0;
+    int app_type = 0;
 
     ALOGV("%s: Entry", __func__);
     if (!adev) {
@@ -855,7 +870,20 @@
 
 exit:
     /* Clear VI feedback cal and replace with handset MIC  */
-    platform_send_audio_calibration(adev->platform, SND_DEVICE_IN_HANDSET_MIC);
+    if (platform_supports_app_type_cfg()) {
+        ALOGD("%s: Platform supports APP type configuration, using V2\n", __func__);
+        if (uc_info_tx != NULL) {
+            ALOGD("%s: UC Info TX is not NULL, updating and sending calibration\n", __func__);
+            uc_info_tx->in_snd_device = SND_DEVICE_IN_HANDSET_MIC;
+            uc_info_tx->out_snd_device = SND_DEVICE_NONE;
+            platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE, &app_type);
+            platform_send_audio_calibration_v2(adev->platform, uc_info_tx,
+                                               app_type, 8000);
+        }
+    } else {
+        ALOGW("%s: Platform does not support APP type configuration, using V1\n", __func__);
+        platform_send_audio_calibration(adev->platform, SND_DEVICE_IN_HANDSET_MIC);
+    }
     if (ret) {
         if (handle.pcm_tx)
             pcm_close(handle.pcm_tx);
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 7267931..85c886b 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -51,6 +51,7 @@
 #include <audio_utils/clock.h>
 #include "audio_hw.h"
 #include "audio_extn.h"
+#include "audio_perf.h"
 #include "platform_api.h"
 #include <platform.h>
 #include "voice_extn.h"
@@ -999,9 +1000,9 @@
     return ret;
 }
 
-static ssize_t read_usb_sup_sample_rates(bool is_playback __unused,
-                                         uint32_t *supported_sample_rates __unused,
-                                         uint32_t max_rates __unused)
+static ssize_t read_usb_sup_sample_rates(bool is_playback,
+                                         uint32_t *supported_sample_rates,
+                                         uint32_t max_rates)
 {
     ssize_t count = audio_extn_usb_sup_sample_rates(is_playback,
                                                     supported_sample_rates,
@@ -1460,6 +1461,7 @@
 
     list_add_tail(&adev->usecase_list, &uc_info->list);
 
+    audio_streaming_hint_start();
     audio_extn_perf_lock_acquire();
 
     select_devices(adev, in->usecase);
@@ -1526,6 +1528,7 @@
         }
     }
     register_in_stream(in);
+    audio_streaming_hint_end();
     audio_extn_perf_lock_release();
     ALOGV("%s: exit", __func__);
 
@@ -1533,6 +1536,7 @@
 
 error_open:
     stop_input_stream(in);
+    audio_streaming_hint_end();
     audio_extn_perf_lock_release();
 
 error_config:
@@ -1799,6 +1803,9 @@
             adev->visualizer_stop_output(out->handle, out->pcm_device_id);
         if (adev->offload_effects_stop_output != NULL)
             adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
+    } else if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL ||
+               out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
+        audio_low_latency_hint_end();
     }
 
     /* 1. Get and set stream specific mixer controls */
@@ -1867,6 +1874,7 @@
 
     list_add_tail(&adev->usecase_list, &uc_info->list);
 
+    audio_streaming_hint_start();
     audio_extn_perf_lock_acquire();
 
     select_devices(adev, out->usecase);
@@ -1953,9 +1961,15 @@
         }
     }
     register_out_stream(out);
+    audio_streaming_hint_end();
     audio_extn_perf_lock_release();
     audio_extn_tfa_98xx_enable_speaker();
 
+    if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL ||
+        out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
+        audio_low_latency_hint_start();
+    }
+
     // consider a scenario where on pause lower layers are tear down.
     // so on resume, swap mixer control need to be sent only when
     // backend is active, hence rather than sending from enable device
@@ -1966,6 +1980,7 @@
     ALOGV("%s: exit", __func__);
     return 0;
 error_open:
+    audio_streaming_hint_end();
     audio_extn_perf_lock_release();
     stop_output_stream(out);
 error_config:
@@ -3680,18 +3695,17 @@
 {
     struct audio_device *adev = (struct audio_device *)dev;
     struct stream_out *out;
-    int i, ret;
+    int i, ret = 0;
     bool is_hdmi = devices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
     bool is_usb_dev = audio_is_usb_out_device(devices) &&
                       (devices != AUDIO_DEVICE_OUT_USB_ACCESSORY);
-    bool direct_dev = is_hdmi || is_usb_dev;
 
     if (is_usb_dev && !is_usb_ready(adev, true /* is_playback */)) {
         return -ENOSYS;
     }
 
-    ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
-          __func__, config->sample_rate, config->channel_mask, devices, flags);
+    ALOGV("%s: enter: format(%#x) sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
+          __func__, config->format, config->sample_rate, config->channel_mask, devices, flags);
     *stream_out = NULL;
     out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
 
@@ -3701,16 +3715,14 @@
     out->flags = flags;
     out->devices = devices;
     out->dev = adev;
-    out->format = config->format;
-    out->sample_rate = config->sample_rate;
-    out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
     out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
     out->handle = handle;
 
     /* Init use case and pcm_config */
-    if (audio_is_linear_pcm(out->format) &&
-        (out->flags == AUDIO_OUTPUT_FLAG_NONE ||
-         out->flags == AUDIO_OUTPUT_FLAG_DIRECT) && direct_dev) {
+    if ((is_hdmi || is_usb_dev) &&
+        (audio_is_linear_pcm(config->format) || config->format == AUDIO_FORMAT_DEFAULT) &&
+        (flags == AUDIO_OUTPUT_FLAG_NONE ||
+        (flags & AUDIO_OUTPUT_FLAG_DIRECT) != 0)) {
         pthread_mutex_lock(&adev->lock);
         if (is_hdmi) {
             ret = read_hdmi_channel_masks(out);
@@ -3732,25 +3744,38 @@
                                                   &out->supported_sample_rates[0],
                                                   MAX_SUPPORTED_SAMPLE_RATES);
             ALOGV("plugged dev USB ret %d", ret);
-        } else {
-            ret = -1;
         }
         pthread_mutex_unlock(&adev->lock);
         if (ret != 0)
             goto error_open;
 
-        out->channel_mask = config->channel_mask;
+
         out->sample_rate = config->sample_rate;
+        out->channel_mask = config->channel_mask;
         out->format = config->format;
-        out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
-        // does this change?
-        out->config = is_hdmi ? pcm_config_hdmi_multi : pcm_config_hifi;
-        out->config.rate = config->sample_rate;
+        if (is_hdmi) {
+            out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
+            out->config = pcm_config_hdmi_multi;
+        } else if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
+            out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
+            out->config = pcm_config_mmap_playback;
+            out->stream.start = out_start;
+            out->stream.stop = out_stop;
+            out->stream.create_mmap_buffer = out_create_mmap_buffer;
+            out->stream.get_mmap_position = out_get_mmap_position;
+        } else {
+            out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
+            out->config = pcm_config_hifi;
+        }
+
+        out->config.rate = out->sample_rate;
         out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
-        out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
-                                                         audio_bytes_per_sample(config->format));
+        if (is_hdmi) {
+            out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
+                                                         audio_bytes_per_sample(out->format));
+        }
         out->config.format = pcm_format_from_audio_format(out->format);
-    } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
+    } else if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
         pthread_mutex_lock(&adev->lock);
         bool offline = (adev->card_status == CARD_STATUS_OFFLINE);
         pthread_mutex_unlock(&adev->lock);
@@ -3773,17 +3798,20 @@
             ret = -EINVAL;
             goto error_open;
         }
+        out->sample_rate = config->offload_info.sample_rate;
+        if (config->offload_info.channel_mask != AUDIO_CHANNEL_NONE)
+            out->channel_mask = config->offload_info.channel_mask;
+        else if (config->channel_mask != AUDIO_CHANNEL_NONE)
+            out->channel_mask = config->channel_mask;
+        else
+            out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
+
+        out->format = config->offload_info.format;
 
         out->compr_config.codec = (struct snd_codec *)
                                     calloc(1, sizeof(struct snd_codec));
 
         out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
-        if (config->offload_info.channel_mask)
-            out->channel_mask = config->offload_info.channel_mask;
-        else if (config->channel_mask)
-            out->channel_mask = config->channel_mask;
-        out->format = config->offload_info.format;
-        out->sample_rate = config->offload_info.sample_rate;
 
         out->stream.set_callback = out_set_callback;
         out->stream.pause = out_pause;
@@ -3795,11 +3823,11 @@
                 get_snd_codec_id(config->offload_info.format);
         out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
         out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
-        out->compr_config.codec->sample_rate = config->offload_info.sample_rate;
+        out->compr_config.codec->sample_rate = out->sample_rate;
         out->compr_config.codec->bit_rate =
                     config->offload_info.bit_rate;
         out->compr_config.codec->ch_in =
-                audio_channel_count_from_out_mask(config->channel_mask);
+                audio_channel_count_from_out_mask(out->channel_mask);
         out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
 
         if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
@@ -3812,46 +3840,136 @@
                 config->offload_info.bit_rate);
     } else  if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) {
         switch (config->sample_rate) {
+            case 0:
+                out->sample_rate = AFE_PROXY_SAMPLING_RATE;
+                break;
             case 8000:
             case 16000:
             case 48000:
                 out->sample_rate = config->sample_rate;
                 break;
             default:
-                out->sample_rate = AFE_PROXY_SAMPLING_RATE;
+                ALOGE("%s: Unsupported sampling rate %d for Telephony TX", __func__,
+                      config->sample_rate);
+                config->sample_rate = AFE_PROXY_SAMPLING_RATE;
+                ret = -EINVAL;
+                break;
         }
-        out->format = AUDIO_FORMAT_PCM_16_BIT;
+        //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
+        switch (config->channel_mask) {
+            case AUDIO_CHANNEL_NONE:
+                out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
+                break;
+            case AUDIO_CHANNEL_OUT_STEREO:
+                out->channel_mask = config->channel_mask;
+                break;
+            default:
+                ALOGE("%s: Unsupported channel mask %#x for Telephony TX", __func__,
+                      config->channel_mask);
+                config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
+                ret = -EINVAL;
+                break;
+        }
+        switch (config->format) {
+            case AUDIO_FORMAT_DEFAULT:
+                out->format = AUDIO_FORMAT_PCM_16_BIT;
+                break;
+            case AUDIO_FORMAT_PCM_16_BIT:
+                out->format = config->format;
+                break;
+            default:
+                ALOGE("%s: Unsupported format %#x for Telephony TX", __func__,
+                      config->format);
+                config->format = AUDIO_FORMAT_PCM_16_BIT;
+                ret = -EINVAL;
+                break;
+        }
+        if (ret != 0)
+            goto error_open;
+
         out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
         out->config = pcm_config_afe_proxy_playback;
+        out->config.rate = out->sample_rate;
+        out->config.channels =
+                audio_channel_count_from_out_mask(out->channel_mask);
+        out->config.format = pcm_format_from_audio_format(out->format);
         adev->voice_tx_output = out;
-    } else if (out->flags == AUDIO_OUTPUT_FLAG_VOIP_RX) {
+    } else if (flags == AUDIO_OUTPUT_FLAG_VOIP_RX) {
+        switch (config->sample_rate) {
+            case 0:
+                out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
+                break;
+            case 8000:
+            case 16000:
+            case 32000:
+            case 48000:
+                out->sample_rate = config->sample_rate;
+                break;
+            default:
+                ALOGE("%s: Unsupported sampling rate %d for Voip RX", __func__,
+                      config->sample_rate);
+                config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
+                ret = -EINVAL;
+                break;
+        }
         //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
+        switch (config->channel_mask) {
+            case AUDIO_CHANNEL_NONE:
+                out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
+                break;
+            case AUDIO_CHANNEL_OUT_STEREO:
+                out->channel_mask = config->channel_mask;
+                break;
+            default:
+                ALOGE("%s: Unsupported channel mask %#x for Voip RX", __func__,
+                      config->channel_mask);
+                config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
+                ret = -EINVAL;
+                break;
+        }
+        switch (config->format) {
+            case AUDIO_FORMAT_DEFAULT:
+                out->format = AUDIO_FORMAT_PCM_16_BIT;
+                break;
+            case AUDIO_FORMAT_PCM_16_BIT:
+                out->format = config->format;
+                break;
+            default:
+                ALOGE("%s: Unsupported format %#x for Voip RX", __func__,
+                      config->format);
+                config->format = AUDIO_FORMAT_PCM_16_BIT;
+                ret = -EINVAL;
+                break;
+        }
+        if (ret != 0)
+            goto error_open;
+
         uint32_t buffer_size, frame_size;
         out->usecase = USECASE_AUDIO_PLAYBACK_VOIP;
         out->config = pcm_config_voip;
-        out->config.format = pcm_format_from_audio_format(config->format);
-        out->config.rate = config->sample_rate;
+        out->config.rate = out->sample_rate;
+        out->config.format = pcm_format_from_audio_format(out->format);
         buffer_size = get_stream_buffer_size(VOIP_PLAYBACK_PERIOD_DURATION_MSEC,
-                                             config->sample_rate,
-                                             config->format,
+                                             out->sample_rate,
+                                             out->format,
                                              out->config.channels,
                                              false /*is_low_latency*/);
-        frame_size = audio_bytes_per_sample(config->format) * out->config.channels;
+        frame_size = audio_bytes_per_sample(out->format) * out->config.channels;
         out->config.period_size = buffer_size / frame_size;
         out->config.period_count = VOIP_PLAYBACK_PERIOD_COUNT;
         out->af_period_multiplier = 1;
     } else {
-        if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
+        if (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
             out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
             out->config = pcm_config_deep_buffer;
-        } else if (out->flags & AUDIO_OUTPUT_FLAG_TTS) {
+        } else if (flags & AUDIO_OUTPUT_FLAG_TTS) {
             out->usecase = USECASE_AUDIO_PLAYBACK_TTS;
             out->config = pcm_config_deep_buffer;
-        } else if (out->flags & AUDIO_OUTPUT_FLAG_RAW) {
+        } else if (flags & AUDIO_OUTPUT_FLAG_RAW) {
             out->usecase = USECASE_AUDIO_PLAYBACK_ULL;
             out->realtime = may_use_noirq_mode(adev, USECASE_AUDIO_PLAYBACK_ULL, out->flags);
             out->config = out->realtime ? pcm_config_rt : pcm_config_low_latency;
-        } else if (out->flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
+        } else if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
             out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
             out->config = pcm_config_mmap_playback;
             out->stream.start = out_start;
@@ -3862,15 +3980,38 @@
             out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
             out->config = pcm_config_low_latency;
         }
-        if (config->format != audio_format_from_pcm_format(out->config.format)) {
-            out->config.format = pcm_format_from_audio_format(config->format);
+
+        if (config->sample_rate == 0) {
+            out->sample_rate = out->config.rate;
+        } else {
+            out->sample_rate = config->sample_rate;
         }
-        out->sample_rate = out->config.rate;
+        if (config->channel_mask == AUDIO_CHANNEL_NONE) {
+            out->channel_mask = audio_channel_out_mask_from_count(out->config.channels);
+        } else {
+            out->channel_mask = config->channel_mask;
+        }
+        if (config->format == AUDIO_FORMAT_DEFAULT)
+            out->format = audio_format_from_pcm_format(out->config.format);
+        else if (!audio_is_linear_pcm(config->format)) {
+            config->format = AUDIO_FORMAT_PCM_16_BIT;
+            ret = -EINVAL;
+            goto error_open;
+        } else {
+            out->format = config->format;
+        }
+
+        out->config.rate = out->sample_rate;
+        out->config.channels =
+                audio_channel_count_from_out_mask(out->channel_mask);
+        if (out->format != audio_format_from_pcm_format(out->config.format)) {
+            out->config.format = pcm_format_from_audio_format(out->format);
+        }
     }
 
     if ((config->sample_rate != 0 && config->sample_rate != out->sample_rate) ||
         (config->format != AUDIO_FORMAT_DEFAULT && config->format != out->format) ||
-        (config->channel_mask != 0 && config->channel_mask != out->channel_mask)) {
+        (config->channel_mask != AUDIO_CHANNEL_NONE && config->channel_mask != out->channel_mask)) {
         ALOGI("%s: Unsupported output config. sample_rate:%u format:%#x channel_mask:%#x",
               __func__, config->sample_rate, config->format, config->channel_mask);
         config->sample_rate = out->sample_rate;
@@ -3999,6 +4140,7 @@
     out->error_log = NULL;
 
     pthread_cond_destroy(&out->cond);
+    pthread_mutex_destroy(&out->pre_lock);
     pthread_mutex_destroy(&out->lock);
     free(stream);
     ALOGV("%s: exit", __func__);
@@ -4544,6 +4686,9 @@
     error_log_destroy(in->error_log);
     in->error_log = NULL;
 
+    pthread_mutex_destroy(&in->pre_lock);
+    pthread_mutex_destroy(&in->lock);
+
     free(stream);
 
     return;
@@ -4711,6 +4856,7 @@
         }
         if (adev->adm_deinit)
             adev->adm_deinit(adev->adm_data);
+        pthread_mutex_destroy(&adev->lock);
         free(device);
     }
 
diff --git a/hal/audio_perf.cpp b/hal/audio_perf.cpp
new file mode 100644
index 0000000..671a324
--- /dev/null
+++ b/hal/audio_perf.cpp
@@ -0,0 +1,116 @@
+/*
+ * Copyright (C) 2017 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_primary"
+
+#include <cinttypes>
+
+#include <utils/Log.h>
+#include <utils/Mutex.h>
+
+#include <android/hardware/power/1.2/IPower.h>
+
+#include "audio_perf.h"
+
+using android::hardware::power::V1_2::IPower;
+using android::hardware::power::V1_2::PowerHint;
+using android::hardware::power::V1_2::toString;
+using android::hardware::Return;
+using android::hardware::Void;
+using android::hardware::hidl_death_recipient;
+using android::hidl::base::V1_0::IBase;
+
+// Do not use gPowerHAL, use getPowerHal to retrieve a copy instead
+static android::sp<IPower> gPowerHal_ = nullptr;
+// Protect gPowerHal_
+static std::mutex gPowerHalMutex;
+
+// PowerHalDeathRecipient to invalid the client when service dies
+struct PowerHalDeathRecipient : virtual public hidl_death_recipient {
+    // hidl_death_recipient interface
+    virtual void serviceDied(uint64_t, const android::wp<IBase>&) override {
+        std::lock_guard<std::mutex> lock(gPowerHalMutex);
+        ALOGE("PowerHAL just died");
+        gPowerHal_ = nullptr;
+    }
+};
+
+// Retrieve a copy of client
+static android::sp<IPower> getPowerHal() {
+    std::lock_guard<std::mutex> lock(gPowerHalMutex);
+    static android::sp<PowerHalDeathRecipient> gPowerHalDeathRecipient = nullptr;
+    static bool gPowerHalExists = true;
+
+    if (gPowerHalExists && gPowerHal_ == nullptr) {
+        gPowerHal_ = IPower::getService();
+
+        if (gPowerHal_ == nullptr) {
+            ALOGE("Unable to get Power service");
+            gPowerHalExists = false;
+        } else {
+            if (gPowerHalDeathRecipient == nullptr) {
+                gPowerHalDeathRecipient = new PowerHalDeathRecipient();
+            }
+            Return<bool> linked = gPowerHal_->linkToDeath(
+                gPowerHalDeathRecipient, 0 /* cookie */);
+            if (!linked.isOk()) {
+                ALOGE("Transaction error in linking to PowerHAL death: %s",
+                      linked.description().c_str());
+                gPowerHal_ = nullptr;
+            } else if (!linked) {
+                ALOGW("Unable to link to PowerHal death notifications");
+                gPowerHal_ = nullptr;
+            } else {
+                ALOGD("Connect to PowerHAL and link to death "
+                      "notification successfully");
+            }
+        }
+    }
+    return gPowerHal_;
+}
+
+static bool powerHint(PowerHint hint, int32_t data) {
+    android::sp<IPower> powerHal = getPowerHal();
+    if (powerHal == nullptr) {
+        return false;
+    }
+
+    auto ret = powerHal->powerHintAsync_1_2(hint, data);
+
+    if (!ret.isOk()) {
+        ALOGE("powerHint failed, hint: %s, data: %" PRId32 ",  error: %s",
+              toString(hint).c_str(),
+              data,
+              ret.description().c_str());
+    }
+    return ret.isOk();
+}
+
+int audio_streaming_hint_start() {
+    return powerHint(PowerHint::AUDIO_STREAMING, 1);
+}
+
+int audio_streaming_hint_end() {
+    return powerHint(PowerHint::AUDIO_STREAMING, 0);
+}
+
+int audio_low_latency_hint_start() {
+    return powerHint(PowerHint::AUDIO_LOW_LATENCY, 1);
+}
+
+int audio_low_latency_hint_end() {
+    return powerHint(PowerHint::AUDIO_LOW_LATENCY, 0);
+}
diff --git a/hal/audio_perf.h b/hal/audio_perf.h
new file mode 100644
index 0000000..4263086
--- /dev/null
+++ b/hal/audio_perf.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2017 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.
+ */
+
+#ifndef __QAUDIOPERF_H__
+#define __QAUDIOPERF_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int audio_streaming_hint_start();
+int audio_streaming_hint_end();
+
+int audio_low_latency_hint_start();
+int audio_low_latency_hint_end();
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif //__QCAMREAPERF_H__
diff --git a/hal/msm8916/platform.c b/hal/msm8916/platform.c
index c113eca..5f056a8 100644
--- a/hal/msm8916/platform.c
+++ b/hal/msm8916/platform.c
@@ -34,10 +34,10 @@
 #include "audio_extn/tfa_98xx.h"
 #include <dirent.h>
 #define MAX_MIXER_XML_PATH  100
-#define MIXER_XML_PATH "/system/etc/mixer_paths.xml"
-#define MIXER_XML_PATH_MTP "/system/etc/mixer_paths_mtp.xml"
-#define MIXER_XML_PATH_MSM8909_PM8916 "/system/etc/mixer_paths_msm8909_pm8916.xml"
-#define MIXER_XML_PATH_L9300 "/system/etc/mixer_paths_l9300.xml"
+#define MIXER_XML_PATH "mixer_paths.xml"
+#define MIXER_XML_PATH_MTP "mixer_paths_mtp.xml"
+#define MIXER_XML_PATH_MSM8909_PM8916 "mixer_paths_msm8909_pm8916.xml"
+#define MIXER_XML_PATH_L9300 "mixer_paths_l9300.xml"
 
 #define LIB_ACDB_LOADER "libacdbloader.so"
 #define AUDIO_DATA_BLOCK_MIXER_CTL "HDMI EDID"
@@ -485,6 +485,7 @@
     {TO_NAME_INDEX(USECASE_VOICEMMODE1_CALL)},
     {TO_NAME_INDEX(USECASE_VOICEMMODE2_CALL)},
     {TO_NAME_INDEX(USECASE_AUDIO_HFP_SCO)},
+    {TO_NAME_INDEX(USECASE_AUDIO_HFP_SCO_WB)},
     {TO_NAME_INDEX(USECASE_AUDIO_SPKR_CALIB_TX)},
 };
 
@@ -836,6 +837,28 @@
     return result;
 }
 
+// Treblized config files will be located in /odm/etc or /vendor/etc.
+static const char *kConfigLocationList[] =
+        {"/odm/etc", "/vendor/etc", "/system/etc"};
+static const int kConfigLocationListSize =
+        (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
+
+bool resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH]) {
+    char full_config_path[MIXER_PATH_MAX_LENGTH];
+    for (int i = 0; i < kConfigLocationListSize; i++) {
+        snprintf(full_config_path,
+                 MIXER_PATH_MAX_LENGTH,
+                 "%s/%s",
+                 kConfigLocationList[i],
+                 file_name);
+        if (F_OK == access(full_config_path, 0)) {
+            strcpy(file_name, full_config_path);
+            return true;
+        }
+    }
+    return false;
+}
+
 void *platform_init(struct audio_device *adev)
 {
     char platform[PROPERTY_VALUE_MAX] = {0};
@@ -845,6 +868,7 @@
     int retry_num = 0, snd_card_num = 0, key = 0;
     const char *snd_card_name;
     char mixer_xml_path[MAX_MIXER_XML_PATH] = {0};
+    char platform_info_path[MAX_MIXER_XML_PATH] = {0};
     char ffspEnable[PROPERTY_VALUE_MAX] = {0};
     char *cvd_version = NULL;
     int idx;
@@ -897,6 +921,11 @@
             ALOGE("%s: Failed to init hardware info", __func__);
         } else {
             query_platform(snd_card_name, mixer_xml_path);
+            if (!resolve_config_file(mixer_xml_path)) {
+                memset(mixer_xml_path, 0, sizeof(mixer_xml_path));
+                strlcpy(mixer_xml_path, MIXER_XML_PATH, MAX_MIXER_XML_PATH);
+                resolve_config_file(mixer_xml_path);
+            }
             ALOGD("%s: mixer path file is %s", __func__,
                                     mixer_xml_path);
             adev->audio_route = audio_route_init(snd_card_num,
@@ -1020,7 +1049,9 @@
     set_platform_defaults();
 
     /* Initialize ACDB and PCM ID's */
-    platform_info_init(PLATFORM_INFO_XML_PATH, my_data);
+    strlcpy(platform_info_path, PLATFORM_INFO_XML_PATH, MAX_MIXER_XML_PATH);
+    resolve_config_file(platform_info_path);
+    platform_info_init(platform_info_path, my_data);
 
 
     /* Read one time ssr property */
diff --git a/hal/msm8916/platform.h b/hal/msm8916/platform.h
index c61331a..de81a71 100644
--- a/hal/msm8916/platform.h
+++ b/hal/msm8916/platform.h
@@ -277,7 +277,7 @@
     stop_record_t stop_record;
 };
 
-#define PLATFORM_INFO_XML_PATH          "/system/etc/audio_platform_info.xml"
-#define PLATFORM_INFO_XML_BASE_STRING   "/system/etc/audio_platform_info"
+#define PLATFORM_INFO_XML_PATH          "audio_platform_info.xml"
+#define PLATFORM_INFO_XML_BASE_STRING   "audio_platform_info"
 
 #endif // QCOM_AUDIO_PLATFORM_H
diff --git a/hal/msm8974/platform.c b/hal/msm8974/platform.c
index 6560d14..4addc80 100644
--- a/hal/msm8974/platform.c
+++ b/hal/msm8974/platform.c
@@ -30,7 +30,7 @@
 #include "platform.h"
 #include "audio_extn.h"
 #include <linux/msm_audio.h>
-#if defined (PLATFORM_MSM8996) || (PLATFORM_MSM8998)
+#if defined (PLATFORM_MSM8996) || (PLATFORM_MSM8998) || (PLATFORM_SDM845)
 #include <sound/devdep_params.h>
 #endif
 
@@ -120,7 +120,7 @@
     bool speaker_lr_swap;
 
     void *acdb_handle;
-#if defined (PLATFORM_MSM8994) || (PLATFORM_MSM8996) || (PLATFORM_MSM8998)
+#if defined (PLATFORM_MSM8994) || (PLATFORM_MSM8996) || (PLATFORM_MSM8998) || (PLATFORM_SDM845)
     acdb_init_v2_cvd_t acdb_init;
 #elif defined (PLATFORM_MSM8084)
     acdb_init_v2_t acdb_init;
@@ -720,7 +720,7 @@
 
 inline bool platform_supports_app_type_cfg()
 {
-#ifdef PLATFORM_MSM8998
+#if defined (PLATFORM_MSM8998) || (PLATFORM_SDM845)
     return true;
 #else
     return false;
@@ -1146,7 +1146,7 @@
         return 0;
     }
 
-#if defined (PLATFORM_MSM8994) || (PLATFORM_MSM8996) || (PLATFORM_MSM8998)
+#if defined (PLATFORM_MSM8994) || (PLATFORM_MSM8996) || (PLATFORM_MSM8998) || (PLATFORM_SDM845)
     char *cvd_version = calloc(1, MAX_CVD_VERSION_STRING_SIZE);
     if (!cvd_version)
         ALOGE("failed to allocate cvd_version");
@@ -1472,7 +1472,7 @@
             ALOGV("%s: Could not find the symbol acdb_loader_send_gain_dep_cal from %s",
                   __func__, LIB_ACDB_LOADER);
 
-#if defined (PLATFORM_MSM8994) || (PLATFORM_MSM8996) || (PLATFORM_MSM8998)
+#if defined (PLATFORM_MSM8994) || (PLATFORM_MSM8996) || (PLATFORM_MSM8998) || (PLATFORM_SDM845)
         acdb_init_v2_cvd_t acdb_init_local;
         acdb_init_local = (acdb_init_v2_cvd_t)dlsym(my_data->acdb_handle,
                                               "acdb_loader_init_v2");
@@ -4112,7 +4112,7 @@
 int platform_get_mmap_data_fd(void *platform __unused, int fe_dev __unused, int dir __unused,
                               int *fd __unused, uint32_t *size __unused)
 {
-#if defined (PLATFORM_MSM8996) || (PLATFORM_MSM8998)
+#if defined (PLATFORM_MSM8996) || (PLATFORM_MSM8998) || (PLATFORM_SDM845)
     struct platform_data *my_data = (struct platform_data *)platform;
     struct audio_device *adev = my_data->adev;
     int hw_fd = -1;
diff --git a/hal/voice_extn/voice_extn.c b/hal/voice_extn/voice_extn.c
index edf5523..01a1c3f 100644
--- a/hal/voice_extn/voice_extn.c
+++ b/hal/voice_extn/voice_extn.c
@@ -162,6 +162,7 @@
     struct voice_session *session = NULL;
     int fd = 0;
     int ret = 0;
+    bool is_voice_sess_active = false;
 
     ALOGD("%s: enter:", __func__);
 
@@ -222,6 +223,12 @@
                           __func__, usecase_id);
                 } else {
                     session->state.current = session->state.new;
+
+                    // The flag is not reset if another voice session is active as routing/mode is
+                    // set globally instead of per session.
+                    voice_extn_is_call_state_active(adev, &is_voice_sess_active);
+                    if (!is_voice_sess_active)
+                        adev->voice.in_call = false;
                 }
                 break;
 
diff --git a/post_proc/Android.mk b/post_proc/Android.mk
index 1a2f2e8..c31b769 100644
--- a/post_proc/Android.mk
+++ b/post_proc/Android.mk
@@ -1,4 +1,4 @@
-ifneq ($(filter msm8974 msm8226 msm8084 msm8992 msm8994 msm8996 msm8909 msm8998,$(TARGET_BOARD_PLATFORM)),)
+ifneq ($(filter msm8974 msm8226 msm8084 msm8992 msm8994 msm8996 msm8909 msm8998 sdm845,$(TARGET_BOARD_PLATFORM)),)
 
 LOCAL_PATH:= $(call my-dir)
 
@@ -43,7 +43,7 @@
 
 ################################################################################
 
-ifneq ($(filter msm8992 msm8994 msm8996 msm8909 msm8998,$(TARGET_BOARD_PLATFORM)),)
+ifneq ($(filter msm8992 msm8994 msm8996 msm8909 msm8998 sdm845,$(TARGET_BOARD_PLATFORM)),)
 
 include $(CLEAR_VARS)