Merge changes I35fbf555,Id436471b

* changes:
  powerHAL: Parameterize lowpower cpufreq code
  powerHAL: Parameterize schedtune boost values
diff --git a/power/power_hikey.c b/power/power_hikey.c
index d54eae9..2de4c1c 100644
--- a/power/power_hikey.c
+++ b/power/power_hikey.c
@@ -37,17 +37,17 @@
 #include <hardware/power.h>
 
 #define SCHEDTUNE_BOOST_PATH "/dev/stune/top-app/schedtune.boost"
-#define SCHEDTUNE_BOOST_NORM "10"
-#define SCHEDTUNE_BOOST_INTERACTIVE "40"
-#define SCHEDTUNE_BOOST_TIME_NS 1000000000LL
+#define SCHEDTUNE_BOOST_VAL_PROP "ro.config.schetune.touchboost.value"
+#define SCHEDTUNE_BOOST_TIME_PROP "ro.config.schetune.touchboost.time_ns"
+
+#define SCHEDTUNE_BOOST_VAL_DEFAULT "40"
+
+char schedtune_boost_norm[PROPERTY_VALUE_MAX] = "10";
+char schedtune_boost_interactive[PROPERTY_VALUE_MAX] = SCHEDTUNE_BOOST_VAL_DEFAULT;
+long long schedtune_boost_time_ns = 1000000000LL;
+
 #define INTERACTIVE_BOOSTPULSE_PATH "/sys/devices/system/cpu/cpufreq/interactive/boostpulse"
 #define INTERACTIVE_IO_IS_BUSY_PATH "/sys/devices/system/cpu/cpufreq/interactive/io_is_busy"
-#define CPU_MAX_FREQ_PATH "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq"
-#define LOW_POWER_MAX_FREQ "729000"
-#define NORMAL_MAX_FREQ "1200000"
-#define SVELTE_PROP "ro.boot.svelte"
-#define SVELTE_MAX_FREQ_PROP "ro.config.svelte.max_cpu_freq"
-#define SVELTE_LOW_POWER_MAX_FREQ_PROP "ro.config.svelte.low_power_max_cpu_freq"
 
 struct hikey_power_module {
     struct power_module base;
@@ -61,10 +61,21 @@
     sem_t signal_lock;
 };
 
+
 static bool low_power_mode = false;
 
-static char *max_cpu_freq = NORMAL_MAX_FREQ;
-static char *low_power_max_cpu_freq = LOW_POWER_MAX_FREQ;
+
+#define CPUFREQ_CLUST_MAX_FREQ_PATH_PROP "ro.config.cpufreq.max_freq.cluster"
+#define CPUFREQ_CLUST_LOW_POWER_MAX_FREQ_PROP "ro.config.cpufreq.low_power_max.cluster"
+#define CPUFREQ_CLUST0_MAX_FREQ_PATH_DEFAULT "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq"
+
+#define NR_CLUSTERS 4
+static int max_clusters = 1;
+static struct hikey_cpufreq_t {
+	char path[PROPERTY_VALUE_MAX];
+	char normal_max[PROPERTY_VALUE_MAX];
+	char low_power_max[PROPERTY_VALUE_MAX];
+} hikey_cpufreq_clusters[NR_CLUSTERS];
 
 
 #define container_of(addr, struct_name, field_name) \
@@ -93,6 +104,25 @@
     return len;
 }
 
+static int sysfs_read(const char *path, char *s, int slen)
+{
+    int len;
+    int fd = open(path, O_RDONLY);
+
+    if (fd < 0) {
+        ALOGE("Error opening %s\n", path);
+        return fd;
+    }
+
+    len = read(fd, s, slen);
+    if (len < 0) {
+        ALOGE("Error reading %s\n", path);
+    }
+
+    close(fd);
+    return len;
+}
+
 #define NSEC_PER_SEC 1000000000LL
 static long long gettime_ns(void)
 {
@@ -113,8 +143,6 @@
 /*[interactive cpufreq gov funcs]*********************************************/
 static void interactive_power_init(struct hikey_power_module __unused *hikey)
 {
-    int32_t is_svelte = property_get_int32(SVELTE_PROP, 0);
-
     if (sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/timer_rate",
                 "20000") < 0)
         return;
@@ -134,29 +162,6 @@
                 "1000000");
     sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/io_is_busy", "0");
 
