hal: audio: change wsa detection to sysfs thermal

For WSA detection, use sysfs thermal entry instead
of debugfs. Since debugfs will not be available
in production builds, change detection logic
for WSA presence to use sysfs thermal entries.

Change-Id: I79069d6286fd63b688211826740b89d0a6f8afa7
diff --git a/hal/audio_extn/audio_extn.h b/hal/audio_extn/audio_extn.h
index fe1ac74..73826d0 100644
--- a/hal/audio_extn/audio_extn.h
+++ b/hal/audio_extn/audio_extn.h
@@ -470,6 +470,7 @@
 
 int b64decode(char *inp, int ilen, uint8_t* outp);
 int b64encode(uint8_t *inp, int ilen, char* outp);
+int read_line_from_file(const char *path, char *buf, size_t count);
 
 #ifndef KPI_OPTIMIZE_ENABLED
 #define audio_extn_perf_lock_init() (0)
diff --git a/hal/audio_extn/spkr_protection.c b/hal/audio_extn/spkr_protection.c
index 139f986..aba2412 100644
--- a/hal/audio_extn/spkr_protection.c
+++ b/hal/audio_extn/spkr_protection.c
@@ -94,6 +94,11 @@
 #define SPKR_PROCESSING_IN_PROGRESS 1
 #define SPKR_PROCESSING_IN_IDLE 0
 
+#ifdef PLATFORM_MSM8916
+#define ACDB_DEVICE_SPKR_PROT_WSA_ANALOG 136
+#define ACDB_DEVICE_VI_FEEDBACK_WSA_ANALOG 137
+#endif
+
 #define MAX_PATH             (256)
 #define THERMAL_SYSFS "/sys/class/thermal"
 #define TZ_TYPE "/sys/class/thermal/thermal_zone%d/type"
@@ -151,27 +156,6 @@
 static struct speaker_prot_session handle;
 static int vi_feed_no_channels;
 
