asoc: sdm845: check sound card status before sending AFE config

WCD codec must be enumerated before AFE configuration for
some of the features can be sent to DSP. This is because
as part of setting up this configuration, DSP tries to
access some codec register. These register accesses will
fail if codec is not enumerated already.
Add a check for Sound Card online state to confirm that WCD
codec has already enumerated before sending AFE configs.

Change-Id: I0b7e5eb49e4f6c6aee9cb142054e57eae592602b
Signed-off-by: Banajit Goswami <bgoswami@codeaurora.org>
diff --git a/asoc/sdm845.c b/asoc/sdm845.c
index d75174c..3eb20a2 100644
--- a/asoc/sdm845.c
+++ b/asoc/sdm845.c
@@ -3618,31 +3618,44 @@
 	afe_clear_config(AFE_SLIMBUS_SLAVE_CONFIG);
 }
 
-static int msm_adsp_power_up_config(struct snd_soc_codec *codec)
+static int msm_adsp_power_up_config(struct snd_soc_codec *codec,
+				    struct snd_card *card)
 {
 	int ret = 0;
 	unsigned long timeout;
 	int adsp_ready = 0;
+	bool snd_card_online = 0;
 
 	timeout = jiffies +
 		msecs_to_jiffies(ADSP_STATE_READY_TIMEOUT_MS);
 
 	do {
-		if (q6core_is_adsp_ready()) {
-			pr_debug("%s: ADSP Audio is ready\n", __func__);
-			adsp_ready = 1;
-			break;
+		if (!snd_card_online) {
+			snd_card_online = snd_card_is_online_state(card);
+			pr_debug("%s: Sound card is %s\n", __func__,
+				 snd_card_online ? "Online" : "Offline");
 		}
+		if (!adsp_ready) {
+			adsp_ready = q6core_is_adsp_ready();
+			pr_debug("%s: ADSP Audio is %s\n", __func__,
+				 adsp_ready ? "ready" : "not ready");
+		}
+		if (snd_card_online && adsp_ready)
+			break;
+
 		/*
-		 * ADSP will be coming up after subsystem restart and
+		 * Sound card/ADSP will be coming up after subsystem restart and
 		 * it might not be fully up when the control reaches
 		 * here. So, wait for 50msec before checking ADSP state
 		 */
 		msleep(50);
 	} while (time_after(timeout, jiffies));
 
-	if (!adsp_ready) {
-		pr_err("%s: timed out waiting for ADSP Audio\n", __func__);
+	if (!snd_card_online || !adsp_ready) {
+		pr_err("%s: Timeout. Sound card is %s, ADSP Audio is %s\n",
+		       __func__,
+		       snd_card_online ? "Online" : "Offline",
+		       adsp_ready ? "ready" : "not ready");
 		ret = -ETIMEDOUT;
 		goto err;
 	}
@@ -3700,7 +3713,7 @@
 		}
 		codec = rtd->codec;
 
-		ret = msm_adsp_power_up_config(codec);
+		ret = msm_adsp_power_up_config(codec, card->snd_card);
 		if (ret < 0) {
 			dev_err(card->dev,
 				"%s: msm_adsp_power_up_config failed ret = %d!\n",
@@ -3798,7 +3811,7 @@
 
 	msm_codec_fn.get_afe_config_fn = tavil_get_afe_config;
 
-	ret = msm_adsp_power_up_config(codec);
+	ret = msm_adsp_power_up_config(codec, rtd->card->snd_card);
 	if (ret) {
 		pr_err("%s: Failed to set AFE config %d\n", __func__, ret);
 		goto err;
@@ -7046,6 +7059,7 @@
 	struct msm_asoc_mach_data *pdata =
 				snd_soc_card_get_drvdata(card);
 
+	audio_notifier_deregister("sdm845");
 	if (pdata->us_euro_gpio > 0) {
 		gpio_free(pdata->us_euro_gpio);
 		pdata->us_euro_gpio = 0;
@@ -7054,7 +7068,6 @@
 
 	msm_release_pinctrl(pdev);
 	snd_soc_unregister_card(card);
-	audio_notifier_deregister("sdm845");
 	return 0;
 }