ASoC: msm: ensure ADM is only opened/closed for multimedia purpose

It was found that upon initation of voice call, ADM COPP is opened
even though there is no audio playback or capture. This behavior
forces QDSP6 to scale up clock frequency because it is expected that
, with ADM COPP opened, post/pre processing will be exercised hence
require more MIPs in QDSP6 to process the request. Unecessary power
consumption is wasted as result. Update pcm routing driver not to
start ADM COPP unnecessarily.

CRs-fixed: 316604
Change-Id: I90b33ae1582fb76c74d9e4ec13962db34374c60d
Signed-off-by: Patrick Lai <plai@codeaurora.org>
diff --git a/sound/soc/msm/msm-pcm-routing.c b/sound/soc/msm/msm-pcm-routing.c
index b254cd8..9a076d6 100644
--- a/sound/soc/msm/msm-pcm-routing.c
+++ b/sound/soc/msm/msm-pcm-routing.c
@@ -118,7 +118,7 @@
 
 
 /* Track ASM playback & capture sessions of DAI */
-static int fe_dai_map[MSM_FRONTEND_DAI_MAX][2] = {
+static int fe_dai_map[MSM_FRONTEND_DAI_MM_SIZE][2] = {
 	/* MULTIMEDIA1 */
 	{INVALID_SESSION, INVALID_SESSION},
 	/* MULTIMEDIA2 */
@@ -158,6 +158,12 @@
 	int i, session_type, path_type, port_type;
 	struct route_payload payload;
 
+	if (fedai_id > MSM_FRONTEND_DAI_MM_MAX_ID) {
+		/* bad ID assigned in machine driver */
+		pr_err("%s: bad MM ID\n", __func__);
+		return;
+	}
+
 	if (stream_type == SNDRV_PCM_STREAM_PLAYBACK) {
 		session_type = SESSION_TYPE_RX;
 		path_type = ADM_PATH_PLAYBACK;
@@ -200,6 +206,12 @@
 {
 	int i, port_type, session_type;
 
+	if (fedai_id > MSM_FRONTEND_DAI_MM_MAX_ID) {
+		/* bad ID assigned in machine driver */
+		pr_err("%s: bad MM ID\n", __func__);
+		return;
+	}
+
 	if (stream_type == SNDRV_PCM_STREAM_PLAYBACK) {
 		port_type = MSM_AFE_PORT_TYPE_RX;
 		session_type = SESSION_TYPE_RX;
@@ -229,6 +241,12 @@
 
 	pr_debug("%s: reg %x val %x set %x\n", __func__, reg, val, set);
 
+	if (val > MSM_FRONTEND_DAI_MM_MAX_ID) {
+		/* recheck FE ID in the mixer control defined in this file */
+		pr_err("%s: bad MM ID\n", __func__);
+		return;
+	}
+
 	if (afe_get_port_type(msm_bedais[reg].port_id) ==
 		MSM_AFE_PORT_TYPE_RX) {
 		session_type = SESSION_TYPE_RX;
@@ -1253,6 +1271,11 @@
 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
 	unsigned int be_id = rtd->dai_link->be_id;
 
+	if (be_id >= MSM_BACKEND_DAI_MAX) {
+		pr_err("%s: unexpected be_id %d\n", __func__, be_id);
+		return -EINVAL;
+	}
+
 	mutex_lock(&routing_lock);
 	msm_bedais[be_id].hw_params = params;
 	mutex_unlock(&routing_lock);
@@ -1278,7 +1301,7 @@
 
 	mutex_lock(&routing_lock);
 
-	for_each_set_bit(i, &bedai->fe_sessions, MSM_FRONTEND_DAI_MAX) {
+	for_each_set_bit(i, &bedai->fe_sessions, MSM_FRONTEND_DAI_MM_MAX_ID) {
 		if (fe_dai_map[i][session_type] != INVALID_SESSION)
 			adm_close(bedai->port_id);
 	}
@@ -1332,7 +1355,7 @@
 	 */
 	bedai->active = 1;
 
-	for_each_set_bit(i, &bedai->fe_sessions, MSM_FRONTEND_DAI_MAX) {
+	for_each_set_bit(i, &bedai->fe_sessions, MSM_FRONTEND_DAI_MM_MAX_ID) {
 		if (fe_dai_map[i][session_type] != INVALID_SESSION) {
 			adm_open(bedai->port_id, path_type,
 				params_rate(bedai->hw_params),