hal: retry failed MBDRC cal level

- in the start of the playback since usecase list is empty
  or not populated with the current usecase (which will be added
  on the first write) unless volume is changed when playing music
  correct gain level is not selected
- cache gain level on failure and trigger set gain level when a
  new usecase is added

Bug 30503798 29933450

Change-Id: I3d199a2810d34a693a099e3cf4b635d5b156a317
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index a96a509..35b726e 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -233,6 +233,8 @@
 static struct audio_device *adev = NULL;
 static pthread_mutex_t adev_init_lock;
 static unsigned int audio_device_ref_count;
+//cache last MBDRC cal step level
+static int last_known_cal_step = -1 ;
 
 static bool may_use_noirq_mode(struct audio_device *adev, audio_usecase_t uc_id,
                                int flags __unused)
@@ -380,6 +382,13 @@
         pthread_mutex_lock(&adev->lock);
         ret_val = platform_send_gain_dep_cal(adev->platform, level);
         pthread_mutex_unlock(&adev->lock);
+
+        // if cal set fails, cache level info
+        // if cal set succeds, reset known last cal set
+        if (!ret_val)
+            last_known_cal_step = level;
+        else if (last_known_cal_step != -1)
+            last_known_cal_step = -1;
     } else {
         ALOGE("%s: %s is NULL", __func__, adev == NULL ? "adev" : "adev->platform");
     }
@@ -1995,6 +2004,11 @@
             out->standby = true;
             goto exit;
         }
+
+        if (last_known_cal_step != -1) {
+            ALOGD("%s: retry previous failed cal level set", __func__);
+            audio_hw_send_gain_dep_calibration(last_known_cal_step);
+        }
     }
 
     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
diff --git a/hal/msm8916/platform.c b/hal/msm8916/platform.c
index 683723f..52e9fd8 100644
--- a/hal/msm8916/platform.c
+++ b/hal/msm8916/platform.c
@@ -599,7 +599,7 @@
 
 bool platform_send_gain_dep_cal(void *platform __unused, int level __unused)
 {
-    return 0;
+    return true;
 }
 
 void platform_set_echo_reference(struct audio_device *adev, bool enable,
diff --git a/hal/msm8960/platform.c b/hal/msm8960/platform.c
index e11db63..d7fc84c 100644
--- a/hal/msm8960/platform.c
+++ b/hal/msm8960/platform.c
@@ -1072,7 +1072,7 @@
 bool platform_send_gain_dep_cal(void *platform __unused,
                                 int level __unused)
 {
-    return 0;
+    return true;
 }
 
 bool platform_can_split_snd_device(snd_device_t in_snd_device __unused,
diff --git a/post_proc/volume_listener.c b/post_proc/volume_listener.c
index 897dd8f..3367b76 100644
--- a/post_proc/volume_listener.c
+++ b/post_proc/volume_listener.c
@@ -46,6 +46,7 @@
 
 #define AHAL_GAIN_DEPENDENT_INTERFACE_FUNCTION "audio_hw_send_gain_dep_calibration"
 #define AHAL_GAIN_GET_MAPPING_TABLE "audio_hw_get_gain_level_mapping"
+#define DEFAULT_CAL_STEP 0
 
 enum {
     VOL_LISTENER_STATE_UNINITIALIZED,
@@ -232,7 +233,7 @@
     // 4. if new value is different than the current value then load new calibration
 
     struct listnode *node = NULL;
-    float new_vol = 0.0;
+    float new_vol = -1.0;
     int max_level = 0;
     vol_listener_context_t *context = NULL;
     if (dumping_enabled) {
@@ -261,7 +262,9 @@
 
             if (new_vol >= 1 && total_volume_cal_step > 0) { // max amplitude, use highest DRC level
                 gain_dep_cal_level = volume_curve_gain_mapping_table[total_volume_cal_step - 1].level;
-            } else if (new_vol <= 0) {
+            } else if (new_vol == -1) {
+                gain_dep_cal_level = DEFAULT_CAL_STEP;
+            } else if (new_vol == 0) {
                 gain_dep_cal_level = volume_curve_gain_mapping_table[0].level;
             } else {
                 for (max_level = 0; max_level + 1 < total_volume_cal_step; max_level++) {