-    if (is_svelte) {
-        char prop_buffer[PROPERTY_VALUE_MAX];
-        int len = property_get(SVELTE_MAX_FREQ_PROP, prop_buffer,
-                               LOW_POWER_MAX_FREQ);
-
-        max_cpu_freq = strndup(prop_buffer, len);
-        len = property_get(SVELTE_LOW_POWER_MAX_FREQ_PROP, prop_buffer,
-                           LOW_POWER_MAX_FREQ);
-        low_power_max_cpu_freq = strndup(prop_buffer, len);
-    }
-}
-
-static void power_set_interactive(struct power_module __unused *module, int on)
-{
-    ALOGV("power_set_interactive: %d\n", on);
-
-    /*
-     * Lower maximum frequency when screen is off.
-     */
-    sysfs_write(CPU_MAX_FREQ_PATH,
-                (!on || low_power_mode) ? low_power_max_cpu_freq : max_cpu_freq);
-    sysfs_write(INTERACTIVE_IO_IS_BUSY_PATH, on ? "1" : "0");
-    ALOGV("power_set_interactive: %d done\n", on);
 }
 
 static int interactive_boostpulse(struct hikey_power_module *hikey)
@@ -197,7 +202,7 @@
     if (hikey->schedtune_boost_fd < 0)
         return hikey->schedtune_boost_fd;
 
-    len = write(hikey->schedtune_boost_fd, booststr, 2);
+    len = write(hikey->schedtune_boost_fd, booststr, strlen(booststr));
     if (len < 0) {
         strerror_r(errno, buf, sizeof(buf));
         ALOGE("Error writing to %s: %s\n", SCHEDTUNE_BOOST_PATH, buf);
@@ -223,7 +228,7 @@
                 continue;
             }
 
-            schedtune_sysfs_boost(hikey, SCHEDTUNE_BOOST_NORM);
+            schedtune_sysfs_boost(hikey, schedtune_boost_norm);
             hikey->deboost_time = 0;
             pthread_mutex_unlock(&hikey->lock);
             break;
@@ -241,10 +246,10 @@
 
     now = gettime_ns();
     if (!hikey->deboost_time) {
-        schedtune_sysfs_boost(hikey, SCHEDTUNE_BOOST_INTERACTIVE);
+        schedtune_sysfs_boost(hikey, schedtune_boost_interactive);
         sem_post(&hikey->signal_lock);
     }
-    hikey->deboost_time = now + SCHEDTUNE_BOOST_TIME_NS;
+    hikey->deboost_time = now + schedtune_boost_time_ns;
 
     return 0;
 }
@@ -254,24 +259,93 @@
     char buf[50];
     pthread_t tid;
 
-
     hikey->deboost_time = 0;
     sem_init(&hikey->signal_lock, 0, 1);
 
-    hikey->schedtune_boost_fd = open(SCHEDTUNE_BOOST_PATH, O_WRONLY);
+    hikey->schedtune_boost_fd = open(SCHEDTUNE_BOOST_PATH, O_RDWR);
     if (hikey->schedtune_boost_fd < 0) {
         strerror_r(errno, buf, sizeof(buf));
         ALOGE("Error opening %s: %s\n", SCHEDTUNE_BOOST_PATH, buf);
+        return;
     }
 
+    schedtune_boost_time_ns = property_get_int64(SCHEDTUNE_BOOST_TIME_PROP,
+                                                 1000000000LL);
+    property_get(SCHEDTUNE_BOOST_VAL_PROP, schedtune_boost_interactive,
+                 SCHEDTUNE_BOOST_VAL_DEFAULT);
+
+    if (hikey->schedtune_boost_fd >= 0) {
+        size_t len = read(hikey->schedtune_boost_fd, schedtune_boost_norm,
+                          PROPERTY_VALUE_MAX);
+	if (len <= 0)
+            ALOGE("Error reading normal boost value\n");
+	else if (schedtune_boost_norm[len] == '\n')
+            schedtune_boost_norm[len] = '\0';
+
+    }
+
+    ALOGV("Starting with schedtune boost norm: %s touchboost: %s and boosttime: %lld\n",
+	  schedtune_boost_norm, schedtune_boost_interactive, schedtune_boost_time_ns);
+
     pthread_create(&tid, NULL, schedtune_deboost_thread, hikey);
 }
 
 /*[generic functions]*********************************************************/
