dynamic usb profile

- Add support for dynamic USB profile
- Add support for 24 bit end to end pcm playback

Bug: 37304195
Test: phone call, playback, capture with and without USB headset

Change-Id: Idb0b7de8e32a8c8aca335de6bbd9f160b7b44b8b
Signed-off-by: David Lin <dtwlin@google.com>
diff --git a/hal/audio_extn/audio_extn.h b/hal/audio_extn/audio_extn.h
index b734cb6..ae71fe0 100644
--- a/hal/audio_extn/audio_extn.h
+++ b/hal/audio_extn/audio_extn.h
@@ -79,6 +79,9 @@
 #define audio_extn_usb_enable_sidetone(device, enable)                 (0)
 #define audio_extn_usb_set_sidetone_gain(parms, value, len)            (0)
 #define audio_extn_usb_is_capture_supported()                          (false)
+#define audio_extn_usb_get_max_channels()                              (0)
+#define audio_extn_usb_get_max_bit_width()                             (0)
+#define audio_extn_usb_sup_sample_rates(t, s, l)                       (0)
 #else
 void audio_extn_usb_init(void *adev);
 void audio_extn_usb_deinit();
@@ -92,6 +95,9 @@
 int audio_extn_usb_set_sidetone_gain(struct str_parms *parms,
                                      char *value, int len);
 bool audio_extn_usb_is_capture_supported();
+int audio_extn_usb_get_max_channels();
+int audio_extn_usb_get_max_bit_width();
+int audio_extn_usb_sup_sample_rates(int type, uint32_t *sr, uint32_t l);
 #endif
 
 
@@ -137,6 +143,8 @@
 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);
+void audio_extn_utils_send_audio_calibration(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/usb.c b/hal/audio_extn/usb.c
index 3747318..a1af141 100644
--- a/hal/audio_extn/usb.c
+++ b/hal/audio_extn/usb.c
@@ -42,9 +42,12 @@
 #define SAMPLE_RATE_11025         11025
 /* TODO: dynamically populate supported sample rates */
 static uint32_t supported_sample_rates[] =
-    {44100, 48000, 64000, 88200, 96000, 176400, 192000, 384000};
+    {192000, 176400, 96000, 88200, 64000, 48000, 44100};
+static uint32_t supported_sample_rates_mask[2];
+static const uint32_t MAX_SAMPLE_RATE_SIZE =
+        (sizeof(supported_sample_rates)/sizeof(supported_sample_rates[0]));
 
-#define  MAX_SAMPLE_RATE_SIZE  sizeof(supported_sample_rates)/sizeof(supported_sample_rates[0])
+// assert on sizeof bm v/s size of rates if needed
 
 enum usb_usecase_type{
     USB_PLAYBACK = 0,
@@ -211,7 +214,7 @@
     return 0;
 }
 
-static int usb_get_sample_rates(char *rates_str,
+static int usb_get_sample_rates(int type, char *rates_str,
                                 struct usb_device_config *config)
 {
     uint32_t i;
@@ -242,6 +245,7 @@
             if (supported_sample_rates[i] >= min_sr &&
                 supported_sample_rates[i] <= max_sr) {
                 config->rates[sr_size++] = supported_sample_rates[i];
+                supported_sample_rates_mask[type] |= (1<<i);
                 ALOGI_IF(usb_audio_debug_enable,
                     "%s: continuous sample rate supported_sample_rates[%d] %d",
                     __func__, i, supported_sample_rates[i]);
@@ -256,6 +260,7 @@
                         "%s: sr %d, supported_sample_rates[%d] %d -> matches!!",
                         __func__, sr, i, supported_sample_rates[i]);
                     config->rates[sr_size++] = supported_sample_rates[i];
+                    supported_sample_rates_mask[type] |= (1<<i);
                 }
             }
             next_sr_string = strtok_r(NULL, " ,.-", &temp_ptr);
@@ -418,7 +423,7 @@
         }
         memcpy(rates_str, rates_str_start, size);
         rates_str[size] = '\0';
-        ret = usb_get_sample_rates(rates_str, usb_device_info);
+        ret = usb_get_sample_rates(type, rates_str, usb_device_info);
         if (rates_str)
             free(rates_str);
         if (ret < 0) {
@@ -876,6 +881,68 @@
     return true;
 }
 
+#define _MAX(x, y) (((x) >= (y)) ? (x) : (y))
+#define _MIN(x, y) (((x) <= (y)) ? (x) : (y))
+
+int audio_extn_usb_get_max_channels()
+{
+    struct listnode *node_i, *node_j;
+    struct usb_device_config *dev_info;
+    struct usb_card_config *card_info;
+    unsigned int max_ch = 1;
+    list_for_each(node_i, &usbmod->usb_card_conf_list) {
+            card_info = node_to_item(node_i, struct usb_card_config, list);
+            list_for_each(node_j, &card_info->usb_device_conf_list) {
+                dev_info = node_to_item(node_j, struct usb_device_config, list);
+                max_ch = _MAX(max_ch, dev_info->channel_count);
+            }
+    }
+
+    return max_ch;
+}
+
+int audio_extn_usb_get_max_bit_width()
+{
+    struct listnode *node_i, *node_j;
+    struct usb_device_config *dev_info;
+    struct usb_card_config *card_info;
+    unsigned int max_bw = 16;
+    list_for_each(node_i, &usbmod->usb_card_conf_list) {
+            card_info = node_to_item(node_i, struct usb_card_config, list);
+            list_for_each(node_j, &card_info->usb_device_conf_list) {
+                dev_info = node_to_item(node_j, struct usb_device_config, list);
+                max_bw = _MAX(max_bw, dev_info->bit_width);
+            }
+    }
+
+    return max_bw;
+}
+
+int audio_extn_usb_sup_sample_rates(int type,
+                                    uint32_t *sample_rates,
+                                    uint32_t sample_rate_size)
+{
+    struct listnode *node_i, *node_j;
+    struct usb_device_config *dev_info;
+    struct usb_card_config *card_info;
+
+    if (type != USB_PLAYBACK && type != USB_CAPTURE)
+        return -1;
+
+    ALOGV("%s supported_sample_rates_mask 0x%x", __func__, supported_sample_rates_mask[type]);
+    uint32_t bm = supported_sample_rates_mask[type];
+    uint32_t tries = _MIN(sample_rate_size, (uint32_t)__builtin_popcount(bm));
+
+    int i = 0;
+    while (tries--) {
+        int idx = __builtin_ffs(bm) - 1;
+        sample_rates[i++] = supported_sample_rates[idx];
+        bm &= ~(1<<idx);
+    }
+
+    return i;
+}
+
 bool audio_extn_usb_is_capture_supported()
 {
     if (usbmod == NULL) {
@@ -997,6 +1064,8 @@
         }
     }
     usbmod->is_capture_supported = false;
