sthal: fix race condition of amp-ref route

note: Race condition happend during STREAM_INACTIVE and
      DEVICE_INACTIVE, Application can start_reg during
      this period. That will cause 48K amp-ref route
      doesn't disable before enable 16K mic route.
      That causes crash.

Bug:138081640
Test: manually test microphone concurrency and bargein scenario
Change-Id: Iaca6de7b1847b9b40a65ddab6398b19489a84da1
diff --git a/sound_trigger_hw_iaxxx.c b/sound_trigger_hw_iaxxx.c
index 2f12383..268f14d 100644
--- a/sound_trigger_hw_iaxxx.c
+++ b/sound_trigger_hw_iaxxx.c
@@ -1382,6 +1382,12 @@
                         ALOGE("Failed to disable mic route with EXT OSC");
                         goto exit;
                     }
+
+                    ret = enable_amp_ref_route(stdev->route_hdl, true, STRM_48K);
+                    if (ret != 0) {
+                        ALOGE("Failed to enable amp-ref route");
+                        goto exit;
+                    }
                 } else {
                     ret = enable_mic_route(stdev->route_hdl, false,
                                         INTERNAL_OSCILLATOR);
@@ -1438,6 +1444,11 @@
             // reconfig mic
             if (stdev->is_mic_route_enabled == true) {
                 if (stdev->is_bargein_route_enabled == true) {
+                    ret = enable_amp_ref_route(stdev->route_hdl, false, STRM_48K);
+                    if (ret != 0) {
+                        ALOGE("Failed to disable amp-ref route");
+                        goto exit;
+                    }
                     ret = enable_mic_route(stdev->route_hdl, true,
                                         EXTERNAL_OSCILLATOR);
                     if (ret != 0) {
@@ -3552,15 +3563,6 @@
         else if (config->u.usecase.type == USECASE_TYPE_PCM_CAPTURE)
             stdev->is_media_recording = false;
 
-        // turn off amp-ref with 48khz before turning off main mic by media recording
-        if (is_mic_controlled_by_audhal(stdev) == true &&
-            stdev->is_bargein_route_enabled == true) {
-            ret = enable_amp_ref_route(stdev->route_hdl, false, STRM_48K);
-            if (ret != 0) {
-                ALOGE("Failed to disable amp-ref route");
-                goto exit;
-            }
-        }
         break;
     case AUDIO_EVENT_CAPTURE_DEVICE_ACTIVE:
         /*
@@ -3599,15 +3601,6 @@
         ALOGD("%s: handle capture stream active event %d, usecase :%d",
               __func__, event, config->u.usecase.type);
 
-        // turn on amp-ref with 48khz after turning on main mic by media recording
-        if (is_mic_controlled_by_audhal(stdev) == true &&
-            stdev->is_bargein_route_enabled == true) {
-            ret = enable_amp_ref_route(stdev->route_hdl, true, STRM_48K);
-            if (ret != 0) {
-                ALOGE("Failed to enable amp-ref route");
-                goto exit;
-            }
-        }
         break;
     case AUDIO_EVENT_PLAYBACK_STREAM_INACTIVE:
         ALOGD("%s: handle playback stream inactive", __func__);