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" |
| 34 | #include <utils/Log.h> |
| 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 | |
John Stultz | 127e1cc | 2016-08-18 12:20:40 -0700 | [diff] [blame] | 49 | #define INTERACTIVE_BOOSTPULSE_PATH "/sys/devices/system/cpu/cpufreq/interactive/boostpulse" |
| 50 | #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] | 51 | |
| 52 | struct hikey_power_module { |
| 53 | struct power_module base; |
| 54 | pthread_mutex_t lock; |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 55 | /* interactive gov boost values */ |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 56 | int boostpulse_fd; |
| 57 | int boostpulse_warned; |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 58 | /* EAS schedtune values */ |
| 59 | int schedtune_boost_fd; |
| 60 | long long deboost_time; |
| 61 | sem_t signal_lock; |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
John Stultz | ed77173 | 2017-05-23 17:39:53 -0700 | [diff] [blame] | 64 | |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 65 | static bool low_power_mode = false; |
| 66 | |
John Stultz | ed77173 | 2017-05-23 17:39:53 -0700 | [diff] [blame] | 67 | |
| 68 | #define CPUFREQ_CLUST_MAX_FREQ_PATH_PROP "ro.config.cpufreq.max_freq.cluster" |
| 69 | #define CPUFREQ_CLUST_LOW_POWER_MAX_FREQ_PROP "ro.config.cpufreq.low_power_max.cluster" |
| 70 | #define CPUFREQ_CLUST0_MAX_FREQ_PATH_DEFAULT "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq" |
| 71 | |
| 72 | #define NR_CLUSTERS 4 |
| 73 | static int max_clusters = 1; |
| 74 | static struct hikey_cpufreq_t { |
| 75 | char path[PROPERTY_VALUE_MAX]; |
| 76 | char normal_max[PROPERTY_VALUE_MAX]; |
| 77 | char low_power_max[PROPERTY_VALUE_MAX]; |
| 78 | } hikey_cpufreq_clusters[NR_CLUSTERS]; |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 79 | |
John Stultz | e150ab3 | 2016-08-18 14:04:34 -0700 | [diff] [blame] | 80 | |
| 81 | #define container_of(addr, struct_name, field_name) \ |
| 82 | ((struct_name *)((char *)(addr) - offsetof(struct_name, field_name))) |
| 83 | |
| 84 | |
Dmitry Shmidt | cc14758 | 2016-11-07 13:13:34 -0800 | [diff] [blame] | 85 | static int sysfs_write(const char *path, char *s) |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 86 | { |
| 87 | char buf[80]; |
| 88 | int len; |
| 89 | int fd = open(path, O_WRONLY); |
| 90 | |
| 91 | if (fd < 0) { |
| 92 | strerror_r(errno, buf, sizeof(buf)); |
| 93 | ALOGE("Error opening %s: %s\n", path, buf); |
Dmitry Shmidt | cc14758 | 2016-11-07 13:13:34 -0800 | [diff] [blame] | 94 | return fd; |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | len = write(fd, s, strlen(s)); |
| 98 | if (len < 0) { |
| 99 | strerror_r(errno, buf, sizeof(buf)); |
| 100 | ALOGE("Error writing to %s: %s\n", path, buf); |
| 101 | } |
| 102 | |
| 103 | close(fd); |
Dmitry Shmidt | cc14758 | 2016-11-07 13:13:34 -0800 | [diff] [blame] | 104 | return len; |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 105 | } |
| 106 | |
John Stultz | ed77173 | 2017-05-23 17:39:53 -0700 | [diff] [blame] | 107 | static int sysfs_read(const char *path, char *s, int slen) |
| 108 | { |
| 109 | int len; |
| 110 | int fd = open(path, O_RDONLY); |
| 111 | |
| 112 | if (fd < 0) { |
| 113 | ALOGE("Error opening %s\n", path); |
| 114 | return fd; |
| 115 | } |
| 116 | |
| 117 | len = read(fd, s, slen); |
| 118 | if (len < 0) { |
| 119 | ALOGE("Error reading %s\n", path); |
| 120 | } |
| 121 | |
| 122 | close(fd); |
| 123 | return len; |
| 124 | } |
| 125 | |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 126 | #define NSEC_PER_SEC 1000000000LL |
| 127 | static long long gettime_ns(void) |
| 128 | { |
| 129 | struct timespec ts; |
| 130 | |
| 131 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 132 | return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec; |
| 133 | } |
| 134 | |
| 135 | static void nanosleep_ns(long long ns) |
| 136 | { |
| 137 | struct timespec ts; |
| 138 | ts.tv_sec = ns/NSEC_PER_SEC; |
| 139 | ts.tv_nsec = ns%NSEC_PER_SEC; |
| 140 | nanosleep(&ts, NULL); |
| 141 | } |
| 142 | |
John Stultz | c579371 | 2016-08-18 14:00:49 -0700 | [diff] [blame] | 143 | /*[interactive cpufreq gov funcs]*********************************************/ |
John Stultz | e150ab3 | 2016-08-18 14:04:34 -0700 | [diff] [blame] | 144 | static void interactive_power_init(struct hikey_power_module __unused *hikey) |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 145 | { |
Dmitry Shmidt | cc14758 | 2016-11-07 13:13:34 -0800 | [diff] [blame] | 146 | if (sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/timer_rate", |
| 147 | "20000") < 0) |
| 148 | return; |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 149 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/timer_slack", |
| 150 | "20000"); |
| 151 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/min_sample_time", |
| 152 | "80000"); |
| 153 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/hispeed_freq", |
| 154 | "1200000"); |
| 155 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/go_hispeed_load", |
| 156 | "99"); |
| 157 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/target_loads", |
| 158 | "65 729000:75 960000:85"); |
| 159 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/above_hispeed_delay", |
| 160 | "20000"); |
| 161 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/boostpulse_duration", |
| 162 | "1000000"); |
| 163 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/io_is_busy", "0"); |
| 164 | |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 165 | } |
| 166 | |
John Stultz | 5e1546b | 2016-08-18 11:56:31 -0700 | [diff] [blame] | 167 | static int interactive_boostpulse(struct hikey_power_module *hikey) |
| 168 | { |
| 169 | char buf[80]; |
| 170 | int len; |
| 171 | |
John Stultz | 64b54ca | 2016-08-18 15:01:33 -0700 | [diff] [blame] | 172 | if (hikey->boostpulse_fd < 0) |
John Stultz | 127e1cc | 2016-08-18 12:20:40 -0700 | [diff] [blame] | 173 | hikey->boostpulse_fd = open(INTERACTIVE_BOOSTPULSE_PATH, O_WRONLY); |
John Stultz | 5e1546b | 2016-08-18 11:56:31 -0700 | [diff] [blame] | 174 | |
John Stultz | 64b54ca | 2016-08-18 15:01:33 -0700 | [diff] [blame] | 175 | if (hikey->boostpulse_fd < 0) { |
| 176 | if (!hikey->boostpulse_warned) { |
John Stultz | 5e1546b | 2016-08-18 11:56:31 -0700 | [diff] [blame] | 177 | strerror_r(errno, buf, sizeof(buf)); |
John Stultz | 127e1cc | 2016-08-18 12:20:40 -0700 | [diff] [blame] | 178 | ALOGE("Error opening %s: %s\n", INTERACTIVE_BOOSTPULSE_PATH, |
John Stultz | 64b54ca | 2016-08-18 15:01:33 -0700 | [diff] [blame] | 179 | buf); |
| 180 | hikey->boostpulse_warned = 1; |
John Stultz | 5e1546b | 2016-08-18 11:56:31 -0700 | [diff] [blame] | 181 | } |
John Stultz | 64b54ca | 2016-08-18 15:01:33 -0700 | [diff] [blame] | 182 | return hikey->boostpulse_fd; |
| 183 | } |
| 184 | |
| 185 | len = write(hikey->boostpulse_fd, "1", 1); |
| 186 | if (len < 0) { |
| 187 | strerror_r(errno, buf, sizeof(buf)); |
| 188 | ALOGE("Error writing to %s: %s\n", |
John Stultz | 127e1cc | 2016-08-18 12:20:40 -0700 | [diff] [blame] | 189 | INTERACTIVE_BOOSTPULSE_PATH, buf); |
John Stultz | 64b54ca | 2016-08-18 15:01:33 -0700 | [diff] [blame] | 190 | return -1; |
John Stultz | 5e1546b | 2016-08-18 11:56:31 -0700 | [diff] [blame] | 191 | } |
| 192 | return 0; |
| 193 | } |
| 194 | |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 195 | /*[schedtune functions]*******************************************************/ |
John Stultz | c579371 | 2016-08-18 14:00:49 -0700 | [diff] [blame] | 196 | |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 197 | int schedtune_sysfs_boost(struct hikey_power_module *hikey, char* booststr) |
| 198 | { |
| 199 | char buf[80]; |
| 200 | int len; |
| 201 | |
| 202 | if (hikey->schedtune_boost_fd < 0) |
| 203 | return hikey->schedtune_boost_fd; |
| 204 | |
John Stultz | 0e785a2 | 2017-05-23 13:01:56 -0700 | [diff] [blame] | 205 | len = write(hikey->schedtune_boost_fd, booststr, strlen(booststr)); |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 206 | if (len < 0) { |
| 207 | strerror_r(errno, buf, sizeof(buf)); |
| 208 | ALOGE("Error writing to %s: %s\n", SCHEDTUNE_BOOST_PATH, buf); |
| 209 | } |
| 210 | return len; |
| 211 | } |
| 212 | |
| 213 | static void* schedtune_deboost_thread(void* arg) |
| 214 | { |
| 215 | struct hikey_power_module *hikey = (struct hikey_power_module *)arg; |
| 216 | |
| 217 | while(1) { |
| 218 | sem_wait(&hikey->signal_lock); |
| 219 | while(1) { |
| 220 | long long now, sleeptime = 0; |
| 221 | |
| 222 | pthread_mutex_lock(&hikey->lock); |
| 223 | now = gettime_ns(); |
| 224 | if (hikey->deboost_time > now) { |
| 225 | sleeptime = hikey->deboost_time - now; |
| 226 | pthread_mutex_unlock(&hikey->lock); |
| 227 | nanosleep_ns(sleeptime); |
| 228 | continue; |
| 229 | } |
| 230 | |
John Stultz | 0e785a2 | 2017-05-23 13:01:56 -0700 | [diff] [blame] | 231 | schedtune_sysfs_boost(hikey, schedtune_boost_norm); |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 232 | hikey->deboost_time = 0; |
| 233 | pthread_mutex_unlock(&hikey->lock); |
| 234 | break; |
| 235 | } |
| 236 | } |
| 237 | return NULL; |
| 238 | } |
| 239 | |
| 240 | static int schedtune_boost(struct hikey_power_module *hikey) |
| 241 | { |
| 242 | long long now; |
| 243 | |
| 244 | if (hikey->schedtune_boost_fd < 0) |
| 245 | return hikey->schedtune_boost_fd; |
| 246 | |
| 247 | now = gettime_ns(); |
| 248 | if (!hikey->deboost_time) { |
John Stultz | 0e785a2 | 2017-05-23 13:01:56 -0700 | [diff] [blame] | 249 | schedtune_sysfs_boost(hikey, schedtune_boost_interactive); |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 250 | sem_post(&hikey->signal_lock); |
| 251 | } |
John Stultz | 0e785a2 | 2017-05-23 13:01:56 -0700 | [diff] [blame] | 252 | hikey->deboost_time = now + schedtune_boost_time_ns; |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static void schedtune_power_init(struct hikey_power_module *hikey) |
| 258 | { |
| 259 | char buf[50]; |
| 260 | pthread_t tid; |
| 261 | |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 262 | hikey->deboost_time = 0; |
| 263 | sem_init(&hikey->signal_lock, 0, 1); |
| 264 | |
John Stultz | 0e785a2 | 2017-05-23 13:01:56 -0700 | [diff] [blame] | 265 | hikey->schedtune_boost_fd = open(SCHEDTUNE_BOOST_PATH, O_RDWR); |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 266 | if (hikey->schedtune_boost_fd < 0) { |
| 267 | strerror_r(errno, buf, sizeof(buf)); |
| 268 | ALOGE("Error opening %s: %s\n", SCHEDTUNE_BOOST_PATH, buf); |
John Stultz | 0e785a2 | 2017-05-23 13:01:56 -0700 | [diff] [blame] | 269 | return; |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 270 | } |
| 271 | |
John Stultz | 0e785a2 | 2017-05-23 13:01:56 -0700 | [diff] [blame] | 272 | schedtune_boost_time_ns = property_get_int64(SCHEDTUNE_BOOST_TIME_PROP, |
| 273 | 1000000000LL); |
| 274 | property_get(SCHEDTUNE_BOOST_VAL_PROP, schedtune_boost_interactive, |
| 275 | SCHEDTUNE_BOOST_VAL_DEFAULT); |
| 276 | |
| 277 | if (hikey->schedtune_boost_fd >= 0) { |
| 278 | size_t len = read(hikey->schedtune_boost_fd, schedtune_boost_norm, |
| 279 | PROPERTY_VALUE_MAX); |
| 280 | if (len <= 0) |
| 281 | ALOGE("Error reading normal boost value\n"); |
| 282 | else if (schedtune_boost_norm[len] == '\n') |
| 283 | schedtune_boost_norm[len] = '\0'; |
| 284 | |
| 285 | } |
| 286 | |
| 287 | ALOGV("Starting with schedtune boost norm: %s touchboost: %s and boosttime: %lld\n", |
| 288 | schedtune_boost_norm, schedtune_boost_interactive, schedtune_boost_time_ns); |
| 289 | |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 290 | pthread_create(&tid, NULL, schedtune_deboost_thread, hikey); |
| 291 | } |
| 292 | |
| 293 | /*[generic functions]*********************************************************/ |
John Stultz | ed77173 | 2017-05-23 17:39:53 -0700 | [diff] [blame] | 294 | |
| 295 | static void hikey_cpufreq_set_interactive(struct power_module __unused *module, int on) |
| 296 | { |
| 297 | int i; |
| 298 | |
| 299 | /* |
| 300 | * Lower maximum frequency when screen is off. |
| 301 | */ |
| 302 | for (i=0; i < max_clusters; i++) { |
| 303 | if ((!on || low_power_mode) && hikey_cpufreq_clusters[i].low_power_max[0] != '\0') |
| 304 | sysfs_write(hikey_cpufreq_clusters[i].path, hikey_cpufreq_clusters[i].low_power_max); |
| 305 | else |
| 306 | sysfs_write(hikey_cpufreq_clusters[i].path, hikey_cpufreq_clusters[i].normal_max); |
| 307 | } |
| 308 | sysfs_write(INTERACTIVE_IO_IS_BUSY_PATH, on ? "1" : "0"); |
| 309 | } |
| 310 | |
| 311 | |
Dmitry Shmidt | b4db15e | 2017-10-24 16:02:26 -0700 | [diff] [blame] | 312 | static void hikey_cpufreq_init(struct hikey_power_module __unused *hikey) |
John Stultz | ed77173 | 2017-05-23 17:39:53 -0700 | [diff] [blame] | 313 | { |
| 314 | char buf[128]; |
| 315 | int len, i; |
| 316 | |
| 317 | for (i=0; i < NR_CLUSTERS; i++) { |
| 318 | sprintf(buf,"%s%d", CPUFREQ_CLUST_MAX_FREQ_PATH_PROP, i); |
| 319 | property_get(buf, hikey_cpufreq_clusters[i].path, ""); |
| 320 | |
| 321 | if (hikey_cpufreq_clusters[i].path[0] == '\0') { |
| 322 | if (i == 0) { |
| 323 | /* In case no property was set, pick cpu0's cluster */ |
| 324 | strncpy(hikey_cpufreq_clusters[i].path, |
| 325 | CPUFREQ_CLUST0_MAX_FREQ_PATH_DEFAULT, |
| 326 | PROPERTY_VALUE_MAX); |
| 327 | } else |
| 328 | break; |
| 329 | } |
| 330 | sprintf(buf,"%s%d", CPUFREQ_CLUST_LOW_POWER_MAX_FREQ_PROP, i); |
| 331 | property_get(buf, hikey_cpufreq_clusters[i].low_power_max, ""); |
| 332 | len = sysfs_read(hikey_cpufreq_clusters[i].path, |
| 333 | hikey_cpufreq_clusters[i].normal_max, |
| 334 | PROPERTY_VALUE_MAX); |
| 335 | ALOGV("Cluster: %d path: %s low: %s norm: %s\n", i, |
| 336 | hikey_cpufreq_clusters[i].path, |
| 337 | hikey_cpufreq_clusters[i].low_power_max, |
| 338 | hikey_cpufreq_clusters[i].normal_max); |
| 339 | } |
| 340 | max_clusters = i; |
| 341 | } |
| 342 | |
| 343 | |
John Stultz | c579371 | 2016-08-18 14:00:49 -0700 | [diff] [blame] | 344 | static void hikey_power_init(struct power_module __unused *module) |
| 345 | { |
John Stultz | e150ab3 | 2016-08-18 14:04:34 -0700 | [diff] [blame] | 346 | struct hikey_power_module *hikey = container_of(module, |
| 347 | struct hikey_power_module, base); |
John Stultz | ed77173 | 2017-05-23 17:39:53 -0700 | [diff] [blame] | 348 | hikey_cpufreq_init(hikey); |
John Stultz | e150ab3 | 2016-08-18 14:04:34 -0700 | [diff] [blame] | 349 | interactive_power_init(hikey); |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 350 | schedtune_power_init(hikey); |
| 351 | } |
| 352 | |
| 353 | static void hikey_hint_interaction(struct hikey_power_module *mod) |
| 354 | { |
| 355 | /* Try interactive cpufreq boosting first */ |
| 356 | if(!interactive_boostpulse(mod)) |
| 357 | return; |
| 358 | /* Then try EAS schedtune boosting */ |
| 359 | if(!schedtune_boost(mod)) |
| 360 | return; |
John Stultz | c579371 | 2016-08-18 14:00:49 -0700 | [diff] [blame] | 361 | } |
| 362 | |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 363 | static void hikey_power_hint(struct power_module *module, power_hint_t hint, |
| 364 | void *data) |
| 365 | { |
John Stultz | e150ab3 | 2016-08-18 14:04:34 -0700 | [diff] [blame] | 366 | struct hikey_power_module *hikey = container_of(module, |
| 367 | struct hikey_power_module, base); |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 368 | |
| 369 | pthread_mutex_lock(&hikey->lock); |
| 370 | switch (hint) { |
| 371 | case POWER_HINT_INTERACTION: |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 372 | hikey_hint_interaction(hikey); |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 373 | break; |
| 374 | |
| 375 | case POWER_HINT_VSYNC: |
| 376 | break; |
| 377 | |
| 378 | case POWER_HINT_LOW_POWER: |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 379 | low_power_mode = data; |
John Stultz | ed77173 | 2017-05-23 17:39:53 -0700 | [diff] [blame] | 380 | hikey_cpufreq_set_interactive(module, 1); |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 381 | break; |
| 382 | |
| 383 | default: |
| 384 | break; |
| 385 | } |
| 386 | pthread_mutex_unlock(&hikey->lock); |
| 387 | } |
| 388 | |
John Stultz | ab1f6a7 | 2016-08-18 12:28:55 -0700 | [diff] [blame] | 389 | static void set_feature(struct power_module *module, feature_t feature, int state) |
| 390 | { |
John Stultz | e150ab3 | 2016-08-18 14:04:34 -0700 | [diff] [blame] | 391 | struct hikey_power_module *hikey = container_of(module, |
| 392 | struct hikey_power_module, base); |
John Stultz | ab1f6a7 | 2016-08-18 12:28:55 -0700 | [diff] [blame] | 393 | switch (feature) { |
| 394 | default: |
Dmitry Shmidt | 0ea8f4d | 2016-11-07 12:55:16 -0800 | [diff] [blame] | 395 | ALOGW("Error setting the feature %d and state %d, it doesn't exist\n", |
| 396 | feature, state); |
John Stultz | ab1f6a7 | 2016-08-18 12:28:55 -0700 | [diff] [blame] | 397 | break; |
| 398 | } |
| 399 | } |
| 400 | |
Dmitry Shmidt | 0ea8f4d | 2016-11-07 12:55:16 -0800 | [diff] [blame] | 401 | 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] | 402 | hw_device_t** device) |
| 403 | { |
| 404 | int retval = 0; /* 0 is ok; -1 is error */ |
| 405 | ALOGD("%s: enter; name=%s", __FUNCTION__, name); |
| 406 | |
| 407 | if (strcmp(name, POWER_HARDWARE_MODULE_ID) == 0) { |
| 408 | power_module_t *dev = (power_module_t *)calloc(1, |
| 409 | sizeof(power_module_t)); |
| 410 | |
| 411 | if (dev) { |
| 412 | /* Common hw_device_t fields */ |
| 413 | dev->common.tag = HARDWARE_DEVICE_TAG; |
| 414 | dev->common.module_api_version = POWER_MODULE_API_VERSION_0_5; |
| 415 | dev->common.hal_api_version = HARDWARE_HAL_API_VERSION; |
| 416 | |
| 417 | dev->init = hikey_power_init; |
| 418 | dev->powerHint = hikey_power_hint; |
John Stultz | ed77173 | 2017-05-23 17:39:53 -0700 | [diff] [blame] | 419 | dev->setInteractive = hikey_cpufreq_set_interactive; |
Ruchi Kandoi | 96c621c | 2016-11-03 12:46:10 -0700 | [diff] [blame] | 420 | dev->setFeature = set_feature; |
| 421 | |
| 422 | *device = (hw_device_t*)dev; |
| 423 | } else |
| 424 | retval = -ENOMEM; |
| 425 | } else { |
| 426 | retval = -EINVAL; |
| 427 | } |
| 428 | |
| 429 | ALOGD("%s: exit %d", __FUNCTION__, retval); |
| 430 | return retval; |
| 431 | } |
| 432 | |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 433 | static struct hw_module_methods_t power_module_methods = { |
Ruchi Kandoi | 96c621c | 2016-11-03 12:46:10 -0700 | [diff] [blame] | 434 | .open = power_open, |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 435 | }; |
| 436 | |
| 437 | struct hikey_power_module HAL_MODULE_INFO_SYM = { |
Dmitry Shmidt | 0ea8f4d | 2016-11-07 12:55:16 -0800 | [diff] [blame] | 438 | .base = { |
| 439 | .common = { |
| 440 | .tag = HARDWARE_MODULE_TAG, |
| 441 | .module_api_version = POWER_MODULE_API_VERSION_0_2, |
| 442 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
| 443 | .id = POWER_HARDWARE_MODULE_ID, |
| 444 | .name = "HiKey Power HAL", |
| 445 | .author = "The Android Open Source Project", |
| 446 | .methods = &power_module_methods, |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 447 | }, |
| 448 | |
Dmitry Shmidt | 0ea8f4d | 2016-11-07 12:55:16 -0800 | [diff] [blame] | 449 | .init = hikey_power_init, |
John Stultz | ed77173 | 2017-05-23 17:39:53 -0700 | [diff] [blame] | 450 | .setInteractive = hikey_cpufreq_set_interactive, |
Dmitry Shmidt | 0ea8f4d | 2016-11-07 12:55:16 -0800 | [diff] [blame] | 451 | .powerHint = hikey_power_hint, |
| 452 | .setFeature = set_feature, |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 453 | }, |
| 454 | |
Dmitry Shmidt | 0ea8f4d | 2016-11-07 12:55:16 -0800 | [diff] [blame] | 455 | .lock = PTHREAD_MUTEX_INITIALIZER, |
| 456 | .boostpulse_fd = -1, |
| 457 | .boostpulse_warned = 0, |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 458 | }; |