+    supported_sample_rates_mask[USB_PLAYBACK] = 0;
+    supported_sample_rates_mask[USB_CAPTURE] = 0;
 exit:
     if (usb_audio_debug_enable)
         usb_print_active_device();
diff --git a/hal/audio_extn/utils.c b/hal/audio_extn/utils.c
index d2bb42c..6e0004f 100644
--- a/hal/audio_extn/utils.c
+++ b/hal/audio_extn/utils.c
@@ -91,7 +91,7 @@
     return;
 }
 
-int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
+static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
                                        struct audio_usecase *usecase)
 {
     struct mixer_ctl *ctl;
@@ -177,3 +177,119 @@
 exit_send_app_type_cfg:
     return rc;
 }
+
+int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
+                                       struct audio_usecase *usecase)
+{
+    int len = 0;
+    if (usecase->type == PCM_HFP_CALL) {
+        return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
+    }
+
+    if (usecase->type != PCM_PLAYBACK || !platform_supports_app_type_cfg())
+        return -1;
+
+    size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
+    int pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
+
+    char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
+    snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
+             "Audio Stream %d App Type Cfg", pcm_device_id);
+    struct mixer_ctl *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);
+        return -EINVAL;
+    }
+
+    snd_device_t snd_device = usecase->out_snd_device; // add speaker prot changes if needed
+    int 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__);
+        return -1;
+    }
+
+    if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
+        usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
+    } else if (snd_device == SND_DEVICE_OUT_USB_HEADSET ||
+               snd_device == SND_DEVICE_OUT_USB_HEADPHONES) {
+        platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
+                                                   usecase->stream.out->sample_rate,
+                                                   &usecase->stream.out->app_type_cfg.sample_rate);
+    }
+
+    int32_t sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
+    int app_type;
+    if (!audio_is_linear_pcm(usecase->stream.out->format)) {
+        platform_get_default_app_type_v2(adev->platform,
+                                         PCM_PLAYBACK,
+                                         &app_type);
+    } else if (usecase->stream.out->format == AUDIO_FORMAT_PCM_16_BIT) {
+        platform_get_app_type_v2(adev->platform,
+                                 16,
+                                 sample_rate,
+                                 PCM_PLAYBACK,
+                                 &app_type);
+    } else if (usecase->stream.out->format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
+               usecase->stream.out->format == AUDIO_FORMAT_PCM_8_24_BIT) {
+        platform_get_app_type_v2(adev->platform,
+                                 24,
+                                 sample_rate,
+                                 PCM_PLAYBACK,
+                                 &app_type);
+    } else if (usecase->stream.out->format == AUDIO_FORMAT_PCM_32_BIT) {
+        platform_get_app_type_v2(adev->platform,
+                                 32,
+                                 sample_rate,
+                                 PCM_PLAYBACK,
+                                 &app_type);
+    } else {
+        ALOGE("%s bad format\n", __func__);
+        return -1;
+    }
+
+    //XXX this would be set somewhere else
+    usecase->stream.out->app_type_cfg.app_type = app_type;
+    app_type_cfg[len++] = app_type;
+    app_type_cfg[len++] = acdb_dev_id;
+    app_type_cfg[len++] = sample_rate;
+
+    // add be_idx once available
+    // if (snd_device_be_idx > 0)
+    //    app_type_cfg[len++] = snd_device_be_idx;
+
+    ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d",
+          __func__, app_type, acdb_dev_id, sample_rate);
+
+    mixer_ctl_set_array(ctl, app_type_cfg, len);
+    return 0;
+}
+
+void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
+                                             struct audio_usecase *usecase)
+{
+    int type = usecase->type;
+    int app_type = 0;
+
+    if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
+        struct stream_out *out = usecase->stream.out;
+        ALOGV("%s send cal for app_type %d, rate %d", __func__, out->app_type_cfg.app_type,
+              usecase->stream.out->app_type_cfg.sample_rate);
+        platform_send_audio_calibration_v2(adev->platform, usecase,
+                                        out->app_type_cfg.app_type,
+                                        usecase->stream.out->app_type_cfg.sample_rate);
+    } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
+        // TBD
+        // platform_send_audio_calibration_v2(adev->platform, usecase,
+        // usecase->stream.in->app_type_cfg.app_type,
+        // usecase->stream.in->app_type_cfg.sample_rate);
+        // uncomment these once send_app_type_cfg and the config entries for
+        // non-16 bit capture are figured out.
+        platform_get_default_app_type_v2(adev->platform, type, &app_type);
+        platform_send_audio_calibration_v2(adev->platform, usecase, app_type, 48000);
+    } else {
+        /* when app type is default. the sample rate is not used to send cal */
+        platform_get_default_app_type_v2(adev->platform, type, &app_type);
+        platform_send_audio_calibration_v2(adev->platform, usecase, app_type, 48000);
+    }
+}
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index f4ce367..fd590a9 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -159,6 +159,17 @@
     .avail_min = MMAP_PERIOD_SIZE, //1 ms
 };
 
