mcu_mic_codec: Save intended value for hotword status and activity stop

Test: Manual testing that the intended settings are saved even if the
send to MCU fails

Bug: 271166024

Change-Id: If3d61db2759f4ac83bd6bdb186787efac0a69c16
Signed-off-by: Cecilia Hung <ceciliahung@google.com>
diff --git a/mcu_mic_codec.c b/mcu_mic_codec.c
index 747e629..b90a791 100644
--- a/mcu_mic_codec.c
+++ b/mcu_mic_codec.c
@@ -306,17 +306,25 @@
 		codec_data->pending_rec = DMIC_MCU_ON_OFF;
 	}
 
-	ret = dmic_mcu_send_message(component->dev, DMIC_MCU_MESSAGE_MIC_ON, value);
-	if (ret != 0)
-		goto end;
-
-	codec_data->mic_on = value;
-
 	if (value == DMIC_MCU_ON_ON) {
-		// Clear Activity Stop status since DMIC_MCU_ON_ON has been applied
+		// Clear Activity Stop status since framework has sent DMIC_MCU_ON_ON
+		// regardless if the actual sent succeed or not.  Reasoning is this
+		// saved flag is only used to notify MCU if MCU crashes, and we want
+		// to use the framework's intended state in restoring MCU after the
+		// crash.
 		codec_data->activity_stop_sent = false;
 	}
 
+	ret = dmic_mcu_send_message(component->dev, DMIC_MCU_MESSAGE_MIC_ON, value);
+	if (ret != 0) {
+		dev_err(component->dev,
+			"Error sending DMIC_MCU_MESSAGE_MIC_ON: %d", ret);
+		ret = -EIO;
+		goto end;
+	}
+
+	codec_data->mic_on = value;
+
 	dev_info(component->dev, "%s: new mcu_mic_on: %d\n",
 		__func__, codec_data->mic_on);
 
@@ -351,21 +359,27 @@
 	struct mcu_mic_codec_data *codec_data =
 		snd_soc_component_get_drvdata(component);
 	int value = ucontrol->value.integer.value[0];
-	int ret;
+	int ret = 0;
 
 	dev_dbg(component->dev, "%s\n", __func__);
 
-	ret = dmic_mcu_send_message(component->dev, DMIC_MCU_MESSAGE_HOTWORD_STATUS, value);
-	if (ret != 0)
-		return ret;
-
+	// Save HW Enable Status regardless if actual send to MCU succeed or
+	// not.  Reasoning is this saved flag is only used to notify MCU if MCU
+	// crashes, and we want to use the framework's intended state in restoring
+	// 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",
 		__func__, codec_data->hw_enabled);
 	mutex_unlock(&codec_data->iolock);
 
-	return 0;
+	ret = dmic_mcu_send_message(component->dev, DMIC_MCU_MESSAGE_HOTWORD_STATUS, value);
+	if (ret != 0) {
+		dev_err(component->dev,
+			"Error sending DMIC_MCU_MESSAGE_HOTWORD_STATUS: %d", ret);
+		ret = -EIO;
+	}
+	return ret;
 }
 
 static int dmic_mcu_activity_stop_get(struct snd_kcontrol *kcontrol,
@@ -392,22 +406,27 @@
 		snd_soc_kcontrol_component(kcontrol);
 	struct mcu_mic_codec_data *codec_data =
 		snd_soc_component_get_drvdata(component);
-	int ret;
+	int ret = 0;
 
 	dev_dbg(component->dev, "%s\n", __func__);
 
-	ret = dmic_mcu_send_message(component->dev, DMIC_MCU_MESSAGE_ACTIVITY_STOP, 1);
-	if (ret != 0)
-		return ret;
-
-	// Set the flag that ActivityStop has been sent since flag was cleared last time
+	// Save ActivityStop as true regardless if actual send to MCU succeed or
+	// not.  Reasoning is this saved flag is only used to notify MCU if MCU
+	// crashes, and we want to use the framework's intended state in restoring
+	// MCU after the crash.
 	mutex_lock(&codec_data->iolock);
 	codec_data->activity_stop_sent = true;
 	dev_info(component->dev, "%s: mcu_mic_activity_stop: %d\n",
 		__func__, codec_data->activity_stop_sent);
 	mutex_unlock(&codec_data->iolock);
 
-	return 0;
+	ret = dmic_mcu_send_message(component->dev, DMIC_MCU_MESSAGE_ACTIVITY_STOP, 1);
+	if (ret != 0) {
+		dev_err(component->dev,
+			"Error sending DMIC_MCU_MESSAGE_ACTIVITY_STOP: %d", ret);
+		ret = -EIO;
+	}
+	return ret;
 }