+
+static void hikey_cpufreq_set_interactive(struct power_module __unused *module, int on)
+{
+    int i;
+
+    /*
+     * Lower maximum frequency when screen is off.
+     */
+    for (i=0; i < max_clusters; i++) {
+        if ((!on || low_power_mode) && hikey_cpufreq_clusters[i].low_power_max[0] != '\0')
+		sysfs_write(hikey_cpufreq_clusters[i].path, hikey_cpufreq_clusters[i].low_power_max);
+	else
+		sysfs_write(hikey_cpufreq_clusters[i].path, hikey_cpufreq_clusters[i].normal_max);
+    }
+    sysfs_write(INTERACTIVE_IO_IS_BUSY_PATH, on ? "1" : "0");
+}
+
+
+static void hikey_cpufreq_init(struct hikey_power_module *hikey)
+{
+    char buf[128];
+    int len, i;
+
+    for (i=0; i < NR_CLUSTERS; i++) {
+        sprintf(buf,"%s%d", CPUFREQ_CLUST_MAX_FREQ_PATH_PROP, i);
+        property_get(buf, hikey_cpufreq_clusters[i].path, "");
+
+        if (hikey_cpufreq_clusters[i].path[0] == '\0') {
+            if (i == 0) {
+                /* In case no property was set, pick cpu0's cluster */
+                strncpy(hikey_cpufreq_clusters[i].path,
+                        CPUFREQ_CLUST0_MAX_FREQ_PATH_DEFAULT,
+                        PROPERTY_VALUE_MAX);
+            } else
+                break;
+        }
+        sprintf(buf,"%s%d", CPUFREQ_CLUST_LOW_POWER_MAX_FREQ_PROP, i);
+        property_get(buf, hikey_cpufreq_clusters[i].low_power_max, "");
+        len = sysfs_read(hikey_cpufreq_clusters[i].path,
+                         hikey_cpufreq_clusters[i].normal_max,
+                         PROPERTY_VALUE_MAX);
+        ALOGV("Cluster: %d path: %s  low: %s norm: %s\n", i,
+              hikey_cpufreq_clusters[i].path,
+              hikey_cpufreq_clusters[i].low_power_max,
+              hikey_cpufreq_clusters[i].normal_max);
+    }
+    max_clusters = i;
+}
+
+
 static void hikey_power_init(struct power_module __unused *module)
 {
     struct hikey_power_module *hikey = container_of(module,
                                               struct hikey_power_module, base);
+    hikey_cpufreq_init(hikey);
     interactive_power_init(hikey);
     schedtune_power_init(hikey);
 }
@@ -302,12 +376,8 @@
         break;
 
     case POWER_HINT_LOW_POWER:
-        if (data) {
-            sysfs_write(CPU_MAX_FREQ_PATH, low_power_max_cpu_freq);
-        } else {
-            sysfs_write(CPU_MAX_FREQ_PATH, max_cpu_freq);
-        }
         low_power_mode = data;
+        hikey_cpufreq_set_interactive(module, 1);
         break;
 
     default:
@@ -346,7 +416,7 @@
 
             dev->init = hikey_power_init;
             dev->powerHint = hikey_power_hint;
-            dev->setInteractive = power_set_interactive;
+            dev->setInteractive = hikey_cpufreq_set_interactive;
             dev->setFeature = set_feature;
 
             *device = (hw_device_t*)dev;
@@ -377,7 +447,7 @@
         },
 
         .init = hikey_power_init,
-        .setInteractive = power_set_interactive,
+        .setInteractive = hikey_cpufreq_set_interactive,
         .powerHint = hikey_power_hint,
         .setFeature = set_feature,
     },