qahw: Load sound trigger HAL when concurrency enabled

Load sound trigger HAL during audio use case is running when sva and audio
concurrency is enabled.

CRs-Fixed: 2173226
Change-Id: I80b797857eae9dfc2cfaad340ec145dccd3c095f
diff --git a/qahw/Makefile.am b/qahw/Makefile.am
index 1375157..b78c9b7 100644
--- a/qahw/Makefile.am
+++ b/qahw/Makefile.am
@@ -13,6 +13,10 @@
 libqahwwrapper_la_SOURCES = src/qahw.c \
                      src/qahw_effect.c
 
+if SVA_AUDIO_CONCURRENCY
+AM_CFLAGS += -DSVA_AUDIO_CONC
+endif
+
 libqahwwrapper_la_CFLAGS  = $(AM_CFLAGS)
 libqahwwrapper_la_CFLAGS += -include stddef.h
 libqahwwrapper_la_CFLAGS += -Dstrlcpy=g_strlcpy $(GLIB_CFLAGS) -include glib.h
diff --git a/qahw/configure.ac b/qahw/configure.ac
index 04fde19..5bd3a22 100644
--- a/qahw/configure.ac
+++ b/qahw/configure.ac
@@ -29,6 +29,7 @@
 AC_PROG_MAKE_SET
 PKG_PROG_PKG_CONFIG
 
+AM_CONDITIONAL([SVA_AUDIO_CONCURRENCY],[test x$BOARD_SUPPORTS_SVA_AUDIO_CONCURRENCY = xtrue])
 AC_ARG_WITH([glib],
       AC_HELP_STRING([--with-glib],
          [enable glib, Build against glib. Use this when building for HLOS systems which use glib]))
diff --git a/qahw/src/qahw.c b/qahw/src/qahw.c
index 91fc5bf..d03a570 100644
--- a/qahw/src/qahw.c
+++ b/qahw/src/qahw.c
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
+* Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
@@ -37,6 +37,7 @@
 #include <cutils/list.h>
 
 #include <hardware/audio.h>
+#include <hardware/sound_trigger.h>
 #include "qahw.h"
 
 #define NO_ERROR 0
@@ -113,6 +114,7 @@
 static struct listnode qahw_module_list;
 static int qahw_list_count;
 static pthread_mutex_t qahw_module_init_lock = PTHREAD_MUTEX_INITIALIZER;
+sound_trigger_hw_device_t *st_hw_device = NULL;
 
 
 /** Start of internal functions */
@@ -1776,6 +1778,45 @@
     return QAHW_MODULE_API_VERSION_CURRENT;
 }
 
+/* Load AHAL module to run audio and sva concurrency */
+static void load_st_hal()
+{
+#ifdef SVA_AUDIO_CONC
+    int rc = -EINVAL;
+    const hw_module_t* module = NULL;
+
+    rc = hw_get_module_by_class(SOUND_TRIGGER_HARDWARE_MODULE_ID, "primary", &module);
+    if (rc) {
+        ALOGE("%s: AHAL Loading failed %d", __func__, rc);
+        goto error;
+    }
+
+    rc = sound_trigger_hw_device_open(module, &st_hw_device);
+    if (rc) {
+        ALOGE("%s: AHAL Device open failed %d", __func__, rc);
+        st_hw_device = NULL;
+    }
+error:
+    return;
+#else
+    return;
+#endif /*SVA_AUDIO_CONC*/
+}
+
+static void unload_st_hal()
+{
+#ifdef SVA_AUDIO_CONC
+    if (st_hw_device == NULL) {
+        ALOGE("%s: audio device is NULL",__func__);
+        return;
+    }
+    sound_trigger_hw_device_close(st_hw_device);
+    st_hw_device = NULL;
+#else
+    return;
+#endif /*SVA_AUDIO_CONC*/
+}
+
 /* convenience API for opening and closing an audio HAL module */
 
 qahw_module_handle_t *qahw_load_module_l(const char *hw_module_id)
@@ -1867,7 +1908,7 @@
 
     /* Add module list to global module list */
     list_add_tail(&qahw_module_list, &qahw_module->module_list);
-
+    load_st_hal();
 
 error_exit:
     pthread_mutex_unlock(&qahw_module_init_lock);
@@ -1924,6 +1965,7 @@
                "is not closed", __func__);
         rc = -EINVAL;
     }
+    unload_st_hal();
 
 error_exit:
     pthread_mutex_unlock(&qahw_module_init_lock);