hal: Add changes to send pspd coefficients during out_write

Add support to send matrix mixer coefficients in out_write
as older kernel version does not have support to save the
channel weightage values.
Add flag to check and resend matrix mixer coefficients during
device switch.

Change-Id: Ifc874540a9ecfc29243114f546fd9c25323f3be1
diff --git a/hal/audio_extn/audio_extn.c b/hal/audio_extn/audio_extn.c
index 7945168..1d4bf2a 100644
--- a/hal/audio_extn/audio_extn.c
+++ b/hal/audio_extn/audio_extn.c
@@ -393,27 +393,56 @@
     char mixer_ctl_name[128] = {0};
     struct audio_custom_mtmx_params_info *pinfo = &params->info;
     int i = 0, err = 0;
+    int cust_ch_mixer_cfg[128], len = 0;
 
     ALOGI("%s: ip_channels %d, op_channels %d, pcm_device_id %d",
           __func__, pinfo->ip_channels, pinfo->op_channels, pcm_device_id);
 
-    for (i = 0; i < (int)pinfo->op_channels; i++) {
-         snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "%s %d %s %d",
-                  mixer_name_prefix, pcm_device_id, mixer_name_suffix, i+1);
-         ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
-         if (!ctl) {
-             ALOGE("%s: ERROR. Could not get ctl for mixer cmd - %s",
-                   __func__, mixer_ctl_name);
-             return -EINVAL;
-         }
+    if (adev->use_old_pspd_mix_ctrl) {
+        /*
+         * Below code is to ensure backward compatibilty with older
+         * kernel version. Use old mixer control to set mixer coefficients
+         */
+        snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
+         "Audio Stream %d Channel Mix Cfg", pcm_device_id);
 
-         err = mixer_ctl_set_array(ctl,
-                                   &params->coeffs[pinfo->ip_channels * i],
-                                   pinfo->ip_channels);
-         if (err) {
-             ALOGE("%s: ERROR. Mixer ctl set failed", __func__);
-             return -EINVAL;
-         }
+        ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
+        if (!ctl) {
+            ALOGE("%s: ERROR. Could not get ctl for mixer cmd - %s",
+                  __func__, mixer_ctl_name);
+            return -EINVAL;
+        }
+        cust_ch_mixer_cfg[len++] = pinfo->ip_channels;
+        cust_ch_mixer_cfg[len++] = pinfo->op_channels;
+        for (i = 0; i < (int) (pinfo->op_channels * pinfo->ip_channels); i++) {
+            ALOGV("%s: coeff[%d] %d", __func__, i, params->coeffs[i]);
+            cust_ch_mixer_cfg[len++] = params->coeffs[i];
+        }
+        err = mixer_ctl_set_array(ctl, cust_ch_mixer_cfg, len);
+        if (err) {
+            ALOGE("%s: ERROR. Mixer ctl set failed", __func__);
+            return -EINVAL;
+        }
+        ALOGD("%s: Mixer ctl set for %s success", __func__, mixer_ctl_name);
+    } else {
+        for (i = 0; i < (int)pinfo->op_channels; i++) {
+            snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "%s %d %s %d",
+                    mixer_name_prefix, pcm_device_id, mixer_name_suffix, i+1);
+
+            ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
+            if (!ctl) {
+                ALOGE("%s: ERROR. Could not get ctl for mixer cmd - %s",
+                      __func__, mixer_ctl_name);
+                 return -EINVAL;
+            }
+            err = mixer_ctl_set_array(ctl,
+                                      &params->coeffs[pinfo->ip_channels * i],
+                                      pinfo->ip_channels);
+            if (err) {
+                ALOGE("%s: ERROR. Mixer ctl set failed", __func__);
+                return -EINVAL;
+            }
+        }
     }
     return 0;
 }
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 8f27e86..1638a22 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -1042,6 +1042,8 @@
     audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_FREE);
     audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_FREE);
     audio_extn_set_custom_mtmx_params(adev, usecase, false);
+    if (usecase->stream.out != NULL)
+        usecase->stream.out->pspd_coeff_sent = false;
     ALOGV("%s: exit", __func__);
     return 0;
 }
@@ -4592,6 +4594,7 @@
     int channels = 0;
     const size_t frame_size = audio_stream_out_frame_size(stream);
     const size_t frames = (frame_size != 0) ? bytes / frame_size : bytes;