+struct pcm_config pcm_config_hifi = {
+    .channels = DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
+    .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
+    .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE, /* change #define */
+    .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
+    .format = PCM_FORMAT_S24_3LE,
+    .start_threshold = 0,
+    .stop_threshold = INT_MAX,
+    .avail_min = 0,
+};
+
 struct pcm_config pcm_config_audio_capture = {
     .channels = DEFAULT_CHANNEL_COUNT,
     .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
@@ -227,7 +238,7 @@
 const char * const use_case_table[AUDIO_USECASE_MAX] = {
     [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
     [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
-    [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
+    [USECASE_AUDIO_PLAYBACK_HIFI] = "hifi-playback",
     [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
     [USECASE_AUDIO_PLAYBACK_TTS] = "audio-tts-playback",
     [USECASE_AUDIO_PLAYBACK_ULL] = "audio-ull-playback",
@@ -532,6 +543,7 @@
         snd_device = usecase->out_snd_device;
 
     audio_extn_utils_send_app_type_cfg(adev, usecase);
+    audio_extn_utils_send_audio_calibration(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);
@@ -952,6 +964,50 @@
     return ret;
 }
 
+static int read_usb_sup_sample_rates(struct stream_out *out)
+{
+    uint32_t *sr = out->supported_sample_rates;
+    size_t count = audio_extn_usb_sup_sample_rates(0 /*playback*/,
+                                                   sr,
+                                                   MAX_SUPPORTED_SAMPLE_RATES);
+#if !LOG_NDEBUG
+
+    for (size_t i=0; i<count; i++) {
+        ALOGV("%s %d", __func__, out->supported_sample_rates[i]);
+    }
+#endif
+    return count > 0 ? 0 : -1;
+}
+
+static int read_usb_sup_channel_masks(struct stream_out *out)
+{
+    int channels = audio_extn_usb_get_max_channels();
+    out->supported_channel_masks[0] =
+            channels < 3 ? audio_channel_out_mask_from_count(channels) :
+                           audio_channel_mask_for_index_assignment_from_count(channels);
+    return 0;
+}
+
+static int read_usb_sup_formats(struct stream_out *out)
+{
+    int bitwidth = audio_extn_usb_get_max_bit_width();
+    switch (bitwidth) {
+        case 24:
+            // XXX : usb.c returns 24 for s24 and s24_le?
+            out->supported_formats[0] = AUDIO_FORMAT_PCM_24_BIT_PACKED;
+            break;
+        case 32:
+            out->supported_formats[0] = AUDIO_FORMAT_PCM_32_BIT;
+            break;
+        case 16:
+        default :
+            out->supported_formats[0] = AUDIO_FORMAT_PCM_16_BIT;
+            break;
+    }
+
+    return 0;
+}
+
 static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
 {
     struct audio_usecase *usecase;
@@ -1519,8 +1575,8 @@
                       __func__);
                 ret = false;
                 break;
-            } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
-                ALOGV("%s: multi channel playback is active, "
+            } else if (usecase->id == USECASE_AUDIO_PLAYBACK_HIFI) {
+                ALOGV("%s: hifi playback is active, "
                       "no change in HDMI channels", __func__);
                 ret = false;
                 break;
@@ -2085,6 +2141,7 @@
     char *str;
     char value[256];
     struct str_parms *reply = str_parms_create();
+    bool replied = false;
     size_t i, j;
     int ret;
     bool first = true;
@@ -2107,6 +2164,62 @@
             i++;
         }
         str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
+        replied = true;
+    }
+
+    ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value, sizeof(value));
+    if (ret >= 0) {
+        value[0] = '\0';
+        switch (out->supported_formats[0]) {
+            case AUDIO_FORMAT_PCM_16_BIT:
+                strcat(value, "AUDIO_FORMAT_PCM_16_BIT");
+                break;
+            case AUDIO_FORMAT_PCM_24_BIT_PACKED:
+                strcat(value, "AUDIO_FORMAT_PCM_24_BIT_PACKED");
+                break;
+            case AUDIO_FORMAT_PCM_32_BIT:
+                strcat(value, "AUDIO_FORMAT_PCM_32_BIT");
+                break;
+            default:
+                ALOGE("%s: unsupported format %#x", __func__,
+                      out->supported_formats[0]);
+                break;
+        }
+        str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
+        replied = true;
+    }
+
+    ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
+                            value, sizeof(value));
+    if (ret >= 0) {
+        value[0] = '\0';
+        i=0;
+        int cursor = 0;
+        while (out->supported_sample_rates[i]) {
+            int avail = sizeof(value) - cursor;
+            ret = snprintf(value + cursor, avail, "%s%d",
+                           cursor > 0 ? "|" : "",
+                           out->supported_sample_rates[i]);
+            if (ret < 0 || ret >= avail) {
+                // if cursor is at the last element of the array
+                //    overwrite with \0 is duplicate work as
+                //    snprintf already put a \0 in place.
+                // else
+                //    we had space to write the '|' at value[cursor]
+                //    (which will be overwritten) or no space to fill
+                //    the first element (=> cursor == 0)
+                value[cursor] = '\0';
+                break;
+            }
+            cursor += ret;
+            ++i;
+        }
+        str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
+                          value);
+        replied = true;
+    }
+
+    if (replied) {
         str = str_parms_to_str(reply);
     } else {
         str = strdup("");
@@ -2144,7 +2257,7 @@
     struct stream_out *out = (struct stream_out *)stream;
     int volume[2];
 
-    if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
+    if (out->usecase == USECASE_AUDIO_PLAYBACK_HIFI) {
         /* only take left channel into account: the API is for stereo anyway */
         out->muted = (left == 0.0f);
         return 0;
@@ -3209,6 +3322,7 @@
     struct audio_device *adev = (struct audio_device *)dev;
     struct stream_out *out;
     int i, ret;
+    const uint32_t direct_dev = (AUDIO_DEVICE_OUT_HDMI|AUDIO_DEVICE_OUT_USB_DEVICE);
 
     ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
           __func__, config->sample_rate, config->channel_mask, devices, flags);
@@ -3228,11 +3342,24 @@
     out->handle = handle;
 
     /* Init use case and pcm_config */
-    if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
-            !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
-        out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
+    if (audio_is_linear_pcm(out->format) &&
+        (out->flags == AUDIO_OUTPUT_FLAG_NONE ||
+         out->flags == AUDIO_OUTPUT_FLAG_DIRECT) &&
+        (out->devices & direct_dev)) {
+
+        bool hdmi = (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL);
+
         pthread_mutex_lock(&adev->lock);
-        ret = read_hdmi_channel_masks(out);
+        if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
+            ret = read_hdmi_channel_masks(out);
+        } else if (out->devices & AUDIO_DEVICE_OUT_USB_DEVICE) {
+            ret = read_usb_sup_formats(out) ||
+                  read_usb_sup_channel_masks(out) ||
+                  read_usb_sup_sample_rates(out);
+            ALOGV("plugged dev USB ret %d", ret);
+        } else {
+            ret = -1;
+        }
         pthread_mutex_unlock(&adev->lock);
         if (ret != 0)
             goto error_open;
@@ -3247,11 +3374,13 @@
         out->channel_mask = config->channel_mask;
         out->sample_rate = config->sample_rate;
         out->format = config->format;
-        out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
-        out->config = pcm_config_hdmi_multi;
+        out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
+        // does this change?
+        out->config = hdmi ? pcm_config_hdmi_multi : pcm_config_hifi;
         out->config.rate = config->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 * 2);
+        out->config.format = pcm_format_from_audio_format(out->format);
     } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
         pthread_mutex_lock(&adev->lock);
         bool offline = (adev->card_status == CARD_STATUS_OFFLINE);
@@ -3950,13 +4079,13 @@
      *
      * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
      * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
-     * [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
+     * [USECASE_AUDIO_PLAYBACK_HIFI] = {1, 1},
      * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
      * [USECASE_AUDIO_RECORD] = {0, 0},
      * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
      * [USECASE_VOICE_CALL] = {2, 2},
      *
-     * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_MULTI_CH omitted.
+     * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_HIFI omitted.
      * USECASE_VOICE_CALL omitted, but possible for either input or output.
      */
 
@@ -4293,7 +4422,8 @@
         }
     }
 
