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
(cherry picked from commit fb4d7bd389fc087ed36712cbd585367e7e6d0fad)
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) {