Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * Copyright (C) 2014 The CyanogenMod Project |
| 4 | * Copyright (C) 2014-2015 Andreas Schneider <asn@cryptomilk.org> |
Christopher N. Hesse | f05f902 | 2017-01-16 22:49:06 +0100 | [diff] [blame] | 5 | * Copyright (C) 2014-2017 Christopher N. Hesse <raymanfx@gmail.com> |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | |
Christopher N. Hesse | 3360b09 | 2016-07-11 15:48:35 +0200 | [diff] [blame] | 20 | #include <ctype.h> |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 21 | #include <dirent.h> |
| 22 | #include <errno.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <malloc.h> |
| 25 | #include <stdbool.h> |
Christopher N. Hesse | e143419 | 2016-11-18 18:56:17 +0100 | [diff] [blame] | 26 | #include <stdlib.h> |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 27 | #include <string.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/stat.h> |
| 32 | |
| 33 | #define LOG_TAG "SamsungPowerHAL" |
| 34 | /* #define LOG_NDEBUG 0 */ |
| 35 | #include <utils/Log.h> |
| 36 | |
| 37 | #include <hardware/hardware.h> |
| 38 | #include <hardware/power.h> |
Christopher N. Hesse | 1226350 | 2016-12-07 12:18:20 +0100 | [diff] [blame] | 39 | #include <liblights/samsung_lights_helper.h> |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 40 | |
Christopher N. Hesse | 4139d85 | 2016-12-07 12:21:44 +0100 | [diff] [blame] | 41 | #include "samsung_power.h" |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 42 | |
Christopher N. Hesse | 81f714d | 2017-03-12 17:11:04 +0100 | [diff] [blame] | 43 | #define BOOST_PATH CPU0_INTERACTIVE_PATH "/boost" |
| 44 | #define BOOSTPULSE_PATH CPU0_INTERACTIVE_PATH "/boostpulse" |
| 45 | #define IO_IS_BUSY_PATH CPU0_INTERACTIVE_PATH "/io_is_busy" |
| 46 | #define CPU0_HISPEED_FREQ_PATH CPU0_INTERACTIVE_PATH "/hispeed_freq" |
| 47 | #define CPU4_HISPEED_FREQ_PATH CPU4_INTERACTIVE_PATH "/hispeed_freq" |
| 48 | |
| 49 | #define CPU0_MAX_FREQ_PATH CPU0_SYSFS_PATH "/cpufreq/scaling_max_freq" |
| 50 | #define CPU4_MAX_FREQ_PATH CPU4_SYSFS_PATH "/cpufreq/scaling_max_freq" |
| 51 | |
Christopher N. Hesse | d9106e9 | 2017-03-04 01:05:14 +0100 | [diff] [blame] | 52 | #define ARRAY_SIZE(a) sizeof(a) / sizeof(a[0]) |
| 53 | |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 54 | struct samsung_power_module { |
| 55 | struct power_module base; |
| 56 | pthread_mutex_t lock; |
| 57 | int boostpulse_fd; |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 58 | char cpu0_hispeed_freq[10]; |
| 59 | char cpu0_max_freq[10]; |
| 60 | char cpu4_hispeed_freq[10]; |
| 61 | char cpu4_max_freq[10]; |
| 62 | char* touchscreen_power_path; |
| 63 | char* touchkey_power_path; |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 64 | }; |
| 65 | |
Andreas Schneider | f15d7f4 | 2016-02-03 10:43:47 +0100 | [diff] [blame] | 66 | enum power_profile_e { |
| 67 | PROFILE_POWER_SAVE = 0, |
| 68 | PROFILE_BALANCED, |
Christopher N. Hesse | 58f2ca0 | 2017-01-17 00:01:15 +0100 | [diff] [blame] | 69 | PROFILE_HIGH_PERFORMANCE, |
| 70 | PROFILE_MAX |
Andreas Schneider | f15d7f4 | 2016-02-03 10:43:47 +0100 | [diff] [blame] | 71 | }; |
Christopher N. Hesse | ccd970d | 2017-01-25 22:41:05 +0100 | [diff] [blame] | 72 | |
Andreas Schneider | f15d7f4 | 2016-02-03 10:43:47 +0100 | [diff] [blame] | 73 | static enum power_profile_e current_power_profile = PROFILE_BALANCED; |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 74 | |
| 75 | /********************************************************** |
| 76 | *** HELPER FUNCTIONS |
| 77 | **********************************************************/ |
| 78 | |
| 79 | static int sysfs_read(char *path, char *s, int num_bytes) |
| 80 | { |
| 81 | char errno_str[64]; |
| 82 | int len; |
| 83 | int ret = 0; |
| 84 | int fd; |
| 85 | |
| 86 | fd = open(path, O_RDONLY); |
| 87 | if (fd < 0) { |
| 88 | strerror_r(errno, errno_str, sizeof(errno_str)); |
Christopher N. Hesse | 354f713 | 2017-03-04 01:06:22 +0100 | [diff] [blame] | 89 | ALOGE("Error opening %s: %s", path, errno_str); |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 90 | |
| 91 | return -1; |
| 92 | } |
| 93 | |
| 94 | len = read(fd, s, num_bytes - 1); |
| 95 | if (len < 0) { |
| 96 | strerror_r(errno, errno_str, sizeof(errno_str)); |
Christopher N. Hesse | 354f713 | 2017-03-04 01:06:22 +0100 | [diff] [blame] | 97 | ALOGE("Error reading from %s: %s", path, errno_str); |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 98 | |
| 99 | ret = -1; |
| 100 | } else { |
| 101 | s[len] = '\0'; |
| 102 | } |
| 103 | |
| 104 | close(fd); |
| 105 | |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | static void sysfs_write(const char *path, char *s) |
| 110 | { |
| 111 | char errno_str[64]; |
| 112 | int len; |
| 113 | int fd; |
| 114 | |
| 115 | fd = open(path, O_WRONLY); |
| 116 | if (fd < 0) { |
| 117 | strerror_r(errno, errno_str, sizeof(errno_str)); |
Christopher N. Hesse | 354f713 | 2017-03-04 01:06:22 +0100 | [diff] [blame] | 118 | ALOGE("Error opening %s: %s", path, errno_str); |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 119 | return; |
| 120 | } |
| 121 | |
| 122 | len = write(fd, s, strlen(s)); |
| 123 | if (len < 0) { |
| 124 | strerror_r(errno, errno_str, sizeof(errno_str)); |
Christopher N. Hesse | 354f713 | 2017-03-04 01:06:22 +0100 | [diff] [blame] | 125 | ALOGE("Error writing to %s: %s", path, errno_str); |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | close(fd); |
| 129 | } |
| 130 | |
Christopher N. Hesse | 5fada9b | 2017-01-16 23:39:48 +0100 | [diff] [blame] | 131 | static void boost(int32_t duration_us) |
| 132 | { |
| 133 | int fd; |
| 134 | |
| 135 | if (duration_us <= 0) |
| 136 | return; |
| 137 | |
| 138 | fd = open(BOOST_PATH, O_WRONLY); |
| 139 | if (fd < 0) { |
Christopher N. Hesse | 354f713 | 2017-03-04 01:06:22 +0100 | [diff] [blame] | 140 | ALOGE("Error opening %s", BOOST_PATH); |
Christopher N. Hesse | 5fada9b | 2017-01-16 23:39:48 +0100 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | |
| 144 | write(fd, "1", 1); |
| 145 | usleep(duration_us); |
| 146 | write(fd, "0", 1); |
| 147 | |
| 148 | close(fd); |
| 149 | } |
| 150 | |
Christopher N. Hesse | f21fbdb | 2017-03-04 01:41:58 +0100 | [diff] [blame] | 151 | static void boostpulse_open(struct samsung_power_module *samsung_pwr) |
| 152 | { |
| 153 | samsung_pwr->boostpulse_fd = open(BOOSTPULSE_PATH, O_WRONLY); |
| 154 | if (samsung_pwr->boostpulse_fd < 0) { |
| 155 | ALOGE("Error opening %s: %s\n", BOOSTPULSE_PATH, strerror(errno)); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | static void send_boostpulse(int boostpulse_fd) |
| 160 | { |
| 161 | int len; |
| 162 | |
| 163 | if (boostpulse_fd < 0) { |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | len = write(boostpulse_fd, "1", 1); |
| 168 | if (len < 0) { |
| 169 | ALOGE("Error writing to %s: %s", BOOSTPULSE_PATH, strerror(errno)); |
| 170 | } |
| 171 | } |
| 172 | |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 173 | /********************************************************** |
| 174 | *** POWER FUNCTIONS |
| 175 | **********************************************************/ |
| 176 | |
Andreas Schneider | f15d7f4 | 2016-02-03 10:43:47 +0100 | [diff] [blame] | 177 | static void set_power_profile(struct samsung_power_module *samsung_pwr, |
Christopher N. Hesse | 58f2ca0 | 2017-01-17 00:01:15 +0100 | [diff] [blame] | 178 | int profile) |
Andreas Schneider | f15d7f4 | 2016-02-03 10:43:47 +0100 | [diff] [blame] | 179 | { |
| 180 | int rc; |
| 181 | struct stat sb; |
| 182 | |
Christopher N. Hesse | 58f2ca0 | 2017-01-17 00:01:15 +0100 | [diff] [blame] | 183 | if (profile < 0 || profile >= PROFILE_MAX) { |
| 184 | return; |
| 185 | } |
| 186 | |
Andreas Schneider | f15d7f4 | 2016-02-03 10:43:47 +0100 | [diff] [blame] | 187 | if (current_power_profile == profile) { |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | ALOGV("%s: profile=%d", __func__, profile); |
| 192 | |
| 193 | switch (profile) { |
| 194 | case PROFILE_POWER_SAVE: |
Martin Bouchet | 4bf8a79 | 2017-03-28 04:00:04 -0300 | [diff] [blame] | 195 | // Grab value set by init.*.rc |
| 196 | sysfs_read(CPU0_HISPEED_FREQ_PATH, samsung_pwr->cpu0_hispeed_freq, |
| 197 | sizeof(samsung_pwr->cpu0_hispeed_freq)); |
Andreas Schneider | f15d7f4 | 2016-02-03 10:43:47 +0100 | [diff] [blame] | 198 | // Limit to hispeed freq |
| 199 | sysfs_write(CPU0_MAX_FREQ_PATH, samsung_pwr->cpu0_hispeed_freq); |
| 200 | rc = stat(CPU4_MAX_FREQ_PATH, &sb); |
| 201 | if (rc == 0) { |
Martin Bouchet | 4bf8a79 | 2017-03-28 04:00:04 -0300 | [diff] [blame] | 202 | sysfs_read(CPU4_HISPEED_FREQ_PATH, samsung_pwr->cpu4_hispeed_freq, |
| 203 | sizeof(samsung_pwr->cpu4_hispeed_freq)); |
Andreas Schneider | f15d7f4 | 2016-02-03 10:43:47 +0100 | [diff] [blame] | 204 | sysfs_write(CPU4_MAX_FREQ_PATH, samsung_pwr->cpu4_hispeed_freq); |
| 205 | } |
Christopher N. Hesse | f05f902 | 2017-01-16 22:49:06 +0100 | [diff] [blame] | 206 | ALOGV("%s: set powersave mode", __func__); |
Andreas Schneider | f15d7f4 | 2016-02-03 10:43:47 +0100 | [diff] [blame] | 207 | break; |
| 208 | case PROFILE_BALANCED: |
| 209 | // Restore normal max freq |
| 210 | sysfs_write(CPU0_MAX_FREQ_PATH, samsung_pwr->cpu0_max_freq); |
| 211 | rc = stat(CPU4_MAX_FREQ_PATH, &sb); |
| 212 | if (rc == 0) { |
| 213 | sysfs_write(CPU4_MAX_FREQ_PATH, samsung_pwr->cpu4_max_freq); |
| 214 | } |
Christopher N. Hesse | f05f902 | 2017-01-16 22:49:06 +0100 | [diff] [blame] | 215 | ALOGV("%s: set balanced mode", __func__); |
Andreas Schneider | f15d7f4 | 2016-02-03 10:43:47 +0100 | [diff] [blame] | 216 | break; |
| 217 | case PROFILE_HIGH_PERFORMANCE: |
| 218 | // Restore normal max freq |
| 219 | sysfs_write(CPU0_MAX_FREQ_PATH, samsung_pwr->cpu0_max_freq); |
| 220 | rc = stat(CPU4_MAX_FREQ_PATH, &sb); |
| 221 | if (rc == 0) { |
| 222 | sysfs_write(CPU4_MAX_FREQ_PATH, samsung_pwr->cpu4_max_freq); |
| 223 | } |
Christopher N. Hesse | f05f902 | 2017-01-16 22:49:06 +0100 | [diff] [blame] | 224 | ALOGV("%s: set performance mode", __func__); |
Andreas Schneider | f15d7f4 | 2016-02-03 10:43:47 +0100 | [diff] [blame] | 225 | break; |
Christopher N. Hesse | 2981f79 | 2017-03-12 17:08:47 +0100 | [diff] [blame] | 226 | default: |
| 227 | ALOGW("%s: Unknown power profile: %d", __func__, profile); |
| 228 | return; |
Andreas Schneider | f15d7f4 | 2016-02-03 10:43:47 +0100 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | current_power_profile = profile; |
| 232 | } |
| 233 | |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 234 | static void find_input_nodes(struct samsung_power_module *samsung_pwr, char *dir) |
| 235 | { |
| 236 | const char filename[] = "name"; |
| 237 | char errno_str[64]; |
| 238 | struct dirent *de; |
| 239 | char file_content[20]; |
| 240 | char *path = NULL; |
| 241 | char *node_path = NULL; |
| 242 | size_t pathsize; |
| 243 | size_t node_pathsize; |
| 244 | DIR *d; |
| 245 | |
| 246 | d = opendir(dir); |
| 247 | if (d == NULL) { |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | while ((de = readdir(d)) != NULL) { |
| 252 | if (strncmp(filename, de->d_name, sizeof(filename)) == 0) { |
| 253 | pathsize = strlen(dir) + strlen(de->d_name) + 2; |
| 254 | node_pathsize = strlen(dir) + strlen("enabled") + 2; |
| 255 | |
| 256 | path = malloc(pathsize); |
| 257 | node_path = malloc(node_pathsize); |
| 258 | if (path == NULL || node_path == NULL) { |
| 259 | strerror_r(errno, errno_str, sizeof(errno_str)); |
Christopher N. Hesse | 354f713 | 2017-03-04 01:06:22 +0100 | [diff] [blame] | 260 | ALOGE("Out of memory: %s", errno_str); |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 261 | return; |
| 262 | } |
| 263 | |
| 264 | snprintf(path, pathsize, "%s/%s", dir, filename); |
| 265 | sysfs_read(path, file_content, sizeof(file_content)); |
| 266 | |
| 267 | snprintf(node_path, node_pathsize, "%s/%s", dir, "enabled"); |
| 268 | |
| 269 | if (strncmp(file_content, "sec_touchkey", 12) == 0) { |
Christopher N. Hesse | 354f713 | 2017-03-04 01:06:22 +0100 | [diff] [blame] | 270 | ALOGV("%s: found touchkey path: %s", __func__, node_path); |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 271 | samsung_pwr->touchkey_power_path = malloc(node_pathsize); |
| 272 | if (samsung_pwr->touchkey_power_path == NULL) { |
| 273 | strerror_r(errno, errno_str, sizeof(errno_str)); |
Christopher N. Hesse | 354f713 | 2017-03-04 01:06:22 +0100 | [diff] [blame] | 274 | ALOGE("Out of memory: %s", errno_str); |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 275 | return; |
| 276 | } |
Christopher N. Hesse | 22da313 | 2016-02-01 12:36:54 +0100 | [diff] [blame] | 277 | snprintf(samsung_pwr->touchkey_power_path, node_pathsize, |
| 278 | "%s", node_path); |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | if (strncmp(file_content, "sec_touchscreen", 15) == 0) { |
Christopher N. Hesse | 354f713 | 2017-03-04 01:06:22 +0100 | [diff] [blame] | 282 | ALOGV("%s: found touchscreen path: %s", __func__, node_path); |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 283 | samsung_pwr->touchscreen_power_path = malloc(node_pathsize); |
| 284 | if (samsung_pwr->touchscreen_power_path == NULL) { |
| 285 | strerror_r(errno, errno_str, sizeof(errno_str)); |
Christopher N. Hesse | 354f713 | 2017-03-04 01:06:22 +0100 | [diff] [blame] | 286 | ALOGE("Out of memory: %s", errno_str); |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 287 | return; |
| 288 | } |
Christopher N. Hesse | 22da313 | 2016-02-01 12:36:54 +0100 | [diff] [blame] | 289 | snprintf(samsung_pwr->touchscreen_power_path, node_pathsize, |
| 290 | "%s", node_path); |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | if (path) |
| 296 | free(path); |
| 297 | if (node_path) |
| 298 | free(node_path); |
| 299 | closedir(d); |
| 300 | } |
| 301 | |
| 302 | /********************************************************** |
| 303 | *** INIT FUNCTIONS |
| 304 | **********************************************************/ |
| 305 | |
| 306 | static void init_cpufreqs(struct samsung_power_module *samsung_pwr) |
| 307 | { |
| 308 | int rc; |
| 309 | struct stat sb; |
| 310 | |
| 311 | sysfs_read(CPU0_HISPEED_FREQ_PATH, samsung_pwr->cpu0_hispeed_freq, |
| 312 | sizeof(samsung_pwr->cpu0_hispeed_freq)); |
| 313 | sysfs_read(CPU0_MAX_FREQ_PATH, samsung_pwr->cpu0_max_freq, |
| 314 | sizeof(samsung_pwr->cpu0_max_freq)); |
Christopher N. Hesse | 354f713 | 2017-03-04 01:06:22 +0100 | [diff] [blame] | 315 | ALOGV("%s: CPU 0 hispeed freq: %s", __func__, samsung_pwr->cpu0_hispeed_freq); |
| 316 | ALOGV("%s: CPU 0 max freq: %s", __func__, samsung_pwr->cpu0_max_freq); |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 317 | |
| 318 | rc = stat(CPU4_HISPEED_FREQ_PATH, &sb); |
| 319 | if (rc == 0) { |
| 320 | sysfs_read(CPU4_HISPEED_FREQ_PATH, samsung_pwr->cpu4_hispeed_freq, |
| 321 | sizeof(samsung_pwr->cpu4_hispeed_freq)); |
| 322 | sysfs_read(CPU4_MAX_FREQ_PATH, samsung_pwr->cpu4_max_freq, |
| 323 | sizeof(samsung_pwr->cpu4_max_freq)); |
Christopher N. Hesse | 354f713 | 2017-03-04 01:06:22 +0100 | [diff] [blame] | 324 | ALOGV("%s: CPU 4 hispeed freq: %s", __func__, samsung_pwr->cpu4_hispeed_freq); |
| 325 | ALOGV("%s: CPU 4 max freq: %s", __func__, samsung_pwr->cpu4_max_freq); |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
| 329 | static void init_touch_input_power_path(struct samsung_power_module *samsung_pwr) |
| 330 | { |
| 331 | char dir[1024]; |
| 332 | char errno_str[64]; |
| 333 | uint32_t i; |
| 334 | |
| 335 | for (i = 0; i < 20; i++) { |
| 336 | snprintf(dir, sizeof(dir), "/sys/class/input/input%d", i); |
| 337 | find_input_nodes(samsung_pwr, dir); |
| 338 | } |
| 339 | } |
| 340 | |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 341 | static void samsung_power_init(struct power_module *module) |
| 342 | { |
| 343 | struct samsung_power_module *samsung_pwr = (struct samsung_power_module *) module; |
| 344 | |
| 345 | init_cpufreqs(samsung_pwr); |
Christopher N. Hesse | d9106e9 | 2017-03-04 01:05:14 +0100 | [diff] [blame] | 346 | |
| 347 | boostpulse_open(samsung_pwr); |
| 348 | |
Christopher N. Hesse | 65c65bd | 2017-03-07 23:01:12 +0100 | [diff] [blame] | 349 | samsung_pwr->touchscreen_power_path = NULL; |
| 350 | samsung_pwr->touchkey_power_path = NULL; |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 351 | init_touch_input_power_path(samsung_pwr); |
| 352 | } |
| 353 | |
Christopher N. Hesse | f629672 | 2017-01-16 22:47:11 +0100 | [diff] [blame] | 354 | /********************************************************** |
| 355 | *** API FUNCTIONS |
| 356 | *** |
| 357 | *** Refer to power.h for documentation. |
| 358 | **********************************************************/ |
| 359 | |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 360 | static void samsung_power_set_interactive(struct power_module *module, int on) |
| 361 | { |
| 362 | struct samsung_power_module *samsung_pwr = (struct samsung_power_module *) module; |
| 363 | struct stat sb; |
Christopher N. Hesse | d9106e9 | 2017-03-04 01:05:14 +0100 | [diff] [blame] | 364 | int panel_brightness; |
| 365 | char button_state[2]; |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 366 | int rc; |
Christopher N. Hesse | d9106e9 | 2017-03-04 01:05:14 +0100 | [diff] [blame] | 367 | static bool touchkeys_blocked = false; |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 368 | |
Christopher N. Hesse | 354f713 | 2017-03-04 01:06:22 +0100 | [diff] [blame] | 369 | ALOGV("power_set_interactive: %d", on); |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 370 | |
Christopher N. Hesse | d9106e9 | 2017-03-04 01:05:14 +0100 | [diff] [blame] | 371 | /* |
| 372 | * Do not disable any input devices if the screen is on but we are in a non-interactive |
| 373 | * state. |
| 374 | */ |
Christopher N. Hesse | 3360b09 | 2016-07-11 15:48:35 +0200 | [diff] [blame] | 375 | if (!on) { |
Christopher N. Hesse | d9106e9 | 2017-03-04 01:05:14 +0100 | [diff] [blame] | 376 | panel_brightness = get_cur_panel_brightness(); |
| 377 | if (panel_brightness < 0) { |
| 378 | ALOGE("%s: Failed to read panel brightness", __func__); |
| 379 | } else if (panel_brightness > 0) { |
Christopher N. Hesse | 3360b09 | 2016-07-11 15:48:35 +0200 | [diff] [blame] | 380 | ALOGV("%s: Moving to non-interactive state, but screen is still on," |
Christopher N. Hesse | 354f713 | 2017-03-04 01:06:22 +0100 | [diff] [blame] | 381 | " not disabling input devices", __func__); |
Christopher N. Hesse | 3360b09 | 2016-07-11 15:48:35 +0200 | [diff] [blame] | 382 | goto out; |
| 383 | } |
| 384 | } |
| 385 | |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 386 | sysfs_write(samsung_pwr->touchscreen_power_path, on ? "1" : "0"); |
| 387 | |
Christopher N. Hesse | d9106e9 | 2017-03-04 01:05:14 +0100 | [diff] [blame] | 388 | /* Bail out if the device does not have touchkeys */ |
| 389 | if (samsung_pwr->touchkey_power_path == NULL) { |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 390 | goto out; |
| 391 | } |
| 392 | |
| 393 | if (!on) { |
Christopher N. Hesse | d9106e9 | 2017-03-04 01:05:14 +0100 | [diff] [blame] | 394 | rc = sysfs_read(samsung_pwr->touchkey_power_path, button_state, ARRAY_SIZE(button_state)); |
| 395 | if (rc < 0) { |
| 396 | ALOGE("%s: Failed to read touchkey state", __func__); |
| 397 | goto out; |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 398 | } |
Christopher N. Hesse | d9106e9 | 2017-03-04 01:05:14 +0100 | [diff] [blame] | 399 | /* |
| 400 | * If button_state is 0, the keys have been disabled by another component |
| 401 | * (for example cmhw), which means we don't want them to be enabled when resuming |
| 402 | * from suspend. |
| 403 | */ |
| 404 | if (button_state[0] == '0') { |
| 405 | touchkeys_blocked = true; |
| 406 | } else { |
| 407 | touchkeys_blocked = false; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | if (!touchkeys_blocked) { |
| 412 | sysfs_write(samsung_pwr->touchkey_power_path, on ? "1" : "0"); |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 413 | } |
| 414 | |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 415 | out: |
Christopher N. Hesse | 2879c69 | 2016-07-11 15:49:49 +0200 | [diff] [blame] | 416 | sysfs_write(IO_IS_BUSY_PATH, on ? "1" : "0"); |
Christopher N. Hesse | 354f713 | 2017-03-04 01:06:22 +0100 | [diff] [blame] | 417 | ALOGV("power_set_interactive: %d done", on); |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 418 | } |
| 419 | |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 420 | static void samsung_power_hint(struct power_module *module, |
| 421 | power_hint_t hint, |
| 422 | void *data) |
| 423 | { |
| 424 | struct samsung_power_module *samsung_pwr = (struct samsung_power_module *) module; |
| 425 | char errno_str[64]; |
| 426 | int len; |
| 427 | |
Christopher N. Hesse | 15b6309 | 2017-03-12 17:04:53 +0100 | [diff] [blame] | 428 | /* Bail out if low-power mode is active */ |
| 429 | if (current_power_profile == PROFILE_POWER_SAVE && hint != POWER_HINT_SET_PROFILE) { |
| 430 | ALOGW("%s: PROFILE_POWER_SAVE active, ignoring hint %d", __func__, hint); |
| 431 | return; |
| 432 | } |
| 433 | |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 434 | switch (hint) { |
Christopher N. Hesse | 2981f79 | 2017-03-12 17:08:47 +0100 | [diff] [blame] | 435 | case POWER_HINT_VSYNC: |
| 436 | ALOGV("%s: POWER_HINT_VSYNC", __func__); |
| 437 | break; |
| 438 | case POWER_HINT_INTERACTION: |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 439 | ALOGV("%s: POWER_HINT_INTERACTION", __func__); |
Christopher N. Hesse | f21fbdb | 2017-03-04 01:41:58 +0100 | [diff] [blame] | 440 | send_boostpulse(samsung_pwr->boostpulse_fd); |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 441 | break; |
Christopher N. Hesse | 2981f79 | 2017-03-12 17:08:47 +0100 | [diff] [blame] | 442 | case POWER_HINT_LOW_POWER: |
| 443 | ALOGV("%s: POWER_HINT_LOW_POWER", __func__); |
| 444 | set_power_profile(samsung_pwr, PROFILE_POWER_SAVE); |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 445 | break; |
Christopher N. Hesse | 2981f79 | 2017-03-12 17:08:47 +0100 | [diff] [blame] | 446 | case POWER_HINT_LAUNCH: |
Christopher N. Hesse | 5fada9b | 2017-01-16 23:39:48 +0100 | [diff] [blame] | 447 | case POWER_HINT_CPU_BOOST: |
Christopher N. Hesse | 2981f79 | 2017-03-12 17:08:47 +0100 | [diff] [blame] | 448 | ALOGV("%s: POWER_HINT_LAUNCH | POWER_HINT_CPU_BOOST", __func__); |
Christopher N. Hesse | 5fada9b | 2017-01-16 23:39:48 +0100 | [diff] [blame] | 449 | boost((*(int32_t *)data)); |
| 450 | break; |
Christopher N. Hesse | 2981f79 | 2017-03-12 17:08:47 +0100 | [diff] [blame] | 451 | case POWER_HINT_SET_PROFILE: |
Andreas Schneider | f15d7f4 | 2016-02-03 10:43:47 +0100 | [diff] [blame] | 452 | ALOGV("%s: POWER_HINT_SET_PROFILE", __func__); |
Christopher N. Hesse | 2981f79 | 2017-03-12 17:08:47 +0100 | [diff] [blame] | 453 | int profile = *((intptr_t *)data); |
Andreas Schneider | f15d7f4 | 2016-02-03 10:43:47 +0100 | [diff] [blame] | 454 | set_power_profile(samsung_pwr, profile); |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 455 | break; |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 456 | default: |
Christopher N. Hesse | 2981f79 | 2017-03-12 17:08:47 +0100 | [diff] [blame] | 457 | ALOGW("%s: Unknown power hint: %d", __func__, hint); |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 458 | break; |
| 459 | } |
| 460 | } |
| 461 | |
Andreas Schneider | f15d7f4 | 2016-02-03 10:43:47 +0100 | [diff] [blame] | 462 | static int samsung_get_feature(struct power_module *module __unused, |
| 463 | feature_t feature) |
| 464 | { |
| 465 | if (feature == POWER_FEATURE_SUPPORTED_PROFILES) { |
Christopher N. Hesse | 58f2ca0 | 2017-01-17 00:01:15 +0100 | [diff] [blame] | 466 | return PROFILE_MAX; |
Andreas Schneider | f15d7f4 | 2016-02-03 10:43:47 +0100 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | return -1; |
| 470 | } |
| 471 | |
Christopher N. Hesse | 1c47466 | 2016-11-18 18:59:06 +0100 | [diff] [blame] | 472 | static void samsung_set_feature(struct power_module *module, feature_t feature, int state __unused) |
Christopher N. Hesse | e480d89 | 2016-06-22 23:04:39 +0200 | [diff] [blame] | 473 | { |
| 474 | struct samsung_power_module *samsung_pwr = (struct samsung_power_module *) module; |
| 475 | |
| 476 | switch (feature) { |
ishantvivek | 987dcca | 2016-11-21 06:05:47 +0000 | [diff] [blame] | 477 | #ifdef TARGET_TAP_TO_WAKE_NODE |
Christopher N. Hesse | e480d89 | 2016-06-22 23:04:39 +0200 | [diff] [blame] | 478 | case POWER_FEATURE_DOUBLE_TAP_TO_WAKE: |
| 479 | ALOGV("%s: %s double tap to wake", __func__, state ? "enabling" : "disabling"); |
ishantvivek | 987dcca | 2016-11-21 06:05:47 +0000 | [diff] [blame] | 480 | sysfs_write(TARGET_TAP_TO_WAKE_NODE, state > 0 ? "1" : "0"); |
Christopher N. Hesse | e480d89 | 2016-06-22 23:04:39 +0200 | [diff] [blame] | 481 | break; |
| 482 | #endif |
| 483 | default: |
| 484 | break; |
| 485 | } |
| 486 | } |
| 487 | |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 488 | static struct hw_module_methods_t power_module_methods = { |
| 489 | .open = NULL, |
| 490 | }; |
| 491 | |
| 492 | struct samsung_power_module HAL_MODULE_INFO_SYM = { |
| 493 | .base = { |
| 494 | .common = { |
| 495 | .tag = HARDWARE_MODULE_TAG, |
| 496 | .module_api_version = POWER_MODULE_API_VERSION_0_2, |
| 497 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
| 498 | .id = POWER_HARDWARE_MODULE_ID, |
| 499 | .name = "Samsung Power HAL", |
Christopher N. Hesse | aa75be4 | 2017-01-16 22:47:59 +0100 | [diff] [blame] | 500 | .author = "The LineageOS Project", |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 501 | .methods = &power_module_methods, |
| 502 | }, |
| 503 | |
| 504 | .init = samsung_power_init, |
| 505 | .setInteractive = samsung_power_set_interactive, |
| 506 | .powerHint = samsung_power_hint, |
Christopher N. Hesse | e480d89 | 2016-06-22 23:04:39 +0200 | [diff] [blame] | 507 | .getFeature = samsung_get_feature, |
| 508 | .setFeature = samsung_set_feature |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 509 | }, |
| 510 | |
| 511 | .lock = PTHREAD_MUTEX_INITIALIZER, |
| 512 | .boostpulse_fd = -1, |
Christopher N. Hesse | de5e3c6 | 2015-12-21 21:28:23 +0100 | [diff] [blame] | 513 | }; |