-    audio_extn_utils_send_default_app_type_cfg(adev->platform, adev->mixer);
+    // commented as full set of app type cfg is sent from platform
+    // 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/audio_hw.h b/hal/audio_hw.h
index 03f1587..187bec4 100644
--- a/hal/audio_hw.h
+++ b/hal/audio_hw.h
@@ -50,7 +50,7 @@
 #define ACDB_DEV_TYPE_OUT 1
 #define ACDB_DEV_TYPE_IN 2
 
-#define MAX_SUPPORTED_CHANNEL_MASKS 2
+#define MAX_SUPPORTED_CHANNEL_MASKS 8
 #define MAX_SUPPORTED_FORMATS 15
 #define MAX_SUPPORTED_SAMPLE_RATES 7
 #define DEFAULT_HDMI_OUT_CHANNELS   2
@@ -82,7 +82,7 @@
     /* Playback usecases */
     USECASE_AUDIO_PLAYBACK_DEEP_BUFFER = 0,
     USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
-    USECASE_AUDIO_PLAYBACK_MULTI_CH,
+    USECASE_AUDIO_PLAYBACK_HIFI,
     USECASE_AUDIO_PLAYBACK_OFFLOAD,
     USECASE_AUDIO_PLAYBACK_TTS,
     USECASE_AUDIO_PLAYBACK_ULL,
@@ -167,6 +167,12 @@
     int data[];
 };
 
+struct stream_app_type_cfg {
+    int sample_rate;
+    uint32_t bit_width;
+    int app_type;
+};
+
 struct stream_out {
     struct audio_stream_out stream;
     pthread_mutex_t lock; /* see note below on mutex acquisition order */
@@ -186,9 +192,12 @@
     audio_usecase_t usecase;
     /* Array of supported channel mask configurations. +1 so that the last entry is always 0 */
     audio_channel_mask_t supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1];
+    audio_format_t supported_formats[MAX_SUPPORTED_FORMATS + 1];
+    uint32_t supported_sample_rates[MAX_SUPPORTED_SAMPLE_RATES + 1];
     bool muted;
     uint64_t written; /* total frames written, not cleared when entering standby */
     audio_io_handle_t handle;
+    struct stream_app_type_cfg app_type_cfg;
 
     int non_blocking;
     int playback_started;
@@ -245,7 +254,8 @@
     PCM_PLAYBACK,
     PCM_CAPTURE,
     VOICE_CALL,
-    PCM_HFP_CALL
+    PCM_HFP_CALL,
+    USECASE_TYPE_MAX
 } usecase_type_t;
 
 union stream_ptr {
diff --git a/hal/msm8916/platform.c b/hal/msm8916/platform.c
index 2fe6168..eeca5d3 100644
--- a/hal/msm8916/platform.c
+++ b/hal/msm8916/platform.c
@@ -157,7 +157,7 @@
                                             DEEP_BUFFER_PCM_DEVICE},
     [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {LOWLATENCY_PCM_DEVICE,
                                            LOWLATENCY_PCM_DEVICE},
-    [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {MULTIMEDIA2_PCM_DEVICE,
+    [USECASE_AUDIO_PLAYBACK_HIFI] = {MULTIMEDIA2_PCM_DEVICE,
                                         MULTIMEDIA2_PCM_DEVICE},
     [USECASE_AUDIO_PLAYBACK_OFFLOAD] =
                      {PLAYBACK_OFFLOAD_DEVICE, PLAYBACK_OFFLOAD_DEVICE},
@@ -462,7 +462,7 @@
 static struct name_to_index usecase_name_index[AUDIO_USECASE_MAX] = {
     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_DEEP_BUFFER)},
     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_LOW_LATENCY)},
-    {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_MULTI_CH)},
+    {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_HIFI)},
     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_OFFLOAD)},
     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_ULL)},
     {TO_NAME_INDEX(USECASE_AUDIO_RECORD)},
@@ -2294,3 +2294,30 @@
 {
     return -1;
 }
+
+int platform_send_audio_calibration_v2(void *platform, struct audio_usecase *usecase,
+                                       int app_type, int sample_rate)
+{
+    return -ENOSYS;
+}
+
+void platform_check_and_update_copp_sample_rate(void* platform, snd_device_t snd_device,
+                                                unsigned int stream_sr, int* sample_rate)
+{
+
+}
+
+int platform_get_snd_device_backend_index(snd_device_t snd_device)
+{
+    return -ENOSYS;
+}
+
+bool platform_supports_app_type_cfg() { return false; }
+
+void platform_add_app_type(int bw, const char *uc_type,
+                           int app_type, int max_sr) {}
+
+int platform_get_app_type_v2(void *platform, enum usecase_type_t type,
+                             int bw, int sr, int *app_type) {
+    return -ENOSYS;
+}
diff --git a/hal/msm8960/platform.c b/hal/msm8960/platform.c
index 072a1e4..cfae645 100644
--- a/hal/msm8960/platform.c
+++ b/hal/msm8960/platform.c
@@ -1133,3 +1133,31 @@
 {
     return -1;
 }
+
+int platform_get_snd_device_backend_index(snd_device_t snd_device)
+{
+    return -ENOSYS;
+}
+
+void platform_check_and_update_copp_sample_rate(void* platform, snd_device_t snd_device,
+                                                unsigned int stream_sr, int* sample_rate)
+{
+
+}
+
+int platform_send_audio_calibration_v2(void *platform, struct audio_usecase *usecase,
+                                       int app_type, int sample_rate)
+{
+    return -ENOSYS;
+}
+
+bool platform_supports_app_type_cfg() { return false; }
+
+void platform_add_app_type(int bw, const char *uc_type,
+                           int app_type, int max_sr) {}
+
+
+int platform_get_app_type_v2(void *platform, enum usecase_type_t type,
+                             int bw, int sr, int *app_type) {
+    return -ENOSYS;
+}
diff --git a/hal/msm8974/platform.c b/hal/msm8974/platform.c
index 0f3bcf0..db54f5d 100644
--- a/hal/msm8974/platform.c
+++ b/hal/msm8974/platform.c
@@ -61,7 +61,8 @@
 
 #define MAX_SND_CARD_NAME_LEN 31
 
-#define DEFAULT_APP_TYPE_RX_PATH  0x11130
+#define DEFAULT_APP_TYPE_RX_PATH  69936
+#define DEFAULT_APP_TYPE_TX_PATH  69938
 
 #define TOSTRING_(x) #x
 #define TOSTRING(x) TOSTRING_(x)