+    struct audio_usecase *usecase = NULL;
 
     ATRACE_BEGIN("out_write");
     lock_output_stream(out);
@@ -4713,6 +4716,20 @@
         adev->is_channel_status_set = true;
     }
 
+    if ((adev->use_old_pspd_mix_ctrl == true) &&
+        (out->pspd_coeff_sent == false)) {
+        /*
+         * Need to resend pspd coefficients after stream started for
+         * older kernel version as it does not save the coefficients
+         * and also stream has to be started for coeff to apply.
+         */
+        usecase = get_usecase_from_list(adev, out->usecase);
+        if (usecase != NULL) {
+            audio_extn_set_custom_mtmx_params(adev, usecase, true);
+            out->pspd_coeff_sent = true;
+        }
+    }
+
     if (is_offload_usecase(out->usecase)) {
         ALOGVV("copl(%p): writing buffer (%zu bytes) to compress device", out, bytes);
         if (out->send_new_metadata) {
@@ -6117,6 +6134,7 @@
     out->dynamic_pm_qos_config_supported = 0;
     out->set_dual_mono = false;
     out->prev_card_status_offline = false;
+    out->pspd_coeff_sent = false;
 
     if ((flags & AUDIO_OUTPUT_FLAG_BD) &&
         (property_get_bool("vendor.audio.matrix.limiter.enable", false)))
@@ -7824,6 +7842,8 @@
                      hw_device_t **device)
 {
     int ret;
+    char mixer_ctl_name[128] = {0};
+    struct mixer_ctl *ctl = NULL;
 
     ALOGD("%s: enter", __func__);
     if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
@@ -7899,6 +7919,7 @@
     adev->perf_lock_opts[1] = 0x20E;
     adev->perf_lock_opts_size = 2;
     adev->dsp_bit_width_enforce_mode = 0;
+    adev->use_old_pspd_mix_ctrl = false;
 
     /* Loads platform specific libraries dynamically */
     adev->platform = platform_init(adev);
@@ -8086,6 +8107,22 @@
     if (adev->device_cfg_params == NULL)
         ALOGE("%s: Memory allocation failed for Device config params", __func__);
 
+    /*
+     * Check if new PSPD matrix mixer control is supported. If not
+     * supported, then set flag so that old mixer ctrl is sent while
+     * sending pspd coefficients on older kernel version. Query mixer
+     * control for default pcm id and channel value one.
+     */
+    snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
+            "AudStr %d ChMixer Weight Ch %d", 0, 1);
+
+    ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
+    if (!ctl) {
+        ALOGE("%s: ERROR. Could not get ctl for mixer cmd - %s",
+              __func__, mixer_ctl_name);
+        adev->use_old_pspd_mix_ctrl = true;
+    }
+
     ALOGV("%s: exit", __func__);
     return 0;
 }
diff --git a/hal/audio_hw.h b/hal/audio_hw.h
old mode 100755
new mode 100644
index 003690a..9e2b6fc
--- a/hal/audio_hw.h
+++ b/hal/audio_hw.h
@@ -356,6 +356,7 @@
     mix_matrix_params_t downmix_params;
     bool set_dual_mono;
     bool prev_card_status_offline;
+    bool pspd_coeff_sent;
 };
 
 struct stream_in {
@@ -539,6 +540,7 @@
     unsigned int interactive_usecase_state;
     bool dp_allowed_for_voice;
     void *ext_hw_plugin;
+    bool use_old_pspd_mix_ctrl;
 };
 
 int select_devices(struct audio_device *adev,
diff --git a/hal/msm8974/platform.c b/hal/msm8974/platform.c
index 72a1b41..8f2ed07 100644
--- a/hal/msm8974/platform.c
+++ b/hal/msm8974/platform.c
@@ -2544,6 +2544,8 @@
         strdup("SLIM_0_RX Format");
     my_data->current_backend_cfg[DEFAULT_CODEC_BACKEND].samplerate_mixer_ctl =
         strdup("SLIM_0_RX SampleRate");
+    my_data->current_backend_cfg[DEFAULT_CODEC_BACKEND].channels_mixer_ctl =
+        strdup("SLIM_0_RX Channels");
 
     my_data->current_backend_cfg[DSD_NATIVE_BACKEND].bitwidth_mixer_ctl =
         strdup("SLIM_2_RX Format");