hal: speaker protect: Add deinit function to clean up

This patch adds a deinit function for speaker protection module to
perform any clean ups needed. This as well implements the deinit for
Cirrus Logic speaker protection module to wait for the threads to
finish.

Bug: 111113768
Test: VtsHalAudioV4_0Target
Change-Id: I3c89f173d726e265ce64ea4a7e43b49210f8721c
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 b1c701d..1c28b6b 100644
--- a/hal/audio_extn/audio_extn.h
+++ b/hal/audio_extn/audio_extn.h
@@ -42,6 +42,7 @@
 #define audio_extn_spkr_prot_stop_processing(snd_device)     (0)
 #define audio_extn_spkr_prot_is_enabled() (false)
 #define audio_extn_get_spkr_prot_snd_device(snd_device) (snd_device)
+#define audio_extn_spkr_prot_deinit(adev)       (0)
 #else
 void audio_extn_spkr_prot_init(void *adev);
 int audio_extn_spkr_prot_start_processing(snd_device_t snd_device);
@@ -49,6 +50,8 @@
 bool audio_extn_spkr_prot_is_enabled();
 int audio_extn_get_spkr_prot_snd_device(snd_device_t snd_device);
 void audio_extn_spkr_prot_calib_cancel(void *adev);
+void audio_extn_spkr_prot_deinit(void *adev);
+
 #endif
 
 #ifndef HFP_ENABLED
diff --git a/hal/audio_extn/cirrus_playback.c b/hal/audio_extn/cirrus_playback.c
index 3a7afa7..25fe258 100644
--- a/hal/audio_extn/cirrus_playback.c
+++ b/hal/audio_extn/cirrus_playback.c
@@ -190,6 +190,18 @@
 #endif
 }
 
+void audio_extn_spkr_prot_deinit(void *adev __unused) {
+    ALOGV("%s: Entry", __func__);
+
+#ifdef ENABLE_CIRRUS_DETECTION
+    pthread_join(handle.failure_detect_thread, NULL);
+#endif
+    pthread_join(handle.calibration_thread, NULL);
+    pthread_mutex_destroy(&handle.fb_prot_mutex);
+
+    ALOGV("%s: Exit", __func__);
+}
+
 #ifdef CIRRUS_FACTORY_CALIBRATION
 static int audio_extn_cirrus_run_calibration() {
     struct audio_device *adev = handle.adev_handle;
diff --git a/hal/audio_extn/spkr_protection.c b/hal/audio_extn/spkr_protection.c
index 8c09a29..dba69a0 100644
--- a/hal/audio_extn/spkr_protection.c
+++ b/hal/audio_extn/spkr_protection.c
@@ -780,6 +780,11 @@
     }
 }
 
+void audio_extn_spkr_prot_deinit(void *adev __unused)
+{
+    ALOGV("%s: Entry", __func__);
+}
+
 int audio_extn_get_spkr_prot_snd_device(snd_device_t snd_device)
 {
     if (!handle.spkr_prot_enable)
diff --git a/hal/msm8916/platform.c b/hal/msm8916/platform.c
index d92243e..8edd999 100644
--- a/hal/msm8916/platform.c
+++ b/hal/msm8916/platform.c
@@ -1205,6 +1205,8 @@
     struct operator_specific_device *device_item;
     struct listnode *node;
 
+    audio_extn_spkr_prot_deinit(my_data->adev);
+
     hw_info_deinit(my_data->hw_info);
 
     for (dev = 0; dev < SND_DEVICE_MAX; dev++) {
diff --git a/hal/msm8974/platform.c b/hal/msm8974/platform.c
index 429291c..820b47f 100644
--- a/hal/msm8974/platform.c
+++ b/hal/msm8974/platform.c
@@ -1834,6 +1834,8 @@
     struct platform_data *my_data = (struct platform_data *)platform;
     close_csd_client(my_data->csd);
 
+    audio_extn_spkr_prot_deinit(my_data->adev);
+
     hw_info_deinit(my_data->hw_info);
 
     for (dev = 0; dev < SND_DEVICE_MAX; dev++) {