@@ -93,6 +94,12 @@
     int acdb_id;
 };
 
+#define BE_DAI_NAME_MAX_LENGTH 24
+struct be_dai_name_struct {
+    unsigned int be_id;
+    char be_name[BE_DAI_NAME_MAX_LENGTH];
+};
+
 static struct listnode operator_info_list;
 static struct listnode *operator_specific_device_table[SND_DEVICE_MAX];
 
@@ -102,6 +109,7 @@
 typedef int  (*acdb_init_v2_t)(char *);
 typedef int  (*acdb_init_t)();
 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_send_gain_dep_cal_t)(int, int, int, int, int);
@@ -129,6 +137,7 @@
 #endif
     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;
     acdb_send_gain_dep_cal_t   acdb_send_gain_dep_cal;
@@ -151,7 +160,7 @@
                                             DEEP_BUFFER_PCM_DEVICE},
     [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {LOWLATENCY_PCM_DEVICE,
                                             LOWLATENCY_PCM_DEVICE},
-    [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {MULTIMEDIA2_PCM_DEVICE,
+    [USECASE_AUDIO_PLAYBACK_HIFI] = {MULTIMEDIA2_PCM_DEVICE,
                                          MULTIMEDIA2_PCM_DEVICE},
     [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {PLAYBACK_OFFLOAD_DEVICE,
                                         PLAYBACK_OFFLOAD_DEVICE},
@@ -532,7 +541,7 @@
 static const struct name_to_index usecase_name_index[AUDIO_USECASE_MAX] = {
     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_DEEP_BUFFER)},
     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_LOW_LATENCY)},
-    {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_MULTI_CH)},
+    {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_HIFI)},
     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_OFFLOAD)},
     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_TTS)},
     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_ULL)},
@@ -558,6 +567,23 @@
     {TO_NAME_INDEX(USECASE_AUDIO_DSM_FEEDBACK)},
 };
 
+static const struct name_to_index usecase_type_index[USECASE_TYPE_MAX] = {
+    {TO_NAME_INDEX(PCM_PLAYBACK)},
+    {TO_NAME_INDEX(PCM_CAPTURE)},
+    {TO_NAME_INDEX(VOICE_CALL)},
+    {TO_NAME_INDEX(PCM_HFP_CALL)},
+};
+
+struct app_type_entry {
+    int uc_type;
+    int bit_width;
+    int app_type;
+    int max_rate;
+    struct listnode node; // membership in app_type_entry_list;
+};
+
+static struct listnode app_type_entry_list;
+
 #define DEEP_BUFFER_PLATFORM_DELAY (29*1000LL)
 #define LOW_LATENCY_PLATFORM_DELAY (13*1000LL)
 #define ULL_PLATFORM_DELAY         (3*1000LL)
@@ -566,6 +592,8 @@
 static pthread_once_t check_op_once_ctl = PTHREAD_ONCE_INIT;
 static bool is_tmus = false;
 
+static int init_be_dai_name_table(struct audio_device *adev);
+
 static void check_operator()
 {
     char value[PROPERTY_VALUE_MAX];
@@ -667,6 +695,15 @@
     return ret;
 }
 
+inline bool platform_supports_app_type_cfg()
+{
+#ifdef PLATFORM_MSM8998
+    return true;
+#else
+    return false;
+#endif
+}
+
 bool platform_send_gain_dep_cal(void *platform, int level)
 {
     bool ret_val = false;
@@ -1122,6 +1159,53 @@
     return false;
 }
 
+static int
+platform_backend_app_type_cfg_init(struct platform_data *pdata,
+                                   struct mixer *mixer)
+{
+    size_t app_type_cfg[128] = {0};
+    int length, num_app_types = 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 -1;
+    }
+
+    length = 1; // reserve index 0 for number of app types
+
+    struct listnode *node;
+    struct app_type_entry *entry;
+    list_for_each(node, &app_type_entry_list) {
+        entry = node_to_item(node, struct app_type_entry, node);
+        app_type_cfg[length++] = entry->app_type;
+        app_type_cfg[length++] = entry->max_rate;
+        app_type_cfg[length++] = entry->bit_width;
+        ALOGI("%s add entry %d %d", __func__, entry->app_type, entry->bit_width);
+        num_app_types += 1;
+    }
+
+    // default for capture
+    int t;
+    platform_get_default_app_type_v2(pdata,
+                                     PCM_CAPTURE,
+                                     &t);
+    app_type_cfg[length++] = t;
+    app_type_cfg[length++] = 48000;
+    app_type_cfg[length++] = 16;
+    num_app_types += 1;
+
+    if (num_app_types) {
+        app_type_cfg[0] = num_app_types;
+        if (mixer_ctl_set_array(ctl, app_type_cfg, length) < 0) {
+            ALOGE("Failed to set app type cfg");
+        }
+    }
+    return 0;
+}
+
 void *platform_init(struct audio_device *adev)
 {
     char value[PROPERTY_VALUE_MAX];
@@ -1140,6 +1224,7 @@
     my_data->adev = adev;
 
     list_init(&operator_info_list);
+    list_init(&app_type_entry_list);
 
     set_platform_defaults(my_data);
     bool card_verifed[MAX_SND_CARD] = {0};
@@ -1360,6 +1445,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_v3_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_v3 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)
@@ -1433,6 +1524,11 @@
 
     platform_backend_config_init(my_data);
 
+    init_be_dai_name_table(adev);
+
+    if (platform_supports_app_type_cfg())
+        platform_backend_app_type_cfg_init(my_data, adev->mixer);
+
     return my_data;
 
 init_failed:
@@ -1446,6 +1542,7 @@
     int32_t dev;
     struct operator_info *info_item;
     struct operator_specific_device *device_item;
+    struct app_type_entry *ap;
     struct listnode *node;
 
     struct platform_data *my_data = (struct platform_data *)platform;
@@ -1483,6 +1580,13 @@
         free(info_item);
     }
 
+    while (!list_empty(&app_type_entry_list)) {
+        node = list_head(&app_type_entry_list);
+        list_remove(node);
+        ap = node_to_item(node, struct app_type_entry, node);
+        free(ap);
+    }
+
     mixer_close(my_data->adev->mixer);
     free(platform);
 
@@ -1668,13 +1772,6 @@
     return ret;
 }
 