-int read_line_from_file(const char *path, char *buf, size_t count)
-{
-    char * fgets_ret;
-    FILE * fd;
-    int rv;
-
-    fd = fopen(path, "r");
-    if (fd == NULL)
-        return -1;
-
-    fgets_ret = fgets(buf, (int)count, fd);
-    if (NULL != fgets_ret) {
-        rv = (int)strlen(buf);
-    } else {
-        rv = ferror(fd);
-    }
-    fclose(fd);
-
-   return rv;
-}
-
 /*===========================================================================
 FUNCTION get_tzn
 
@@ -950,12 +934,20 @@
     handle.spkr_prot_t0 = -1;
 
     if (is_wsa_present()) {
+#ifdef PLATFORM_MSM8916
         if (platform_get_wsa_mode(adev) == 1) {
             ALOGD("%s: WSA analog mode", __func__);
-            platform_set_snd_device_acdb_id(SND_DEVICE_OUT_SPEAKER_PROTECTED, 136);
-            platform_set_snd_device_acdb_id(SND_DEVICE_IN_CAPTURE_VI_FEEDBACK, 137);
+            platform_set_snd_device_backend(SND_DEVICE_OUT_VOICE_SPEAKER_WSA,
+                                            "speaker-protected");
+            platform_set_snd_device_acdb_id(SND_DEVICE_OUT_SPEAKER_PROTECTED,
+                                            ACDB_DEVICE_SPKR_PROT_WSA_ANALOG);
+            platform_set_snd_device_acdb_id(SND_DEVICE_OUT_VOICE_SPEAKER_PROTECTED,
+                                            ACDB_DEVICE_SPKR_PROT_WSA_ANALOG);
+            platform_set_snd_device_acdb_id(SND_DEVICE_IN_CAPTURE_VI_FEEDBACK,
+                                            ACDB_DEVICE_VI_FEEDBACK_WSA_ANALOG);
             pcm_config_skr_prot.channels = 2;
         }
+#endif
         pthread_cond_init(&handle.spkr_calib_cancel, NULL);
         pthread_cond_init(&handle.spkr_calibcancel_ack, NULL);
         pthread_mutex_init(&handle.mutex_spkr_prot, NULL);
@@ -1030,9 +1022,15 @@
 
     switch(snd_device) {
     case SND_DEVICE_OUT_SPEAKER:
+#ifdef PLATFORM_MSM8916
+    case SND_DEVICE_OUT_SPEAKER_WSA:
+#endif
         acdb_id = platform_get_snd_device_acdb_id(SND_DEVICE_OUT_SPEAKER_PROTECTED);
         break;
     case SND_DEVICE_OUT_VOICE_SPEAKER:
+#ifdef PLATFORM_MSM8916
+    case SND_DEVICE_OUT_VOICE_SPEAKER_WSA:
+#endif
         acdb_id = platform_get_snd_device_acdb_id(SND_DEVICE_OUT_VOICE_SPEAKER_PROTECTED);
         break;
     default:
@@ -1049,8 +1047,14 @@
 
     switch(snd_device) {
     case SND_DEVICE_OUT_SPEAKER:
+#ifdef PLATFORM_MSM8916
+    case SND_DEVICE_OUT_SPEAKER_WSA:
+#endif
         return SND_DEVICE_OUT_SPEAKER_PROTECTED;
     case SND_DEVICE_OUT_VOICE_SPEAKER:
+#ifdef PLATFORM_MSM8916
+    case SND_DEVICE_OUT_VOICE_SPEAKER_WSA:
+#endif
         return SND_DEVICE_OUT_VOICE_SPEAKER_PROTECTED;
     default:
         return snd_device;
diff --git a/hal/audio_extn/utils.c b/hal/audio_extn/utils.c
index f699194..644300a 100644
--- a/hal/audio_extn/utils.c
+++ b/hal/audio_extn/utils.c
@@ -605,6 +605,27 @@
     return rc;
 }
 
+int read_line_from_file(const char *path, char *buf, size_t count)
+{
+    char * fgets_ret;
+    FILE * fd;
+    int rv;
+
+    fd = fopen(path, "r");
+    if (fd == NULL)
+        return -1;
+
+    fgets_ret = fgets(buf, (int)count, fd);
+    if (NULL != fgets_ret) {
+        rv = (int)strlen(buf);
+    } else {
+        rv = ferror(fd);
+    }
+    fclose(fd);
+
+   return rv;
+}
+
 void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
                                              struct audio_usecase *usecase)
 {
diff --git a/hal/msm8916/platform.c b/hal/msm8916/platform.c
index 80cfafb..4ea0785 100644
--- a/hal/msm8916/platform.c
+++ b/hal/msm8916/platform.c
@@ -1293,6 +1293,61 @@
     return result;
 }
 
+#define MAX_PATH             (256)
+#define THERMAL_SYSFS "/sys/class/thermal"
+#define TZ_TYPE "/sys/class/thermal/thermal_zone%d/type"
+#define TZ_WSA "/sys/class/thermal/thermal_zone%d/temp"
+
+bool is_wsa_found(void)
+{
+    DIR *tdir = NULL;
+    struct dirent *tdirent = NULL;
+    int tzn = 0;
+    char name[MAX_PATH] = {0};
+    char cwd[MAX_PATH] = {0};
+    char file[10] = "wsa";
+    bool found = false;
+
+    if (!getcwd(cwd, sizeof(cwd)))
+        return false;
+
+    chdir(THERMAL_SYSFS); /* Change dir to read the entries. Doesnt work
+                             otherwise */
+    tdir = opendir(THERMAL_SYSFS);
+    if (!tdir) {
+        ALOGE("Unable to open %s\n", THERMAL_SYSFS);
+        return false;
+    }
+
+    while ((tdirent = readdir(tdir))) {
+        char buf[50];
+        struct dirent *tzdirent;
+        DIR *tzdir = NULL;
+
+        tzdir = opendir(tdirent->d_name);
+        if (!tzdir)
+            continue;
+        while ((tzdirent = readdir(tzdir))) {
+            if (strcmp(tzdirent->d_name, "type"))
+                continue;
+            snprintf(name, MAX_PATH, TZ_TYPE, tzn);
+            ALOGD("Opening %s\n", name);
+            read_line_from_file(name, buf, sizeof(buf));
+            if (strstr(buf, file)) {
+                found = true;
+                break;
+            }
+            tzn++;
+        }
+        closedir(tzdir);
+        if (found == true)
+            break;
+    }
+    closedir(tdir);
+    chdir(cwd); /* Restore current working dir */
+    return found;
+}
+
 void *platform_init(struct audio_device *adev)
 {
     char platform[PROPERTY_VALUE_MAX];
@@ -1484,24 +1539,8 @@
     }
     audio_extn_pm_vote();
 
-    // Check if WSA speaker is supported in codec
-    char CodecPeek[1024] = "/sys/kernel/debug/asoc/";
-    DIR *dir;
-    struct dirent *dirent;
-    char file_name[10] = "wsa";
-    strlcat(CodecPeek, snd_card_name, sizeof(CodecPeek));
-
-    dir = opendir(CodecPeek);
-    if (dir != NULL) {
-        while (NULL != (dirent = readdir(dir))) {
-            if (strstr (dirent->d_name,file_name))
-            {
-                my_data->is_wsa_speaker = true;
-                break;
-            }
-        }
-        closedir(dir);
-    }
+    if (is_wsa_found())
+        my_data->is_wsa_speaker = true;
 
     /* Configure active back end for HPX*/
     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);