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> |
| 28 | #include <cutils/properties.h> |
| 29 | //#define LOG_NDEBUG 0 |
| 30 | |
| 31 | #define LOG_TAG "HiKeyPowerHAL" |
| 32 | #include <utils/Log.h> |
| 33 | |
| 34 | #include <hardware/hardware.h> |
| 35 | #include <hardware/power.h> |
| 36 | |
John Stultz | 127e1cc | 2016-08-18 12:20:40 -0700 | [diff] [blame] | 37 | #define INTERACTIVE_BOOSTPULSE_PATH "/sys/devices/system/cpu/cpufreq/interactive/boostpulse" |
| 38 | #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] | 39 | #define CPU_MAX_FREQ_PATH "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq" |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 40 | #define LOW_POWER_MAX_FREQ "729000" |
| 41 | #define NORMAL_MAX_FREQ "1200000" |
| 42 | #define SVELTE_PROP "ro.boot.svelte" |
| 43 | #define SVELTE_MAX_FREQ_PROP "ro.config.svelte.max_cpu_freq" |
| 44 | #define SVELTE_LOW_POWER_MAX_FREQ_PROP "ro.config.svelte.low_power_max_cpu_freq" |
| 45 | |
| 46 | struct hikey_power_module { |
| 47 | struct power_module base; |
| 48 | pthread_mutex_t lock; |
| 49 | int boostpulse_fd; |
| 50 | int boostpulse_warned; |
| 51 | }; |
| 52 | |
| 53 | static bool low_power_mode = false; |
| 54 | |
| 55 | static char *max_cpu_freq = NORMAL_MAX_FREQ; |
| 56 | static char *low_power_max_cpu_freq = LOW_POWER_MAX_FREQ; |
| 57 | |
John Stultz | e150ab3 | 2016-08-18 14:04:34 -0700 | [diff] [blame^] | 58 | |
| 59 | #define container_of(addr, struct_name, field_name) \ |
| 60 | ((struct_name *)((char *)(addr) - offsetof(struct_name, field_name))) |
| 61 | |
| 62 | |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 63 | static void sysfs_write(const char *path, char *s) |
| 64 | { |
| 65 | char buf[80]; |
| 66 | int len; |
| 67 | int fd = open(path, O_WRONLY); |
| 68 | |
| 69 | if (fd < 0) { |
| 70 | strerror_r(errno, buf, sizeof(buf)); |
| 71 | ALOGE("Error opening %s: %s\n", path, buf); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | len = write(fd, s, strlen(s)); |
| 76 | if (len < 0) { |
| 77 | strerror_r(errno, buf, sizeof(buf)); |
| 78 | ALOGE("Error writing to %s: %s\n", path, buf); |
| 79 | } |
| 80 | |
| 81 | close(fd); |
| 82 | } |
| 83 | |
John Stultz | c579371 | 2016-08-18 14:00:49 -0700 | [diff] [blame] | 84 | /*[interactive cpufreq gov funcs]*********************************************/ |
John Stultz | e150ab3 | 2016-08-18 14:04:34 -0700 | [diff] [blame^] | 85 | static void interactive_power_init(struct hikey_power_module __unused *hikey) |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 86 | { |
John Stultz | 65de27c | 2016-08-18 11:50:42 -0700 | [diff] [blame] | 87 | int32_t is_svelte = property_get_int32(SVELTE_PROP, 0); |
| 88 | |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 89 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/timer_rate", |
| 90 | "20000"); |
| 91 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/timer_slack", |
| 92 | "20000"); |
| 93 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/min_sample_time", |
| 94 | "80000"); |
| 95 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/hispeed_freq", |
| 96 | "1200000"); |
| 97 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/go_hispeed_load", |
| 98 | "99"); |
| 99 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/target_loads", |
| 100 | "65 729000:75 960000:85"); |
| 101 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/above_hispeed_delay", |
| 102 | "20000"); |
| 103 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/boostpulse_duration", |
| 104 | "1000000"); |
| 105 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/io_is_busy", "0"); |
| 106 | |
John Stultz | 65de27c | 2016-08-18 11:50:42 -0700 | [diff] [blame] | 107 | if (is_svelte) { |
| 108 | char prop_buffer[PROPERTY_VALUE_MAX]; |
John Stultz | 127e1cc | 2016-08-18 12:20:40 -0700 | [diff] [blame] | 109 | int len = property_get(SVELTE_MAX_FREQ_PROP, prop_buffer, |
| 110 | LOW_POWER_MAX_FREQ); |
John Stultz | 65de27c | 2016-08-18 11:50:42 -0700 | [diff] [blame] | 111 | |
| 112 | max_cpu_freq = strndup(prop_buffer, len); |
John Stultz | 127e1cc | 2016-08-18 12:20:40 -0700 | [diff] [blame] | 113 | len = property_get(SVELTE_LOW_POWER_MAX_FREQ_PROP, prop_buffer, |
| 114 | LOW_POWER_MAX_FREQ); |
John Stultz | 65de27c | 2016-08-18 11:50:42 -0700 | [diff] [blame] | 115 | low_power_max_cpu_freq = strndup(prop_buffer, len); |
| 116 | } |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static void power_set_interactive(struct power_module __unused *module, int on) |
| 120 | { |
| 121 | ALOGV("power_set_interactive: %d\n", on); |
| 122 | |
| 123 | /* |
| 124 | * Lower maximum frequency when screen is off. |
| 125 | */ |
| 126 | sysfs_write(CPU_MAX_FREQ_PATH, |
| 127 | (!on || low_power_mode) ? low_power_max_cpu_freq : max_cpu_freq); |
John Stultz | 127e1cc | 2016-08-18 12:20:40 -0700 | [diff] [blame] | 128 | sysfs_write(INTERACTIVE_IO_IS_BUSY_PATH, on ? "1" : "0"); |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 129 | ALOGV("power_set_interactive: %d done\n", on); |
| 130 | } |
| 131 | |
John Stultz | 5e1546b | 2016-08-18 11:56:31 -0700 | [diff] [blame] | 132 | static int interactive_boostpulse(struct hikey_power_module *hikey) |
| 133 | { |
| 134 | char buf[80]; |
| 135 | int len; |
| 136 | |
John Stultz | 64b54ca | 2016-08-18 15:01:33 -0700 | [diff] [blame] | 137 | if (hikey->boostpulse_fd < 0) |
John Stultz | 127e1cc | 2016-08-18 12:20:40 -0700 | [diff] [blame] | 138 | hikey->boostpulse_fd = open(INTERACTIVE_BOOSTPULSE_PATH, O_WRONLY); |
John Stultz | 5e1546b | 2016-08-18 11:56:31 -0700 | [diff] [blame] | 139 | |
John Stultz | 64b54ca | 2016-08-18 15:01:33 -0700 | [diff] [blame] | 140 | if (hikey->boostpulse_fd < 0) { |
| 141 | if (!hikey->boostpulse_warned) { |
John Stultz | 5e1546b | 2016-08-18 11:56:31 -0700 | [diff] [blame] | 142 | strerror_r(errno, buf, sizeof(buf)); |
John Stultz | 127e1cc | 2016-08-18 12:20:40 -0700 | [diff] [blame] | 143 | ALOGE("Error opening %s: %s\n", INTERACTIVE_BOOSTPULSE_PATH, |
John Stultz | 64b54ca | 2016-08-18 15:01:33 -0700 | [diff] [blame] | 144 | buf); |
| 145 | hikey->boostpulse_warned = 1; |
John Stultz | 5e1546b | 2016-08-18 11:56:31 -0700 | [diff] [blame] | 146 | } |
John Stultz | 64b54ca | 2016-08-18 15:01:33 -0700 | [diff] [blame] | 147 | return hikey->boostpulse_fd; |
| 148 | } |
| 149 | |
| 150 | len = write(hikey->boostpulse_fd, "1", 1); |
| 151 | if (len < 0) { |
| 152 | strerror_r(errno, buf, sizeof(buf)); |
| 153 | ALOGE("Error writing to %s: %s\n", |
John Stultz | 127e1cc | 2016-08-18 12:20:40 -0700 | [diff] [blame] | 154 | INTERACTIVE_BOOSTPULSE_PATH, buf); |
John Stultz | 64b54ca | 2016-08-18 15:01:33 -0700 | [diff] [blame] | 155 | return -1; |
John Stultz | 5e1546b | 2016-08-18 11:56:31 -0700 | [diff] [blame] | 156 | } |
| 157 | return 0; |
| 158 | } |
| 159 | |
John Stultz | c579371 | 2016-08-18 14:00:49 -0700 | [diff] [blame] | 160 | /*[generic functions]*********************************************************/ |
| 161 | |
| 162 | static void hikey_power_init(struct power_module __unused *module) |
| 163 | { |
John Stultz | e150ab3 | 2016-08-18 14:04:34 -0700 | [diff] [blame^] | 164 | struct hikey_power_module *hikey = container_of(module, |
| 165 | struct hikey_power_module, base); |
| 166 | interactive_power_init(hikey); |
John Stultz | c579371 | 2016-08-18 14:00:49 -0700 | [diff] [blame] | 167 | } |
| 168 | |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 169 | static void hikey_power_hint(struct power_module *module, power_hint_t hint, |
| 170 | void *data) |
| 171 | { |
John Stultz | e150ab3 | 2016-08-18 14:04:34 -0700 | [diff] [blame^] | 172 | struct hikey_power_module *hikey = container_of(module, |
| 173 | struct hikey_power_module, base); |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 174 | |
| 175 | pthread_mutex_lock(&hikey->lock); |
| 176 | switch (hint) { |
| 177 | case POWER_HINT_INTERACTION: |
John Stultz | 5e1546b | 2016-08-18 11:56:31 -0700 | [diff] [blame] | 178 | interactive_boostpulse(hikey); |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 179 | break; |
| 180 | |
| 181 | case POWER_HINT_VSYNC: |
| 182 | break; |
| 183 | |
| 184 | case POWER_HINT_LOW_POWER: |
| 185 | if (data) { |
| 186 | sysfs_write(CPU_MAX_FREQ_PATH, low_power_max_cpu_freq); |
| 187 | } else { |
| 188 | sysfs_write(CPU_MAX_FREQ_PATH, max_cpu_freq); |
| 189 | } |
| 190 | low_power_mode = data; |
| 191 | break; |
| 192 | |
| 193 | default: |
| 194 | break; |
| 195 | } |
| 196 | pthread_mutex_unlock(&hikey->lock); |
| 197 | } |
| 198 | |
John Stultz | ab1f6a7 | 2016-08-18 12:28:55 -0700 | [diff] [blame] | 199 | static void set_feature(struct power_module *module, feature_t feature, int state) |
| 200 | { |
John Stultz | e150ab3 | 2016-08-18 14:04:34 -0700 | [diff] [blame^] | 201 | struct hikey_power_module *hikey = container_of(module, |
| 202 | struct hikey_power_module, base); |
John Stultz | ab1f6a7 | 2016-08-18 12:28:55 -0700 | [diff] [blame] | 203 | switch (feature) { |
| 204 | default: |
| 205 | ALOGW("Error setting the feature, it doesn't exist %d\n", feature); |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 210 | static struct hw_module_methods_t power_module_methods = { |
| 211 | .open = NULL, |
| 212 | }; |
| 213 | |
| 214 | struct hikey_power_module HAL_MODULE_INFO_SYM = { |
| 215 | base: { |
| 216 | common: { |
| 217 | tag: HARDWARE_MODULE_TAG, |
| 218 | module_api_version: POWER_MODULE_API_VERSION_0_2, |
| 219 | hal_api_version: HARDWARE_HAL_API_VERSION, |
| 220 | id: POWER_HARDWARE_MODULE_ID, |
| 221 | name: "HiKey Power HAL", |
| 222 | author: "The Android Open Source Project", |
| 223 | methods: &power_module_methods, |
| 224 | }, |
| 225 | |
John Stultz | c579371 | 2016-08-18 14:00:49 -0700 | [diff] [blame] | 226 | init: hikey_power_init, |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 227 | setInteractive: power_set_interactive, |
| 228 | powerHint: hikey_power_hint, |
| 229 | setFeature: set_feature, |
| 230 | }, |
| 231 | |
| 232 | lock: PTHREAD_MUTEX_INITIALIZER, |
| 233 | boostpulse_fd: -1, |
| 234 | boostpulse_warned: 0, |
| 235 | }; |
| 236 | |