-int platform_get_default_app_type_v2(void *platform __unused, 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)
 {
     if ((snd_device < SND_DEVICE_MIN) || (snd_device >= SND_DEVICE_MAX)) {
@@ -1725,6 +1822,9 @@
     struct platform_data *my_data = (struct platform_data *)platform;
     int acdb_dev_id, acdb_dev_type;
 
+    if (platform_supports_app_type_cfg()) // use v2 instead
+        return -ENOSYS;
+
     acdb_dev_id = acdb_device_table[audio_extn_get_spkr_prot_snd_device(snd_device)];
     if (acdb_dev_id < 0) {
         ALOGE("%s: Could not find acdb id for device(%d)",
@@ -1744,6 +1844,63 @@
     return 0;
 }
 
+int platform_send_audio_calibration_v2(void *platform, struct audio_usecase *usecase,
+                                       int app_type, int sample_rate)
+{
+    struct platform_data *my_data = (struct platform_data *)platform;
+    int acdb_dev_id, acdb_dev_type;
+    int snd_device = SND_DEVICE_OUT_SPEAKER;
+    int new_snd_device[SND_DEVICE_OUT_END] = {0};
+    int i, num_devices = 1;
+
+    if (!platform_supports_app_type_cfg()) // use v1 instead
+        return -ENOSYS;
+
+    if (usecase->type == PCM_PLAYBACK)
+        snd_device = usecase->out_snd_device;
+    else if (usecase->type == PCM_CAPTURE)
+        snd_device = usecase->in_snd_device;
+
+    // skipped over get_spkr_prot_device
+    acdb_dev_id = acdb_device_table[snd_device];
+    if (acdb_dev_id < 0) {
+        ALOGE("%s: Could not find acdb id for device(%d)",
+              __func__, snd_device);
+        return -EINVAL;
+    }
+
+    if (platform_can_split_snd_device(snd_device,
+                                      &num_devices, new_snd_device) < 0) {
+        new_snd_device[0] = snd_device;
+    }
+
+    for (i = 0; i < num_devices; i++) {
+        acdb_dev_id = acdb_device_table[new_snd_device[i]];
+        if (acdb_dev_id < 0) {
+            ALOGE("%s: Could not find acdb id for device(%d)",
+                  __func__, new_snd_device[i]);
+            return -EINVAL;
+        }
+        ALOGV("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
+              __func__, new_snd_device[i], acdb_dev_id);
+        if (new_snd_device[i] >= SND_DEVICE_OUT_BEGIN &&
+                new_snd_device[i] < 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) {
+            my_data->acdb_send_audio_cal_v3(acdb_dev_id, acdb_dev_type,
+                                            app_type, sample_rate, i);
+        } else if (my_data->acdb_send_audio_cal) {
+            my_data->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type); // this version differs from internal
+        }
+    }
+
+    return 0;
+}
+
+
 int platform_switch_voice_call_device_pre(void *platform)
 {
     struct platform_data *my_data = (struct platform_data *)platform;
@@ -3361,12 +3518,12 @@
     return backend_change;
 }
 
-static void platform_pick_playback_cfg_for_uc(struct audio_device *adev,
-                                              struct audio_usecase *usecase,
-                                              snd_device_t snd_device,
-                                              unsigned int *bit_width,
-                                              unsigned int *sample_rate,
-                                              unsigned int *channels)
+static void pick_playback_cfg_for_uc(struct audio_device *adev,
+                                     struct audio_usecase *usecase,
+                                     snd_device_t snd_device,
+                                     unsigned int *bit_width,
+                                     unsigned int *sample_rate,
+                                     unsigned int *channels)
 {
     int i =0;
     struct listnode *node;
@@ -3397,6 +3554,27 @@
     return;
 }
 
+static void headset_is_config_supported(unsigned int *bit_width,
+                                        unsigned int *sample_rate,
+                                        unsigned int *channels) {
+    switch (*bit_width) {
+        case 16:
+        case 24:
+            break;
+        default:
+            *bit_width = 16;
+            break;
+    }
+
+    if (*sample_rate > 192000) {
+        *sample_rate = 192000;
+    }
+
+    if (*channels > 2) {
+        *channels = 2;
+    }
+}
+
 static bool platform_check_playback_backend_cfg(struct audio_device* adev,
                                              struct audio_usecase* usecase,
                                              snd_device_t snd_device,
@@ -3406,10 +3584,8 @@
     unsigned int bit_width;
     unsigned int sample_rate;
     unsigned int channels;
-    bool passthrough_enabled = false;
     int backend_idx = DEFAULT_CODEC_BACKEND;
     struct platform_data *my_data = (struct platform_data *)adev->platform;
-    bool channels_updated = false;
 
     if (snd_device == SND_DEVICE_OUT_BT_SCO ||
         snd_device == SND_DEVICE_OUT_BT_SCO_WB) {
@@ -3443,10 +3619,10 @@
          *
          * Exception: 16 bit playbacks is allowed through 16 bit/48/44.1 khz backend only
          */
-        platform_pick_playback_cfg_for_uc(adev, usecase, snd_device,
-                                          &bit_width,
-                                          &sample_rate,
-                                          &channels);
+        pick_playback_cfg_for_uc(adev, usecase, snd_device,
+                                 &bit_width,
+                                 &sample_rate,
+                                 &channels);
     }
 
     switch (backend_idx) {
@@ -3456,8 +3632,10 @@
             ALOGV("%s: USB BE configured as bit_width(%d)sample_rate(%d)channels(%d)",
                   __func__, bit_width, sample_rate, channels);
             break;
-        case DEFAULT_CODEC_BACKEND:
         case HEADPHONE_BACKEND:
+            headset_is_config_supported(&bit_width, &sample_rate, &channels);
+            break;
+        case DEFAULT_CODEC_BACKEND:
         default:
             bit_width = platform_get_snd_device_bit_width(snd_device);
             sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
@@ -3465,23 +3643,19 @@
             break;
     }
 
-    if (channels != my_data->current_backend_cfg[backend_idx].channels) {
-        channels_updated = true;
-    }
-
     ALOGV("%s:becf: afe: Codec selected backend: %d updated bit width: %d and"
           "sample rate: %d",
           __func__, backend_idx , bit_width, sample_rate);
 
     // Force routing if the expected bitwdith or samplerate
     // is not same as current backend comfiguration
-    if ((bit_width != my_data->current_backend_cfg[backend_idx].bit_width) ||
-        (sample_rate != my_data->current_backend_cfg[backend_idx].sample_rate) ||
-        passthrough_enabled || channels_updated) {
+    if (bit_width != my_data->current_backend_cfg[backend_idx].bit_width ||
+        sample_rate != my_data->current_backend_cfg[backend_idx].sample_rate ||
+        channels != my_data->current_backend_cfg[backend_idx].channels) {
         backend_cfg->bit_width = bit_width;
         backend_cfg->sample_rate = sample_rate;
         backend_cfg->channels = channels;
-        backend_cfg->passthrough_enabled = passthrough_enabled;
+        backend_cfg->passthrough_enabled = false;
         backend_change = true;
         ALOGV("%s:becf: afe: Codec backend needs to be updated. new bit width: %d"
               "new sample rate: %d new channels: %d",
@@ -3566,3 +3740,237 @@
     return false;
 }
 
+static int max_be_dai_names = 0;
+static const struct be_dai_name_struct *be_dai_name_table;
+
+/*
+ * Retrieves the be_dai_name_table from kernel to enable a mapping
+ * between sound device hw interfaces and backend IDs. This allows HAL to
+ * specify the backend a specific calibration is needed for.
+ */
+static int init_be_dai_name_table(struct audio_device *adev)
+{
+    const char *mixer_ctl_name = "Backend DAI Name Table";
+    struct mixer_ctl *ctl;
+    int i, j, ret, size;
+    bool valid_hw_interface;
+
+    ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
+    if (!ctl) {
+        ALOGE("%s: Could not get ctl for mixer name %s\n",
+               __func__, mixer_ctl_name);
+        ret = -EINVAL;
+        goto done;
+    }
+
+    mixer_ctl_update(ctl);
+
+    size = mixer_ctl_get_num_values(ctl);
+    if (size <= 0){
+        ALOGE("%s: Failed to get %s size %d\n",
+               __func__, mixer_ctl_name, size);
+        ret = -EFAULT;
+        goto done;
+    }
+
+    posix_memalign((void **)&be_dai_name_table, 32, size);
+    if (be_dai_name_table == NULL) {
+        ALOGE("%s: Failed to allocate memory for %s\n",
+               __func__, mixer_ctl_name);
+        ret = -ENOMEM;
+        goto freeMem;
+    }
+
+    ret = mixer_ctl_get_array(ctl, (void *)be_dai_name_table, size);
+    if (ret) {
+        ALOGE("%s: Failed to get %s, ret %d\n",
+               __func__, mixer_ctl_name, ret);
+        ret = -EFAULT;
+        goto freeMem;
+    }
+
+    if (be_dai_name_table != NULL) {
+        max_be_dai_names = size / sizeof(struct be_dai_name_struct);
+        ALOGV("%s: Successfully got %s, number of be dais is %d\n",
+              __func__, mixer_ctl_name, max_be_dai_names);
+        ret = 0;
+    } else {
+        ALOGE("%s: Failed to get %s\n", __func__, mixer_ctl_name);
+        ret = -EFAULT;
+        goto freeMem;
+    }
+
+    /*
+     * Validate all sound devices have a valid backend set to catch
+     * errors for uncommon sound devices
+     */
+    for (i = 0; i < SND_DEVICE_MAX; i++) {
+        valid_hw_interface = false;
+
+        if (hw_interface_table[i] == NULL) {
+            ALOGW("%s: sound device %s has no hw interface set\n",
+                  __func__, platform_get_snd_device_name(i));
+            continue;
+        }
+
+        for (j = 0; j < max_be_dai_names; j++) {
+            if (strcmp(hw_interface_table[i], be_dai_name_table[j].be_name)
+                == 0) {
+                valid_hw_interface = true;
+                break;
+            }
+        }
+        if (!valid_hw_interface)
+            ALOGD("%s: sound device %s does not have a valid hw interface set "
+                  "(disregard for combo devices) %s\n",
+                  __func__, platform_get_snd_device_name(i),
+                  hw_interface_table[i]);
+    }
+
+    goto done;
+
+freeMem:
+    if (be_dai_name_table) {
+        free((void *)be_dai_name_table);
+        be_dai_name_table = NULL;
+    }
+
+done:
+    return ret;
+}
+
+int platform_get_snd_device_backend_index(snd_device_t device)
+{
+    int i, be_dai_id;
+    const char * hw_interface_name = NULL;
+
+    ALOGV("%s: enter with device %d\n", __func__, device);
+
+    if ((device <= SND_DEVICE_MIN) || (device >= SND_DEVICE_MAX)) {
+        ALOGE("%s: Invalid snd_device = %d",
+              __func__, device);
+        be_dai_id = -EINVAL;
+        goto done;
+    }
+
+    /* Get string value of necessary backend for device */
+    hw_interface_name = hw_interface_table[device];
+    if (hw_interface_name == NULL) {
+        ALOGE("%s: no hw_interface set for device %d\n", __func__, device);
+        be_dai_id = -EINVAL;
+        goto done;
+    }
+
+    /* Check if be dai name table was retrieved successfully */
+    if (be_dai_name_table == NULL) {
+        ALOGE("%s: BE DAI Name Table is not present\n", __func__);
+        be_dai_id = -EFAULT;
+        goto done;
+    }
+
+    /* Get backend ID for device specified */
+    for (i = 0; i < max_be_dai_names; i++) {
+        if (strcmp(hw_interface_name, be_dai_name_table[i].be_name) == 0) {
+            be_dai_id = be_dai_name_table[i].be_id;
+            goto done;
+        }
+    }
+    ALOGE("%s: no interface matching name %s\n", __func__, hw_interface_name);
+    be_dai_id = -EINVAL;
+    goto done;
+
+done:
+    return be_dai_id;
+}
+
+void platform_check_and_update_copp_sample_rate(void* platform, snd_device_t snd_device,
+                                                unsigned int stream_sr, int* sample_rate)
+{
+    struct platform_data* my_data = (struct platform_data *)platform;
+    int backend_idx = platform_get_backend_index(snd_device);
+    int device_sr = my_data->current_backend_cfg[backend_idx].sample_rate;
+    /*
+     *Check if device SR is multiple of 8K or 11.025 Khz
+     *check if the stream SR is multiple of same base, if yes
+     *then have copp SR equal to stream SR, this ensures that
+     *post processing happens at stream SR, else have
+     *copp SR equal to device SR.
+     */
+    if (!(((sample_rate_multiple(device_sr, SAMPLE_RATE_8000)) &&
+           (sample_rate_multiple(stream_sr, SAMPLE_RATE_8000))) ||
+          ((sample_rate_multiple(device_sr, SAMPLE_RATE_11025)) &&
+           (sample_rate_multiple(stream_sr, SAMPLE_RATE_11025))))) {
+        *sample_rate = device_sr;
+    } else
+        *sample_rate = stream_sr;
+
+    ALOGI("sn_device %d device sr %d stream sr %d copp sr %d", snd_device, device_sr, stream_sr
+          , *sample_rate);
+
+}
+
+// called from info parser
+void platform_add_app_type(int bw, const char *uc_type,
+                           int app_type, int max_rate) {
+    struct app_type_entry *ap =
+            (struct app_type_entry *)calloc(1, sizeof(struct app_type_entry));
+
+    if (!ap) {
+        ALOGE("%s failed to allocate mem for app type", __func__);
+        return;
+    }
+
+    ap->uc_type = -1;
+    for (int i=0; i<USECASE_TYPE_MAX; i++) {
+        if (!strcmp(uc_type, usecase_type_index[i].name)) {
+            ap->uc_type = usecase_type_index[i].index;
+            break;
+        }
+    }
+
+    if (ap->uc_type == -1) {
+        free(ap);
+        return;
+    }
+
+    ALOGI("%s bw %d uc %s app_type %d max_rate %d",
+          __func__, bw, uc_type, app_type, max_rate);
+    ap->bit_width = bw;
+    ap->app_type = app_type;
+    ap->max_rate = max_rate;
+    list_add_tail(&app_type_entry_list, &ap->node);
+}
+
+
+int platform_get_default_app_type_v2(void *platform __unused,
+                                     usecase_type_t type,
+                                     int *app_type )
+{
+    if (type == PCM_PLAYBACK)
+        *app_type = DEFAULT_APP_TYPE_RX_PATH;
+    else
+        *app_type = DEFAULT_APP_TYPE_TX_PATH;
+    return 0;
+}
+
+int platform_get_app_type_v2(void *platform, usecase_type_t uc_type,
+                             int bw, int sr __unused,
+                             int *app_type)
+{
+    struct listnode *node;
+    struct app_type_entry *entry;
+    *app_type = -1;
+    list_for_each(node, &app_type_entry_list) {
+        entry = node_to_item(node, struct app_type_entry, node);
+        if (entry->bit_width == bw &&
+            entry->uc_type == uc_type) {
+            *app_type = entry->app_type;
+            break;
+        }
+    }
+
+    if (*app_type == -1) {
+        return platform_get_default_app_type_v2(platform, uc_type, app_type);
+    }
+    return 0;
+}
diff --git a/hal/platform_api.h b/hal/platform_api.h
index 59ad4b1..e3bcc1a 100644
--- a/hal/platform_api.h
+++ b/hal/platform_api.h
@@ -50,6 +50,8 @@
 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_send_audio_calibration_v2(void *platform, struct audio_usecase *usecase,
+                                           int app_type, int sample_rate);
 int platform_get_default_app_type_v2(void *platform, enum 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,
@@ -125,4 +127,11 @@
                    struct audio_usecase *usecase, snd_device_t snd_device);
 
 int platform_snd_card_update(void *platform, enum card_status_t status);
+void platform_check_and_update_copp_sample_rate(void *platform, snd_device_t snd_device,
+     unsigned int stream_sr,int *sample_rate);
+int platform_get_snd_device_backend_index(snd_device_t snd_device);
+bool platform_supports_app_type_cfg();
+int platform_get_app_type_v2(void *platform, enum usecase_type_t type,
+                             int bw, int sr, int *app_type);
+void platform_add_app_type(int bw, const char *uc_type, int app_type, int max_sr);
 #endif // AUDIO_PLATFORM_API_H
diff --git a/hal/platform_info.c b/hal/platform_info.c
index f6b57e3..952fc68 100644
--- a/hal/platform_info.c
+++ b/hal/platform_info.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2014 The Android Open Source Project
+ * Copyright (C) 2014-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.
@@ -34,6 +34,7 @@
     CONFIG_PARAMS,
     OPERATOR_SPECIFIC,
     GAIN_LEVEL_MAPPING,
+    APP_TYPE,
 } section_t;
 
 typedef void (* section_process_fn)(const XML_Char **attr);
@@ -45,6 +46,7 @@
 static void process_root(const XML_Char **attr);
 static void process_operator_specific(const XML_Char **attr);
 static void process_gain_db_to_level_map(const XML_Char **attr);
+static void process_app_type(const XML_Char **attr);
 
 static section_process_fn section_table[] = {
     [ROOT] = process_root,
@@ -54,6 +56,7 @@
     [CONFIG_PARAMS] = process_config_params,
     [OPERATOR_SPECIFIC] = process_operator_specific,
     [GAIN_LEVEL_MAPPING] = process_gain_db_to_level_map,
+    [APP_TYPE] = process_app_type,
 };
 
 static section_t section;
@@ -307,6 +310,33 @@
     return;
 }
 
+static void process_app_type(const XML_Char **attr)
+{
+    if (strcmp(attr[0], "uc_type")) {
+        ALOGE("%s: uc_type not found", __func__);
+        goto done;
+    }
+
+    if (strcmp(attr[2], "bit_width")) {
+        ALOGE("%s: bit_width not found", __func__);
+        goto done;
+    }
+
+    if (strcmp(attr[4], "id")) {
+        ALOGE("%s: id not found", __func__);
+        goto done;
+    }
+
+    if (strcmp(attr[6], "max_rate")) {
+        ALOGE("%s: max rate not found", __func__);
+        goto done;
+    }
+
+    platform_add_app_type(atoi(attr[3]), attr[1], atoi(attr[5]), atoi(attr[7]));
+done:
+    return;
+}
+
 static void start_tag(void *userdata __unused, const XML_Char *tag_name,
                       const XML_Char **attr)
 {
@@ -326,6 +356,8 @@
         section = OPERATOR_SPECIFIC;
     } else if (strcmp(tag_name, "gain_db_to_level_mapping") == 0) {
         section = GAIN_LEVEL_MAPPING;
+    } else if (strcmp(tag_name, "app_types") == 0) {
+        section = APP_TYPE;
     } else if (strcmp(tag_name, "device") == 0) {
         if ((section != ACDB) && (section != BACKEND_NAME) && (section != OPERATOR_SPECIFIC)) {
             ALOGE("device tag only supported for acdb/backend names");
@@ -359,6 +391,14 @@
 
         section_process_fn fn = section_table[GAIN_LEVEL_MAPPING];
         fn(attr);
+    } else if (!strcmp(tag_name, "app")) {
+        if (section != APP_TYPE) {
+            ALOGE("app tag only valid in section APP_TYPE");
+            return;
+        }
+
+        section_process_fn fn = section_table[APP_TYPE];
+        fn(attr);
     }
 
     return;
@@ -378,6 +418,8 @@
         section = ROOT;
     } else if (strcmp(tag_name, "gain_db_to_level_mapping") == 0) {
         section = ROOT;
+    } else if (strcmp(tag_name, "app_types") == 0) {
+        section = ROOT;
     }
 }