Support new audio input requests when mic is already on

We want to allow new audio input requests in cases where the mic is
already running on the MCU side without having to force the microphone
to first be turned off.

Test: Manual
Bug: 303337344
Change-Id: Ia745ef305bb62cc160f8abcd5b55a6e60b45aad5
Signed-off-by: Florian Muller <[email protected]>
diff --git a/mcu_mic_codec.c b/mcu_mic_codec.c
index 2c84776..319426a 100644
--- a/mcu_mic_codec.c
+++ b/mcu_mic_codec.c
@@ -88,7 +88,7 @@
 // [iolock]: Utilized to guarantee integrity of the data
 struct mcu_mic_codec_data {
 	int mic_on;
-	int sample_rate_hz;
+	unsigned int sample_rate_hz;
 	int gain;
 	bool gain_user_requested;
 	bool hw_enabled;
@@ -207,7 +207,7 @@
 
 	mutex_lock(&codec_data->iolock);
 	ucontrol->value.integer.value[0] = codec_data->sample_rate_hz;
-	dev_info(component->dev, "%s: mcu_mic_sample_rate: %d\n",
+	dev_info(component->dev, "%s: mcu_mic_sample_rate: %u\n",
 		__func__, codec_data->sample_rate_hz);
 	mutex_unlock(&codec_data->iolock);
 
@@ -223,7 +223,7 @@
 		snd_soc_kcontrol_component(kcontrol);
 	struct mcu_mic_codec_data *codec_data =
 		snd_soc_component_get_drvdata(component);
-	int value = ucontrol->value.integer.value[0];
+	unsigned int value = ucontrol->value.integer.value[0];
 	int ret;
 
 	dev_dbg(component->dev, "%s\n", __func__);
@@ -245,7 +245,7 @@
 			return 0;
 		} else {
 			dev_err(component->dev,
-				"Invalid Sample Rate: %d. (Valid rates: %d, %d, %d Hz)\n",
+				"Invalid Sample Rate: %u. (Valid rates: %d, %d, %d Hz)\n",
 				value, MCU_PCM_RATE_8000, MCU_PCM_RATE_16000,
 				MCU_PCM_RATE_48000);
 			return -EINVAL;
@@ -253,13 +253,13 @@
 	}
 
 	ret = dmic_mcu_send_message(component->dev, DMIC_MCU_MESSAGE_SAMPLE_RATE_KHZ,
-		value / 1000);
+		(int)(value / 1000));
 	if (ret != 0)
 		return ret;
 
 	mutex_lock(&codec_data->iolock);
 	codec_data->sample_rate_hz = value;
-	dev_info(component->dev, "%s: new mcu_mic_sample_rate: %d\n",
+	dev_info(component->dev, "%s: new mcu_mic_sample_rate: %u\n",
 		__func__, codec_data->sample_rate_hz);
 	mutex_unlock(&codec_data->iolock);
 
@@ -405,7 +405,7 @@
 	// MCU after the crash.
 	mutex_lock(&codec_data->iolock);
 	codec_data->hw_enabled = value;
-	dev_info(component->dev, "%s: mcu_mc_hw_enabled: %d\n",
+	dev_info(component->dev, "%s: mcu_mic_hw_enabled: %d\n",
 		__func__, codec_data->hw_enabled);
 	mutex_unlock(&codec_data->iolock);
 
@@ -497,29 +497,35 @@
 	struct snd_soc_component *component = dai->component;
 	struct mcu_mic_codec_data *codec_data =
 		snd_soc_component_get_drvdata(component);
-	u32 sample_rate = 0;
+	unsigned int sample_rate = params_rate(params);
 	int ret = 0;
 	int conditionalized_value;
 
 	mutex_lock(&codec_data->iolock);
 
+	if (!codec_data->pending_rec && (codec_data->mic_on == DMIC_MCU_ON_ON)) {
+		// In case the mic is already running, we allow new audio input
+		// requests without having to force the mic to first be turned off.
+		dev_info(component->dev,
+			"No pending rec request but mic is already on => Proceeding");
+		codec_data->pending_rec = DMIC_MCU_ON_ON;
+	}
+
 	// Double checking there is indeed a pending recording going on
 	if (codec_data->pending_rec) {
-		sample_rate = params_rate(params);
-
 		// Double check the requested sampling rate is a valid one.
 		if (sample_rate != MCU_PCM_RATE_8000
 			&& sample_rate != MCU_PCM_RATE_16000
 			&& sample_rate != MCU_PCM_RATE_48000) {
 			dev_err(component->dev,
-				"Invalid Sample Rate: %d. (Valid rates: %d, %d, %d Hz)\n",
+				"Invalid Sample Rate: %u. (Valid rates: %d, %d, %d Hz)\n",
 				sample_rate, MCU_PCM_RATE_8000, MCU_PCM_RATE_16000,
 				MCU_PCM_RATE_48000);
 			ret = -EINVAL;
 			goto end;
 		}
 
-		dev_info(component->dev, "Sampling at: %lu\n", sample_rate);
+		dev_info(component->dev, "Sampling at: %u\n", sample_rate);
 
 		// Set MCU sampling rate with the one requested by the AP to the ADSP
 		ret = dmic_mcu_send_message(component->dev,
@@ -579,7 +585,6 @@
 	} else {
 		dev_err(component->dev, "Called without calling 'DMIC_MCU On' first");
 		ret = -EINVAL;
-		goto end;
 	}
 
 end: