hal: fix DP-CTS failures

- DP driver supports 32KHz, so update HAL to allow 32KHz samplerate
- Derive channel map and channel allocation based on input channel count

CRs-Fixed: 2519839, 2519881
Change-Id: I321bea7096ba158bb5b6bb4968b938a1eb2b4cae
diff --git a/hal/msm8916/platform.c b/hal/msm8916/platform.c
index 3094183..73369ea 100644
--- a/hal/msm8916/platform.c
+++ b/hal/msm8916/platform.c
@@ -6115,7 +6115,8 @@
             if (rate_str == NULL) {
                 switch (sample_rate) {
                 case 32000:
-                    if (passthrough_enabled || (backend_idx == HDMI_TX_BACKEND )) {
+                    if (passthrough_enabled || (backend_idx == HDMI_TX_BACKEND) ||
+                            (backend_idx == DISP_PORT_RX_BACKEND)) {
                         rate_str = "KHZ_32";
                         break;
                     }
@@ -7569,37 +7570,47 @@
     struct platform_data *my_data = (struct platform_data *)platform;
     struct audio_device *adev = my_data->adev;
     edid_audio_info *info = NULL;
-    int channel_count = 2;
-    int i, ret;
+    int ret;
     char default_channelMap[MAX_CHANNELS_SUPPORTED] = {0};
     struct audio_device_config_param *adev_device_cfg_ptr = adev->device_cfg_params;
+    int channel_alloc = 0;
+    int max_supported_channels = 0;
 
     ret = platform_get_edid_info(platform);
     info = (edid_audio_info *)my_data->edid_info;
     adev_device_cfg_ptr += HDMI_RX_BACKEND;
     if(ret == 0 && info != NULL) {
-        if (channels > 2) {
-
+        if ((channels > 2) && (channels <= MAX_HDMI_CHANNEL_CNT)) {
             ALOGV("%s:able to get HDMI sink capabilities multi channel playback",
                    __func__);
-            for (i = 0; i < info->audio_blocks && i < MAX_EDID_BLOCKS; i++) {
-                if (info->audio_blocks_array[i].format_id == LPCM &&
-                      info->audio_blocks_array[i].channels > channel_count &&
-                      info->audio_blocks_array[i].channels <= MAX_HDMI_CHANNEL_CNT) {
-                    channel_count = info->audio_blocks_array[i].channels;
-                }
+            max_supported_channels = platform_edid_get_max_channels(my_data);
+            if (channels > max_supported_channels)
+                channels = max_supported_channels;
+            // refer to HDMI spec CEA-861-E: Table 28 Audio InfoFrame Data Byte 4
+            switch (channels) {
+            case 3:
+                channel_alloc = 0x02; break;
+            case 4:
+                channel_alloc = 0x06; break;
+            case 5:
+                channel_alloc = 0x0A; break;
+            case 6:
+                channel_alloc = 0x0B; break;
+            case 7:
+                channel_alloc = 0x12; break;
+            case 8:
+                channel_alloc = 0x13; break;
+            default:
+                ALOGE("%s: invalid channel %d", __func__, channels);
+                return -EINVAL;
             }
-            ALOGV("%s:channel_count:%d", __func__, channel_count);
-            /*
-             * Channel map is set for supported hdmi max channel count even
-             * though the input channel count set on adm is less than or equal to
-             * max supported channel count
-             */
+            ALOGVV("%s:channels:%d", __func__, channels);
+
             if (adev_device_cfg_ptr->use_client_dev_cfg) {
                 platform_set_channel_map(platform, adev_device_cfg_ptr->dev_cfg_params.channels,
                                    (char *)adev_device_cfg_ptr->dev_cfg_params.channel_map, -1, -1);
             } else {
-                platform_set_channel_map(platform, channel_count, info->channel_map, -1, -1);
+                platform_set_channel_map(platform, channels, info->channel_map, -1, -1);
             }
 
             if (adev_device_cfg_ptr->use_client_dev_cfg) {
@@ -7608,8 +7619,8 @@
                 platform_set_channel_allocation(platform,
                        adev_device_cfg_ptr->dev_cfg_params.channel_allocation);
             } else {
-                platform_set_channel_allocation(platform, info->channel_allocation);
-           }
+                platform_set_channel_allocation(platform, channel_alloc);
+            }
         } else {
             if (adev_device_cfg_ptr->use_client_dev_cfg) {
                 default_channelMap[0] = adev_device_cfg_ptr->dev_cfg_params.channel_map[0];
@@ -7619,7 +7630,7 @@
                 default_channelMap[1] = PCM_CHANNEL_FR;
             }
             platform_set_channel_map(platform, 2, default_channelMap, -1, -1);
-            platform_set_channel_allocation(platform,0);
+            platform_set_channel_allocation(platform, 0);
         }
     }
 
diff --git a/hal/msm8974/platform.c b/hal/msm8974/platform.c
index 3383c4f..cbeecd3 100644
--- a/hal/msm8974/platform.c
+++ b/hal/msm8974/platform.c
@@ -8479,9 +8479,10 @@
             if (rate_str == NULL) {
                 switch (sample_rate) {
                 case 32000:
-                    if (passthrough_enabled || (backend_idx == SPDIF_TX_BACKEND ) ||
-                        (backend_idx == HDMI_TX_BACKEND ) ||
-                        (backend_idx == HDMI_ARC_TX_BACKEND )) {
+                    if (passthrough_enabled || (backend_idx == SPDIF_TX_BACKEND) ||
+                        (backend_idx == HDMI_TX_BACKEND) ||
+                        (backend_idx == HDMI_ARC_TX_BACKEND) ||
+                        (backend_idx == DISP_PORT_RX_BACKEND)) {
                         rate_str = "KHZ_32";
                         break;
                     }
@@ -10310,11 +10311,12 @@
     struct platform_data *my_data = (struct platform_data *)platform;
     struct audio_device *adev = my_data->adev;
     edid_audio_info *info = NULL;
-    int channel_count = 2;
-    int i, ret;
+    int ret;
     char default_channelMap[MAX_CHANNELS_SUPPORTED] = {0};
     struct audio_device_config_param *adev_device_cfg_ptr = adev->device_cfg_params;
     int be_idx = -1;
+    int channel_alloc = 0;
+    int max_supported_channels = 0;
 
     if ((backend_idx != HDMI_RX_BACKEND) &&
             (backend_idx != DISP_PORT_RX_BACKEND) &&
@@ -10337,28 +10339,37 @@
     info = (edid_audio_info *)my_data->ext_disp[controller][stream].edid_info;
     adev_device_cfg_ptr += backend_idx;
     if(ret == 0 && info != NULL) {
-        if (channels > 2) {
-
+        if ((channels > 2) && (channels <= MAX_HDMI_CHANNEL_CNT)) {
             ALOGV("%s:able to get HDMI/DP sink capabilities multi channel playback",
                    __func__);
-            for (i = 0; i < info->audio_blocks && i < MAX_EDID_BLOCKS; i++) {
-                if (info->audio_blocks_array[i].format_id == LPCM &&
-                      info->audio_blocks_array[i].channels > channel_count &&
-                      info->audio_blocks_array[i].channels <= MAX_HDMI_CHANNEL_CNT) {
-                    channel_count = info->audio_blocks_array[i].channels;
-                }
+            max_supported_channels = platform_edid_get_max_channels(my_data);
+            if (channels > max_supported_channels)
+                channels = max_supported_channels;
+            // refer to HDMI spec CEA-861-E: Table 28 Audio InfoFrame Data Byte 4
+            switch (channels) {
+            case 3:
+                channel_alloc = 0x02; break;
+            case 4:
+                channel_alloc = 0x06; break;
+            case 5:
+                channel_alloc = 0x0A; break;
+            case 6:
+                channel_alloc = 0x0B; break;
+            case 7:
+                channel_alloc = 0x12; break;
+            case 8:
+                channel_alloc = 0x13; break;
+            default:
+                ALOGE("%s: invalid channel %d", __func__, channels);
+                return -EINVAL;
             }
-            ALOGVV("%s:channel_count:%d", __func__, channel_count);
-            /*
-             * Channel map is set for supported hdmi max channel count even
-             * though the input channel count set on adm is less than or equal to
-             * max supported channel count
-             */
+            ALOGVV("%s:channels:%d", __func__, channels);
+
             if (adev_device_cfg_ptr->use_client_dev_cfg) {
                 platform_set_channel_map(platform, adev_device_cfg_ptr->dev_cfg_params.channels,
                                      (char *)adev_device_cfg_ptr->dev_cfg_params.channel_map, -1, be_idx);
             } else {
-                platform_set_channel_map(platform, channel_count, info->channel_map, -1, be_idx);
+                platform_set_channel_map(platform, channels, info->channel_map, -1, be_idx);
             }
 
             if (adev_device_cfg_ptr->use_client_dev_cfg) {
@@ -10368,9 +10379,9 @@
                        adev_device_cfg_ptr->dev_cfg_params.channel_allocation,
                        controller, stream);
             } else {
-                platform_set_channel_allocation_v2(platform, info->channel_allocation,
+                platform_set_channel_allocation_v2(platform, channel_alloc,
                                                 controller, stream);
-           }
+            }
         } else {
             if (adev_device_cfg_ptr->use_client_dev_cfg) {
                 default_channelMap[0] = adev_device_cfg_ptr->dev_cfg_params.channel_map[0];