power: Simplify soc_id checks

 - Get soc_id in a common util function
 - Return boolean values for the target-specific soc_id checks

Change-Id: I038c435d28855859f36566de7acf881037d070f2
diff --git a/power/power-8916.c b/power/power-8916.c
index efef960..e9e983b 100644
--- a/power/power-8916.c
+++ b/power/power-8916.c
@@ -58,8 +58,6 @@
     "sys/devices/system/cpu/cpu3/cpufreq/scaling_min_freq"
 };
 
-static int is_8916 = -1;
-
 static int display_hint_sent;
 int display_boost;
 static int saved_interactive_mode = -1;
@@ -71,27 +69,26 @@
 
 static int current_power_profile = PROFILE_BALANCED;
 
-static int is_target_8916() /* Returns value=8916 if target is 8916 else value 0 */
+/**
+ * If target is 8916:
+ *     return 1
+ * else:
+ *     return 0
+ */
+static int is_target_8916(void)
 {
-    int fd;
-    char buf[10] = {0};
+    static int is_8916 = -1;
+    int soc_id;
 
     if (is_8916 >= 0)
         return is_8916;
 
-    fd = open("/sys/devices/soc0/soc_id", O_RDONLY);
-    if (fd >= 0) {
-        if (read(fd, buf, sizeof(buf) - 1) == -1) {
-            ALOGW("Unable to read soc_id");
-            is_8916 = 0;
-        } else {
-            int soc_id = atoi(buf);
-            if (soc_id == 206 || (soc_id >= 247 && soc_id <= 250))  {
-            is_8916 = 8916; /* Above SOCID for 8916 */
-            }
-        }
-    }
-    close(fd);
+    soc_id = get_soc_id();
+    if (soc_id == 206 || (soc_id >= 247 && soc_id <= 250))
+        is_8916 = 1;
+    else
+        is_8916 = 0;
+
     return is_8916;
 }
 
@@ -283,10 +280,7 @@
 
     if (!on) {
         /* Display off. */
-       switch(is_target_8916()) {
-
-          case 8916:
-           {
+        if (is_target_8916()) {
             if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) &&
                 (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) {
                int resource_values[] = {TR_MS_50, THREAD_MIGRATION_SYNC_OFF};
@@ -297,11 +291,8 @@
                       display_hint_sent = 1;
                   }
             } /* Perf time rate set for 8916 target*/
-           } /* End of Switch case for 8916 */
-            break ;
-
-            default:
-            {
+        /* End of display hint for 8916 */
+        } else {
              if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) &&
                 (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) {
                int resource_values[] = {TR_MS_CPU0_50,TR_MS_CPU4_50, THREAD_MIGRATION_SYNC_OFF};
@@ -328,9 +319,8 @@
                       display_hint_sent = 1;
                   }
              } /* Perf time rate set for CORE0,CORE4 8939 target*/
-           }/* End of Switch case for 8939 */
-           break ;
-          }
+        /* End of display hint for 8939 */
+        }
 
     } else {
         /* Display on. */
diff --git a/power/power-8960.c b/power/power-8960.c
index 23d7b14..cfdcb42 100644
--- a/power/power-8960.c
+++ b/power/power-8960.c
@@ -50,33 +50,30 @@
 
 static int current_power_profile = PROFILE_BALANCED;
 
-static int is_8064 = -1;
-
 int get_number_of_profiles() {
     return 3;
 }
 
-static int is_target_8064() /* Returns value=8064 if target is 8064 else value 0 */
+/**
+ * If target is 8064:
+ *     return 1
+ * else:
+ *     return 0
+ */
+static int is_target_8064(void)
 {
-    int fd;
-    char buf[10] = {0};
+    static int is_8064 = -1;
+    int soc_id;
 
     if (is_8064 >= 0)
         return is_8064;
 
-    fd = open("/sys/devices/system/soc/soc0/id", O_RDONLY);
-    if (fd >= 0) {
-        if (read(fd, buf, sizeof(buf) - 1) == -1) {
-            ALOGW("Unable to read soc_id");
-            is_8064 = 0;
-        } else {
-            int soc_id = atoi(buf);
-            if (soc_id == 153)  {
-                is_8064 = 8064;
-            }
-        }
-    }
-    close(fd);
+    soc_id = get_soc_id();
+    if (soc_id == 153)
+        is_8064 = 1;
+    else
+        is_8064 = 0;
+
     return is_8064;
 }
 
diff --git a/power/power.c b/power/power.c
index 740dd35..311e507 100644
--- a/power/power.c
+++ b/power/power.c
@@ -68,23 +68,14 @@
 
 static void power_init(__attribute__((unused))struct power_module *module)
 {
+    int soc_id;
+
     ALOGI("QCOM power HAL initing.");
 
-    int fd;
-    char buf[10] = {0};
-
-    fd = open("/sys/devices/soc0/soc_id", O_RDONLY);
-    if (fd >= 0) {
-        if (read(fd, buf, sizeof(buf) - 1) == -1) {
-            ALOGW("Unable to read soc_id");
-        } else {
-            int soc_id = atoi(buf);
-            if (soc_id == 194 || (soc_id >= 208 && soc_id <= 218) || soc_id == 178) {
-                display_boost = 1;
-            }
-        }
-        close(fd);
-    }
+    soc_id = get_soc_id();
+    if (soc_id == 178 || soc_id == 194 ||
+            (soc_id >= 208 && soc_id <= 218))
+        display_boost = 1;
 }
 
 static void process_video_decode_hint(void *metadata)
diff --git a/power/utils.c b/power/utils.c
index c1ea78a..202b718 100644
--- a/power/utils.c
+++ b/power/utils.c
@@ -42,6 +42,9 @@
 #define LOG_TAG "QCOM PowerHAL"
 #include <utils/Log.h>
 
+#define SOC_ID_0 "/sys/devices/soc0/soc_id"
+#define SOC_ID_1 "/sys/devices/system/soc/soc0/id"
+
 char scaling_gov_path[4][80] ={
     "sys/devices/system/cpu/cpu0/cpufreq/scaling_governor",
     "sys/devices/system/cpu/cpu1/cpufreq/scaling_governor",
@@ -388,3 +391,24 @@
     }
 }
 
+int get_soc_id(void)
+{
+    int fd;
+    int soc_id = -1;
+    char buf[10] = { 0 };
+
+    if (!access(SOC_ID_0, F_OK))
+        fd = open(SOC_ID_0, O_RDONLY);
+    else
+        fd = open(SOC_ID_1, O_RDONLY);
+
+    if (fd >= 0) {
+        if (read(fd, buf, sizeof(buf) - 1) == -1)
+            ALOGW("Unable to read soc_id");
+        else
+            soc_id = atoi(buf);
+    }
+
+    close(fd);
+    return soc_id;
+}
diff --git a/power/utils.h b/power/utils.h
index 5907281..fb8d84c 100644
--- a/power/utils.h
+++ b/power/utils.h
@@ -44,3 +44,4 @@
 void undo_initial_hint_action();
 void set_profile(int profile);
 void start_prefetch(int pid, const char *packageName);
+int get_soc_id(void);