hal: Add support for feedback speaker protection

Cherry-picked and squashed CAF commits:
f538cefc0747e6bc2cff296ad295f6a37e17f7a5
e2761eb87903d958e19989875595a642cc7d7ada
924e13ddd76dcb9a7dd693b7af60db27cdb5a1df
28e9ed3b2fb68ecbfdae00ce387e7942ae37940b
cc9bb1477583269215330cb7aeaebe4f808c03b3
e85d046e57863f55a61f6e445b371e8b55907bad
cc96e41e5cd7a1a8a9662f5d844c6e3867bd7573
229659c499ddff858cbcc4336cb035335e38bd2c

Change-Id: Id56ff10214eaed343299cc3dd8649d0621c27c45
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 55f5c19..20763ac 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -158,6 +158,9 @@
     [USECASE_QCHAT_CALL] = "qchat-call",
     [USECASE_VOWLAN_CALL] = "vowlan-call",
 
+    [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
+    [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
+
     [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback",
     [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record",
 };
@@ -279,6 +282,9 @@
     audio_extn_sound_trigger_update_device_status(snd_device,
                                     ST_EVENT_SND_DEVICE_BUSY);
 
+    if (audio_extn_spkr_prot_is_enabled())
+         audio_extn_spkr_prot_calib_cancel(adev);
+
     if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
         adev->snd_dev_ref_cnt[snd_device]--;
         audio_extn_sound_trigger_update_device_status(snd_device,
@@ -286,9 +292,22 @@
         return -EINVAL;
     }
 
-    const char * dev_path = platform_get_snd_device_name(snd_device);
-    ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, dev_path);
-    audio_route_apply_and_update_path(adev->audio_route, dev_path);
+    if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
+        snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
+        audio_extn_spkr_prot_is_enabled()) {
+        if (audio_extn_spkr_prot_get_acdb_id(snd_device) < 0) {
+            adev->snd_dev_ref_cnt[snd_device]--;
+            return -EINVAL;
+        }
+        if (audio_extn_spkr_prot_start_processing(snd_device)) {
+            ALOGE("%s: spkr_start_processing failed", __func__);
+            return -EINVAL;
+        }
+    } else {
+        const char * dev_path = platform_get_snd_device_name(snd_device);
+        ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, dev_path);
+        audio_route_apply_and_update_path(adev->audio_route, dev_path);
+    }
 
     return 0;
 }
@@ -309,10 +328,15 @@
     if (adev->snd_dev_ref_cnt[snd_device] == 0) {
         const char * dev_path = platform_get_snd_device_name(snd_device);
         ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, dev_path);
-        audio_route_reset_and_update_path(adev->audio_route, dev_path);
+        if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
+            snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
+            audio_extn_spkr_prot_is_enabled()) {
+            audio_extn_spkr_prot_stop_processing(snd_device);
+        } else {
+            audio_route_reset_and_update_path(adev->audio_route, dev_path);
+        }
         audio_extn_sound_trigger_update_device_status(snd_device,
                                         ST_EVENT_SND_DEVICE_FREE);
-
     }
     return 0;
 }