John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | * Based on the FlounderPowerHAL |
| 17 | */ |
| 18 | |
| 19 | #include <dirent.h> |
| 20 | #include <errno.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <unistd.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <stdbool.h> |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 28 | #include <pthread.h> |
| 29 | #include <semaphore.h> |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 30 | #include <cutils/properties.h> |
| 31 | //#define LOG_NDEBUG 0 |
| 32 | |
| 33 | #define LOG_TAG "HiKeyPowerHAL" |
vishal | 7d9fede | 2018-06-05 12:43:23 +0530 | [diff] [blame] | 34 | #include <log/log.h> |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 35 | |
| 36 | #include <hardware/hardware.h> |
| 37 | #include <hardware/power.h> |
| 38 | |
John Stultz | 17e1124 | 2017-03-14 18:14:24 -0700 | [diff] [blame] | 39 | #define SCHEDTUNE_BOOST_PATH "/dev/stune/top-app/schedtune.boost" |
John Stultz | 0e785a2 | 2017-05-23 13:01:56 -0700 | [diff] [blame] | 40 | #define SCHEDTUNE_BOOST_VAL_PROP "ro.config.schetune.touchboost.value" |
| 41 | #define SCHEDTUNE_BOOST_TIME_PROP "ro.config.schetune.touchboost.time_ns" |
| 42 | |
| 43 | #define SCHEDTUNE_BOOST_VAL_DEFAULT "40" |
| 44 | |
| 45 | char schedtune_boost_norm[PROPERTY_VALUE_MAX] = "10"; |
| 46 | char schedtune_boost_interactive[PROPERTY_VALUE_MAX] = SCHEDTUNE_BOOST_VAL_DEFAULT; |
| 47 | long long schedtune_boost_time_ns = 1000000000LL; |
| 48 | |
Leo Yan | f0e78bf | 2017-11-28 22:01:58 +0800 | [diff] [blame] | 49 | #define DEVFREQ_DDR_MIN_FREQ_PATH_PROP \ |
| 50 | "ro.config.devfreq.ddr.min_freq.path" |
| 51 | #define DEVFREQ_DDR_MIN_FREQ_BOOST_PROP \ |
| 52 | "ro.config.devfreq.ddr.min_freq.boost" |
| 53 | |
| 54 | char devfreq_ddr_min_path[PROPERTY_VALUE_MAX]; |
| 55 | char devfreq_ddr_min_orig[PROPERTY_VALUE_MAX]; |
| 56 | char devfreq_ddr_min_boost[PROPERTY_VALUE_MAX]; |
| 57 | |
| 58 | #define DEVFREQ_GPU_MIN_FREQ_PATH_PROP \ |
| 59 | "ro.config.devfreq.gpu.min_freq.path" |
| 60 | #define DEVFREQ_GPU_MIN_FREQ_BOOST_PROP \ |
| 61 | "ro.config.devfreq.gpu.min_freq.boost" |
| 62 | |
| 63 | char devfreq_gpu_min_path[PROPERTY_VALUE_MAX]; |
| 64 | char devfreq_gpu_min_orig[PROPERTY_VALUE_MAX]; |
| 65 | char devfreq_gpu_min_boost[PROPERTY_VALUE_MAX]; |
| 66 | |
John Stultz | 127e1cc | 2016-08-18 12:20:40 -0700 | [diff] [blame] | 67 | #define INTERACTIVE_BOOSTPULSE_PATH "/sys/devices/system/cpu/cpufreq/interactive/boostpulse" |
| 68 | #define INTERACTIVE_IO_IS_BUSY_PATH "/sys/devices/system/cpu/cpufreq/interactive/io_is_busy" |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 69 | |
| 70 | struct hikey_power_module { |
| 71 | struct power_module base; |
| 72 | pthread_mutex_t lock; |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 73 | /* interactive gov boost values */ |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 74 | int boostpulse_fd; |
| 75 | int boostpulse_warned; |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 76 | /* EAS schedtune values */ |
| 77 | int schedtune_boost_fd; |
| 78 | long long deboost_time; |
| 79 | sem_t signal_lock; |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
John Stultz | ed77173 | 2017-05-23 17:39:53 -0700 | [diff] [blame] | 82 | |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 83 | static bool low_power_mode = false; |
| 84 | |
John Stultz | ed77173 | 2017-05-23 17:39:53 -0700 | [diff] [blame] | 85 | |
| 86 | #define CPUFREQ_CLUST_MAX_FREQ_PATH_PROP "ro.config.cpufreq.max_freq.cluster" |
| 87 | #define CPUFREQ_CLUST_LOW_POWER_MAX_FREQ_PROP "ro.config.cpufreq.low_power_max.cluster" |
| 88 | #define CPUFREQ_CLUST0_MAX_FREQ_PATH_DEFAULT "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq" |
| 89 | |
| 90 | #define NR_CLUSTERS 4 |
| 91 | static int max_clusters = 1; |
| 92 | static struct hikey_cpufreq_t { |
| 93 | char path[PROPERTY_VALUE_MAX]; |
| 94 | char normal_max[PROPERTY_VALUE_MAX]; |
| 95 | char low_power_max[PROPERTY_VALUE_MAX]; |
| 96 | } hikey_cpufreq_clusters[NR_CLUSTERS]; |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 97 | |
John Stultz | e150ab3 | 2016-08-18 14:04:34 -0700 | [diff] [blame] | 98 | #define container_of(addr, struct_name, field_name) \ |
| 99 | ((struct_name *)((char *)(addr) - offsetof(struct_name, field_name))) |
| 100 | |
| 101 | |
Dmitry Shmidt | cc14758 | 2016-11-07 13:13:34 -0800 | [diff] [blame] | 102 | static int sysfs_write(const char *path, char *s) |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 103 | { |
| 104 | char buf[80]; |
| 105 | int len; |
| 106 | int fd = open(path, O_WRONLY); |
| 107 | |
| 108 | if (fd < 0) { |
| 109 | strerror_r(errno, buf, sizeof(buf)); |
| 110 | ALOGE("Error opening %s: %s\n", path, buf); |
Dmitry Shmidt | cc14758 | 2016-11-07 13:13:34 -0800 | [diff] [blame] | 111 | return fd; |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | len = write(fd, s, strlen(s)); |
| 115 | if (len < 0) { |
| 116 | strerror_r(errno, buf, sizeof(buf)); |
| 117 | ALOGE("Error writing to %s: %s\n", path, buf); |
| 118 | } |
| 119 | |
| 120 | close(fd); |
Dmitry Shmidt | cc14758 | 2016-11-07 13:13:34 -0800 | [diff] [blame] | 121 | return len; |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 122 | } |
| 123 | |
John Stultz | ed77173 | 2017-05-23 17:39:53 -0700 | [diff] [blame] | 124 | static int sysfs_read(const char *path, char *s, int slen) |
| 125 | { |
| 126 | int len; |
| 127 | int fd = open(path, O_RDONLY); |
| 128 | |
| 129 | if (fd < 0) { |
| 130 | ALOGE("Error opening %s\n", path); |
| 131 | return fd; |
| 132 | } |
| 133 | |
| 134 | len = read(fd, s, slen); |
| 135 | if (len < 0) { |
| 136 | ALOGE("Error reading %s\n", path); |
| 137 | } |
| 138 | |
| 139 | close(fd); |
| 140 | return len; |
| 141 | } |
| 142 | |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 143 | #define NSEC_PER_SEC 1000000000LL |
| 144 | static long long gettime_ns(void) |
| 145 | { |
| 146 | struct timespec ts; |
| 147 | |
| 148 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 149 | return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec; |
| 150 | } |
| 151 | |
| 152 | static void nanosleep_ns(long long ns) |
| 153 | { |
| 154 | struct timespec ts; |
| 155 | ts.tv_sec = ns/NSEC_PER_SEC; |
| 156 | ts.tv_nsec = ns%NSEC_PER_SEC; |
| 157 | nanosleep(&ts, NULL); |
| 158 | } |
| 159 | |
John Stultz | c579371 | 2016-08-18 14:00:49 -0700 | [diff] [blame] | 160 | /*[interactive cpufreq gov funcs]*********************************************/ |
John Stultz | e150ab3 | 2016-08-18 14:04:34 -0700 | [diff] [blame] | 161 | static void interactive_power_init(struct hikey_power_module __unused *hikey) |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 162 | { |
Dmitry Shmidt | cc14758 | 2016-11-07 13:13:34 -0800 | [diff] [blame] | 163 | if (sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/timer_rate", |
| 164 | "20000") < 0) |
| 165 | return; |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 166 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/timer_slack", |
| 167 | "20000"); |
| 168 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/min_sample_time", |
| 169 | "80000"); |
| 170 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/hispeed_freq", |
| 171 | "1200000"); |
| 172 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/go_hispeed_load", |
| 173 | "99"); |
| 174 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/target_loads", |
| 175 | "65 729000:75 960000:85"); |
| 176 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/above_hispeed_delay", |
| 177 | "20000"); |
| 178 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/boostpulse_duration", |
| 179 | "1000000"); |
| 180 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/io_is_busy", "0"); |
| 181 | |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 182 | } |
| 183 | |
John Stultz | 5e1546b | 2016-08-18 11:56:31 -0700 | [diff] [blame] | 184 | static int interactive_boostpulse(struct hikey_power_module *hikey) |
| 185 | { |
| 186 | char buf[80]; |
| 187 | int len; |
| 188 | |
John Stultz | 64b54ca | 2016-08-18 15:01:33 -0700 | [diff] [blame] | 189 | if (hikey->boostpulse_fd < 0) |
John Stultz | 127e1cc | 2016-08-18 12:20:40 -0700 | [diff] [blame] | 190 | hikey->boostpulse_fd = open(INTERACTIVE_BOOSTPULSE_PATH, O_WRONLY); |
John Stultz | 5e1546b | 2016-08-18 11:56:31 -0700 | [diff] [blame] | 191 | |
John Stultz | 64b54ca | 2016-08-18 15:01:33 -0700 | [diff] [blame] | 192 | if (hikey->boostpulse_fd < 0) { |
| 193 | if (!hikey->boostpulse_warned) { |
John Stultz | 5e1546b | 2016-08-18 11:56:31 -0700 | [diff] [blame] | 194 | strerror_r(errno, buf, sizeof(buf)); |
John Stultz | 127e1cc | 2016-08-18 12:20:40 -0700 | [diff] [blame] | 195 | ALOGE("Error opening %s: %s\n", INTERACTIVE_BOOSTPULSE_PATH, |
John Stultz | 64b54ca | 2016-08-18 15:01:33 -0700 | [diff] [blame] | 196 | buf); |
| 197 | hikey->boostpulse_warned = 1; |
John Stultz | 5e1546b | 2016-08-18 11:56:31 -0700 | [diff] [blame] | 198 | } |
John Stultz | 64b54ca | 2016-08-18 15:01:33 -0700 | [diff] [blame] | 199 | return hikey->boostpulse_fd; |
| 200 | } |
| 201 | |
| 202 | len = write(hikey->boostpulse_fd, "1", 1); |
| 203 | if (len < 0) { |
| 204 | strerror_r(errno, buf, sizeof(buf)); |
| 205 | ALOGE("Error writing to %s: %s\n", |
John Stultz | 127e1cc | 2016-08-18 12:20:40 -0700 | [diff] [blame] | 206 | INTERACTIVE_BOOSTPULSE_PATH, buf); |
John Stultz | 64b54ca | 2016-08-18 15:01:33 -0700 | [diff] [blame] | 207 | return -1; |
John Stultz | 5e1546b | 2016-08-18 11:56:31 -0700 | [diff] [blame] | 208 | } |
| 209 | return 0; |
| 210 | } |
| 211 | |
Leo Yan | f0e78bf | 2017-11-28 22:01:58 +0800 | [diff] [blame] | 212 | static void |
| 213 | hikey_devfreq_set_interactive(struct hikey_power_module __unused *hikey, int on) |
| 214 | { |
| 215 | if (!on || low_power_mode) { |
| 216 | if (devfreq_ddr_min_path[0] != '\0') |
| 217 | sysfs_write(devfreq_ddr_min_path, devfreq_ddr_min_orig); |
| 218 | |
| 219 | if (devfreq_gpu_min_path[0] != '\0') |
| 220 | sysfs_write(devfreq_gpu_min_path, devfreq_gpu_min_orig); |
| 221 | } else { |
| 222 | if (devfreq_ddr_min_path[0] != '\0') |
| 223 | sysfs_write(devfreq_ddr_min_path, devfreq_ddr_min_boost); |
| 224 | |
| 225 | if (devfreq_gpu_min_path[0] != '\0') |
| 226 | sysfs_write(devfreq_gpu_min_path, devfreq_gpu_min_boost); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | static void hikey_devfreq_init(struct hikey_power_module __unused *hikey) |
| 231 | { |
| 232 | property_get(DEVFREQ_DDR_MIN_FREQ_PATH_PROP, devfreq_ddr_min_path, ""); |
| 233 | if (devfreq_ddr_min_path[0] != '\0') { |
| 234 | sysfs_read(devfreq_ddr_min_path, devfreq_ddr_min_orig, |
| 235 | PROPERTY_VALUE_MAX); |
| 236 | property_get(DEVFREQ_DDR_MIN_FREQ_BOOST_PROP, |
| 237 | devfreq_ddr_min_boost, ""); |
| 238 | } |
| 239 | |
| 240 | property_get(DEVFREQ_GPU_MIN_FREQ_PATH_PROP, devfreq_gpu_min_path, ""); |
| 241 | if (devfreq_gpu_min_path[0] != '\0') { |
| 242 | sysfs_read(devfreq_gpu_min_path, devfreq_gpu_min_orig, |
| 243 | PROPERTY_VALUE_MAX); |
| 244 | property_get(DEVFREQ_GPU_MIN_FREQ_BOOST_PROP, |
| 245 | devfreq_gpu_min_boost, ""); |
| 246 | } |
| 247 | } |
| 248 | |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 249 | /*[schedtune functions]*******************************************************/ |
John Stultz | c579371 | 2016-08-18 14:00:49 -0700 | [diff] [blame] | 250 | |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 251 | int schedtune_sysfs_boost(struct hikey_power_module *hikey, char* booststr) |
| 252 | { |
| 253 | char buf[80]; |
| 254 | int len; |
| 255 | |
| 256 | if (hikey->schedtune_boost_fd < 0) |
| 257 | return hikey->schedtune_boost_fd; |
| 258 | |
John Stultz | 0e785a2 | 2017-05-23 13:01:56 -0700 | [diff] [blame] | 259 | len = write(hikey->schedtune_boost_fd, booststr, strlen(booststr)); |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 260 | if (len < 0) { |
| 261 | strerror_r(errno, buf, sizeof(buf)); |
| 262 | ALOGE("Error writing to %s: %s\n", SCHEDTUNE_BOOST_PATH, buf); |
| 263 | } |
| 264 | return len; |
| 265 | } |
| 266 | |
| 267 | static void* schedtune_deboost_thread(void* arg) |
| 268 | { |
| 269 | struct hikey_power_module *hikey = (struct hikey_power_module *)arg; |
| 270 | |
| 271 | while(1) { |
| 272 | sem_wait(&hikey->signal_lock); |
| 273 | while(1) { |
| 274 | long long now, sleeptime = 0; |
| 275 | |
| 276 | pthread_mutex_lock(&hikey->lock); |
| 277 | now = gettime_ns(); |
| 278 | if (hikey->deboost_time > now) { |
| 279 | sleeptime = hikey->deboost_time - now; |
| 280 | pthread_mutex_unlock(&hikey->lock); |
| 281 | nanosleep_ns(sleeptime); |
| 282 | continue; |
| 283 | } |
| 284 | |
John Stultz | 0e785a2 | 2017-05-23 13:01:56 -0700 | [diff] [blame] | 285 | schedtune_sysfs_boost(hikey, schedtune_boost_norm); |
Leo Yan | f0e78bf | 2017-11-28 22:01:58 +0800 | [diff] [blame] | 286 | hikey_devfreq_set_interactive(hikey, 0); |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 287 | hikey->deboost_time = 0; |
| 288 | pthread_mutex_unlock(&hikey->lock); |
| 289 | break; |
| 290 | } |
| 291 | } |
| 292 | return NULL; |
| 293 | } |
| 294 | |
| 295 | static int schedtune_boost(struct hikey_power_module *hikey) |
| 296 | { |
| 297 | long long now; |
| 298 | |
| 299 | if (hikey->schedtune_boost_fd < 0) |
| 300 | return hikey->schedtune_boost_fd; |
| 301 | |
| 302 | now = gettime_ns(); |
| 303 | if (!hikey->deboost_time) { |
John Stultz | 0e785a2 | 2017-05-23 13:01:56 -0700 | [diff] [blame] | 304 | schedtune_sysfs_boost(hikey, schedtune_boost_interactive); |
Leo Yan | f0e78bf | 2017-11-28 22:01:58 +0800 | [diff] [blame] | 305 | hikey_devfreq_set_interactive(hikey, 1); |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 306 | sem_post(&hikey->signal_lock); |
| 307 | } |
John Stultz | 0e785a2 | 2017-05-23 13:01:56 -0700 | [diff] [blame] | 308 | hikey->deboost_time = now + schedtune_boost_time_ns; |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static void schedtune_power_init(struct hikey_power_module *hikey) |
| 314 | { |
| 315 | char buf[50]; |
| 316 | pthread_t tid; |
| 317 | |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 318 | hikey->deboost_time = 0; |
| 319 | sem_init(&hikey->signal_lock, 0, 1); |
| 320 | |
John Stultz | 0e785a2 | 2017-05-23 13:01:56 -0700 | [diff] [blame] | 321 | hikey->schedtune_boost_fd = open(SCHEDTUNE_BOOST_PATH, O_RDWR); |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 322 | if (hikey->schedtune_boost_fd < 0) { |
| 323 | strerror_r(errno, buf, sizeof(buf)); |
| 324 | ALOGE("Error opening %s: %s\n", SCHEDTUNE_BOOST_PATH, buf); |
John Stultz | 0e785a2 | 2017-05-23 13:01:56 -0700 | [diff] [blame] | 325 | return; |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 326 | } |
| 327 | |
John Stultz | 0e785a2 | 2017-05-23 13:01:56 -0700 | [diff] [blame] | 328 | schedtune_boost_time_ns = property_get_int64(SCHEDTUNE_BOOST_TIME_PROP, |
| 329 | 1000000000LL); |
| 330 | property_get(SCHEDTUNE_BOOST_VAL_PROP, schedtune_boost_interactive, |
| 331 | SCHEDTUNE_BOOST_VAL_DEFAULT); |
| 332 | |
| 333 | if (hikey->schedtune_boost_fd >= 0) { |
| 334 | size_t len = read(hikey->schedtune_boost_fd, schedtune_boost_norm, |
| 335 | PROPERTY_VALUE_MAX); |
| 336 | if (len <= 0) |
| 337 | ALOGE("Error reading normal boost value\n"); |
| 338 | else if (schedtune_boost_norm[len] == '\n') |
| 339 | schedtune_boost_norm[len] = '\0'; |
| 340 | |
| 341 | } |
| 342 | |
| 343 | ALOGV("Starting with schedtune boost norm: %s touchboost: %s and boosttime: %lld\n", |
| 344 | schedtune_boost_norm, schedtune_boost_interactive, schedtune_boost_time_ns); |
| 345 | |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 346 | pthread_create(&tid, NULL, schedtune_deboost_thread, hikey); |
| 347 | } |
| 348 | |
| 349 | /*[generic functions]*********************************************************/ |
John Stultz | ed77173 | 2017-05-23 17:39:53 -0700 | [diff] [blame] | 350 | |
| 351 | static void hikey_cpufreq_set_interactive(struct power_module __unused *module, int on) |
| 352 | { |
| 353 | int i; |
| 354 | |
| 355 | /* |
| 356 | * Lower maximum frequency when screen is off. |
| 357 | */ |
| 358 | for (i=0; i < max_clusters; i++) { |
| 359 | if ((!on || low_power_mode) && hikey_cpufreq_clusters[i].low_power_max[0] != '\0') |
| 360 | sysfs_write(hikey_cpufreq_clusters[i].path, hikey_cpufreq_clusters[i].low_power_max); |
| 361 | else |
| 362 | sysfs_write(hikey_cpufreq_clusters[i].path, hikey_cpufreq_clusters[i].normal_max); |
| 363 | } |
| 364 | sysfs_write(INTERACTIVE_IO_IS_BUSY_PATH, on ? "1" : "0"); |
| 365 | } |
| 366 | |
| 367 | |
Dmitry Shmidt | b4db15e | 2017-10-24 16:02:26 -0700 | [diff] [blame] | 368 | static void hikey_cpufreq_init(struct hikey_power_module __unused *hikey) |
John Stultz | ed77173 | 2017-05-23 17:39:53 -0700 | [diff] [blame] | 369 | { |
| 370 | char buf[128]; |
| 371 | int len, i; |
| 372 | |
| 373 | for (i=0; i < NR_CLUSTERS; i++) { |
| 374 | sprintf(buf,"%s%d", CPUFREQ_CLUST_MAX_FREQ_PATH_PROP, i); |
| 375 | property_get(buf, hikey_cpufreq_clusters[i].path, ""); |
| 376 | |
| 377 | if (hikey_cpufreq_clusters[i].path[0] == '\0') { |
| 378 | if (i == 0) { |
| 379 | /* In case no property was set, pick cpu0's cluster */ |
| 380 | strncpy(hikey_cpufreq_clusters[i].path, |
| 381 | CPUFREQ_CLUST0_MAX_FREQ_PATH_DEFAULT, |
| 382 | PROPERTY_VALUE_MAX); |
| 383 | } else |
| 384 | break; |
| 385 | } |
| 386 | sprintf(buf,"%s%d", CPUFREQ_CLUST_LOW_POWER_MAX_FREQ_PROP, i); |
| 387 | property_get(buf, hikey_cpufreq_clusters[i].low_power_max, ""); |
| 388 | len = sysfs_read(hikey_cpufreq_clusters[i].path, |
| 389 | hikey_cpufreq_clusters[i].normal_max, |
| 390 | PROPERTY_VALUE_MAX); |
| 391 | ALOGV("Cluster: %d path: %s low: %s norm: %s\n", i, |
| 392 | hikey_cpufreq_clusters[i].path, |
| 393 | hikey_cpufreq_clusters[i].low_power_max, |
| 394 | hikey_cpufreq_clusters[i].normal_max); |
| 395 | } |
| 396 | max_clusters = i; |
| 397 | } |
| 398 | |
John Stultz | c579371 | 2016-08-18 14:00:49 -0700 | [diff] [blame] | 399 | static void hikey_power_init(struct power_module __unused *module) |
| 400 | { |
John Stultz | e150ab3 | 2016-08-18 14:04:34 -0700 | [diff] [blame] | 401 | struct hikey_power_module *hikey = container_of(module, |
| 402 | struct hikey_power_module, base); |
John Stultz | ed77173 | 2017-05-23 17:39:53 -0700 | [diff] [blame] | 403 | hikey_cpufreq_init(hikey); |
Leo Yan | f0e78bf | 2017-11-28 22:01:58 +0800 | [diff] [blame] | 404 | hikey_devfreq_init(hikey); |
John Stultz | e150ab3 | 2016-08-18 14:04:34 -0700 | [diff] [blame] | 405 | interactive_power_init(hikey); |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 406 | schedtune_power_init(hikey); |
| 407 | } |
| 408 | |
| 409 | static void hikey_hint_interaction(struct hikey_power_module *mod) |
| 410 | { |
| 411 | /* Try interactive cpufreq boosting first */ |
| 412 | if(!interactive_boostpulse(mod)) |
| 413 | return; |
| 414 | /* Then try EAS schedtune boosting */ |
| 415 | if(!schedtune_boost(mod)) |
| 416 | return; |
John Stultz | c579371 | 2016-08-18 14:00:49 -0700 | [diff] [blame] | 417 | } |
| 418 | |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 419 | static void hikey_power_hint(struct power_module *module, power_hint_t hint, |
| 420 | void *data) |
| 421 | { |
John Stultz | e150ab3 | 2016-08-18 14:04:34 -0700 | [diff] [blame] | 422 | struct hikey_power_module *hikey = container_of(module, |
| 423 | struct hikey_power_module, base); |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 424 | |
| 425 | pthread_mutex_lock(&hikey->lock); |
| 426 | switch (hint) { |
| 427 | case POWER_HINT_INTERACTION: |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 428 | hikey_hint_interaction(hikey); |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 429 | break; |
| 430 | |
| 431 | case POWER_HINT_VSYNC: |
| 432 | break; |
| 433 | |
| 434 | case POWER_HINT_LOW_POWER: |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 435 | low_power_mode = data; |
John Stultz | ed77173 | 2017-05-23 17:39:53 -0700 | [diff] [blame] | 436 | hikey_cpufreq_set_interactive(module, 1); |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 437 | break; |
| 438 | |
| 439 | default: |
| 440 | break; |
| 441 | } |
| 442 | pthread_mutex_unlock(&hikey->lock); |
| 443 | } |
| 444 | |
Leo Yan | 0bf3fb2 | 2017-12-14 11:24:02 +0800 | [diff] [blame] | 445 | static void set_feature(struct power_module __unused *module, |
| 446 | feature_t feature, int state) |
John Stultz | ab1f6a7 | 2016-08-18 12:28:55 -0700 | [diff] [blame] | 447 | { |
John Stultz | ab1f6a7 | 2016-08-18 12:28:55 -0700 | [diff] [blame] | 448 | switch (feature) { |
| 449 | default: |
Dmitry Shmidt | 0ea8f4d | 2016-11-07 12:55:16 -0800 | [diff] [blame] | 450 | ALOGW("Error setting the feature %d and state %d, it doesn't exist\n", |
| 451 | feature, state); |
John Stultz | ab1f6a7 | 2016-08-18 12:28:55 -0700 | [diff] [blame] | 452 | break; |
| 453 | } |
| 454 | } |
| 455 | |
Dmitry Shmidt | 0ea8f4d | 2016-11-07 12:55:16 -0800 | [diff] [blame] | 456 | static int power_open(const hw_module_t* __unused module, const char* name, |
Ruchi Kandoi | 96c621c | 2016-11-03 12:46:10 -0700 | [diff] [blame] | 457 | hw_device_t** device) |
| 458 | { |
| 459 | int retval = 0; /* 0 is ok; -1 is error */ |
| 460 | ALOGD("%s: enter; name=%s", __FUNCTION__, name); |
| 461 | |
| 462 | if (strcmp(name, POWER_HARDWARE_MODULE_ID) == 0) { |
John Stultz | 5af9bdc | 2017-11-02 21:23:22 -0700 | [diff] [blame] | 463 | struct hikey_power_module *dev = (struct hikey_power_module *)calloc(1, |
| 464 | sizeof(struct hikey_power_module)); |
Ruchi Kandoi | 96c621c | 2016-11-03 12:46:10 -0700 | [diff] [blame] | 465 | |
| 466 | if (dev) { |
| 467 | /* Common hw_device_t fields */ |
John Stultz | 5af9bdc | 2017-11-02 21:23:22 -0700 | [diff] [blame] | 468 | dev->base.common.tag = HARDWARE_DEVICE_TAG; |
| 469 | dev->base.common.module_api_version = POWER_MODULE_API_VERSION_0_5; |
| 470 | dev->base.common.hal_api_version = HARDWARE_HAL_API_VERSION; |
Ruchi Kandoi | 96c621c | 2016-11-03 12:46:10 -0700 | [diff] [blame] | 471 | |
John Stultz | 5af9bdc | 2017-11-02 21:23:22 -0700 | [diff] [blame] | 472 | dev->base.init = hikey_power_init; |
| 473 | dev->base.powerHint = hikey_power_hint; |
| 474 | dev->base.setInteractive = hikey_cpufreq_set_interactive; |
| 475 | dev->base.setFeature = set_feature; |
Ruchi Kandoi | 96c621c | 2016-11-03 12:46:10 -0700 | [diff] [blame] | 476 | |
John Stultz | 5af9bdc | 2017-11-02 21:23:22 -0700 | [diff] [blame] | 477 | pthread_mutex_init(&dev->lock, NULL); |
| 478 | dev->boostpulse_fd = -1; |
| 479 | dev->boostpulse_warned = 0; |
| 480 | |
| 481 | *device = (hw_device_t*)&dev->base; |
Ruchi Kandoi | 96c621c | 2016-11-03 12:46:10 -0700 | [diff] [blame] | 482 | } else |
| 483 | retval = -ENOMEM; |
| 484 | } else { |
| 485 | retval = -EINVAL; |
| 486 | } |
| 487 | |
| 488 | ALOGD("%s: exit %d", __FUNCTION__, retval); |
| 489 | return retval; |
| 490 | } |
| 491 | |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 492 | static struct hw_module_methods_t power_module_methods = { |
Ruchi Kandoi | 96c621c | 2016-11-03 12:46:10 -0700 | [diff] [blame] | 493 | .open = power_open, |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 494 | }; |
| 495 | |
| 496 | struct hikey_power_module HAL_MODULE_INFO_SYM = { |
Dmitry Shmidt | 0ea8f4d | 2016-11-07 12:55:16 -0800 | [diff] [blame] | 497 | .base = { |
| 498 | .common = { |
| 499 | .tag = HARDWARE_MODULE_TAG, |
| 500 | .module_api_version = POWER_MODULE_API_VERSION_0_2, |
| 501 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
| 502 | .id = POWER_HARDWARE_MODULE_ID, |
| 503 | .name = "HiKey Power HAL", |
| 504 | .author = "The Android Open Source Project", |
| 505 | .methods = &power_module_methods, |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 506 | }, |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 507 | }, |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 508 | }; |