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