blob: 66ad16607ca7310e30f403fea16f91de1a00241d [file] [log] [blame]
Christopher N. Hessede5e3c62015-12-21 21:28:23 +01001/*
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. Hessef05f9022017-01-16 22:49:06 +01005 * Copyright (C) 2014-2017 Christopher N. Hesse <raymanfx@gmail.com>
Christopher N. Hessede5e3c62015-12-21 21:28:23 +01006 *
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. Hesse3360b092016-07-11 15:48:35 +020020#include <ctype.h>
Christopher N. Hessede5e3c62015-12-21 21:28:23 +010021#include <dirent.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <malloc.h>
25#include <stdbool.h>
Christopher N. Hessee1434192016-11-18 18:56:17 +010026#include <stdlib.h>
Christopher N. Hessede5e3c62015-12-21 21:28:23 +010027#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. Hesse12263502016-12-07 12:18:20 +010039#include <liblights/samsung_lights_helper.h>
Christopher N. Hessede5e3c62015-12-21 21:28:23 +010040
Christopher N. Hesse4139d852016-12-07 12:21:44 +010041#include "samsung_power.h"
Christopher N. Hessede5e3c62015-12-21 21:28:23 +010042
43struct samsung_power_module {
44 struct power_module base;
45 pthread_mutex_t lock;
46 int boostpulse_fd;
47 int boostpulse_warned;
48 char cpu0_hispeed_freq[10];
49 char cpu0_max_freq[10];
50 char cpu4_hispeed_freq[10];
51 char cpu4_max_freq[10];
52 char* touchscreen_power_path;
53 char* touchkey_power_path;
54 bool touchkey_blocked;
55};
56
Andreas Schneiderf15d7f42016-02-03 10:43:47 +010057enum power_profile_e {
58 PROFILE_POWER_SAVE = 0,
59 PROFILE_BALANCED,
60 PROFILE_HIGH_PERFORMANCE
61};
62static enum power_profile_e current_power_profile = PROFILE_BALANCED;
Christopher N. Hessede5e3c62015-12-21 21:28:23 +010063
64/**********************************************************
65 *** HELPER FUNCTIONS
66 **********************************************************/
67
68static int sysfs_read(char *path, char *s, int num_bytes)
69{
70 char errno_str[64];
71 int len;
72 int ret = 0;
73 int fd;
74
75 fd = open(path, O_RDONLY);
76 if (fd < 0) {
77 strerror_r(errno, errno_str, sizeof(errno_str));
78 ALOGE("Error opening %s: %s\n", path, errno_str);
79
80 return -1;
81 }
82
83 len = read(fd, s, num_bytes - 1);
84 if (len < 0) {
85 strerror_r(errno, errno_str, sizeof(errno_str));
86 ALOGE("Error reading from %s: %s\n", path, errno_str);
87
88 ret = -1;
89 } else {
90 s[len] = '\0';
91 }
92
93 close(fd);
94
95 return ret;
96}
97
98static void sysfs_write(const char *path, char *s)
99{
100 char errno_str[64];
101 int len;
102 int fd;
103
104 fd = open(path, O_WRONLY);
105 if (fd < 0) {
106 strerror_r(errno, errno_str, sizeof(errno_str));
107 ALOGE("Error opening %s: %s\n", path, errno_str);
108 return;
109 }
110
111 len = write(fd, s, strlen(s));
112 if (len < 0) {
113 strerror_r(errno, errno_str, sizeof(errno_str));
114 ALOGE("Error writing to %s: %s\n", path, errno_str);
115 }
116
117 close(fd);
118}
119
120/**********************************************************
121 *** POWER FUNCTIONS
122 **********************************************************/
123
124/* You need to request the powerhal lock before calling this function */
125static int boostpulse_open(struct samsung_power_module *samsung_pwr)
126{
127 char errno_str[64];
128
129 if (samsung_pwr->boostpulse_fd < 0) {
130 samsung_pwr->boostpulse_fd = open(BOOSTPULSE_PATH, O_WRONLY);
131 if (samsung_pwr->boostpulse_fd < 0) {
132 if (!samsung_pwr->boostpulse_warned) {
133 strerror_r(errno, errno_str, sizeof(errno_str));
134 ALOGE("Error opening %s: %s\n", BOOSTPULSE_PATH, errno_str);
135 samsung_pwr->boostpulse_warned = 1;
136 }
137 }
138 }
139
140 return samsung_pwr->boostpulse_fd;
141}
142
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100143static void set_power_profile(struct samsung_power_module *samsung_pwr,
144 enum power_profile_e profile)
145{
146 int rc;
147 struct stat sb;
148
149 if (current_power_profile == profile) {
150 return;
151 }
152
153 ALOGV("%s: profile=%d", __func__, profile);
154
155 switch (profile) {
156 case PROFILE_POWER_SAVE:
157 // Limit to hispeed freq
158 sysfs_write(CPU0_MAX_FREQ_PATH, samsung_pwr->cpu0_hispeed_freq);
159 rc = stat(CPU4_MAX_FREQ_PATH, &sb);
160 if (rc == 0) {
161 sysfs_write(CPU4_MAX_FREQ_PATH, samsung_pwr->cpu4_hispeed_freq);
162 }
Christopher N. Hessef05f9022017-01-16 22:49:06 +0100163 ALOGV("%s: set powersave mode", __func__);
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100164 break;
165 case PROFILE_BALANCED:
166 // Restore normal max freq
167 sysfs_write(CPU0_MAX_FREQ_PATH, samsung_pwr->cpu0_max_freq);
168 rc = stat(CPU4_MAX_FREQ_PATH, &sb);
169 if (rc == 0) {
170 sysfs_write(CPU4_MAX_FREQ_PATH, samsung_pwr->cpu4_max_freq);
171 }
Christopher N. Hessef05f9022017-01-16 22:49:06 +0100172 ALOGV("%s: set balanced mode", __func__);
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100173 break;
174 case PROFILE_HIGH_PERFORMANCE:
175 // Restore normal max freq
176 sysfs_write(CPU0_MAX_FREQ_PATH, samsung_pwr->cpu0_max_freq);
177 rc = stat(CPU4_MAX_FREQ_PATH, &sb);
178 if (rc == 0) {
179 sysfs_write(CPU4_MAX_FREQ_PATH, samsung_pwr->cpu4_max_freq);
180 }
Christopher N. Hessef05f9022017-01-16 22:49:06 +0100181 ALOGV("%s: set performance mode", __func__);
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100182 break;
183 }
184
185 current_power_profile = profile;
186}
187
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100188static void find_input_nodes(struct samsung_power_module *samsung_pwr, char *dir)
189{
190 const char filename[] = "name";
191 char errno_str[64];
192 struct dirent *de;
193 char file_content[20];
194 char *path = NULL;
195 char *node_path = NULL;
196 size_t pathsize;
197 size_t node_pathsize;
198 DIR *d;
199
200 d = opendir(dir);
201 if (d == NULL) {
202 return;
203 }
204
205 while ((de = readdir(d)) != NULL) {
206 if (strncmp(filename, de->d_name, sizeof(filename)) == 0) {
207 pathsize = strlen(dir) + strlen(de->d_name) + 2;
208 node_pathsize = strlen(dir) + strlen("enabled") + 2;
209
210 path = malloc(pathsize);
211 node_path = malloc(node_pathsize);
212 if (path == NULL || node_path == NULL) {
213 strerror_r(errno, errno_str, sizeof(errno_str));
214 ALOGE("Out of memory: %s\n", errno_str);
215 return;
216 }
217
218 snprintf(path, pathsize, "%s/%s", dir, filename);
219 sysfs_read(path, file_content, sizeof(file_content));
220
221 snprintf(node_path, node_pathsize, "%s/%s", dir, "enabled");
222
223 if (strncmp(file_content, "sec_touchkey", 12) == 0) {
224 ALOGV("%s: found touchkey path: %s\n", __func__, node_path);
225 samsung_pwr->touchkey_power_path = malloc(node_pathsize);
226 if (samsung_pwr->touchkey_power_path == NULL) {
227 strerror_r(errno, errno_str, sizeof(errno_str));
228 ALOGE("Out of memory: %s\n", errno_str);
229 return;
230 }
Christopher N. Hesse22da3132016-02-01 12:36:54 +0100231 snprintf(samsung_pwr->touchkey_power_path, node_pathsize,
232 "%s", node_path);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100233 }
234
235 if (strncmp(file_content, "sec_touchscreen", 15) == 0) {
236 ALOGV("%s: found touchscreen path: %s\n", __func__, node_path);
237 samsung_pwr->touchscreen_power_path = malloc(node_pathsize);
238 if (samsung_pwr->touchscreen_power_path == NULL) {
239 strerror_r(errno, errno_str, sizeof(errno_str));
240 ALOGE("Out of memory: %s\n", errno_str);
241 return;
242 }
Christopher N. Hesse22da3132016-02-01 12:36:54 +0100243 snprintf(samsung_pwr->touchscreen_power_path, node_pathsize,
244 "%s", node_path);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100245 }
246 }
247 }
248
249 if (path)
250 free(path);
251 if (node_path)
252 free(node_path);
253 closedir(d);
254}
255
256/**********************************************************
257 *** INIT FUNCTIONS
258 **********************************************************/
259
260static void init_cpufreqs(struct samsung_power_module *samsung_pwr)
261{
262 int rc;
263 struct stat sb;
264
265 sysfs_read(CPU0_HISPEED_FREQ_PATH, samsung_pwr->cpu0_hispeed_freq,
266 sizeof(samsung_pwr->cpu0_hispeed_freq));
267 sysfs_read(CPU0_MAX_FREQ_PATH, samsung_pwr->cpu0_max_freq,
268 sizeof(samsung_pwr->cpu0_max_freq));
269 ALOGV("%s: CPU 0 hispeed freq: %s\n", __func__, samsung_pwr->cpu0_hispeed_freq);
270 ALOGV("%s: CPU 0 max freq: %s\n", __func__, samsung_pwr->cpu0_max_freq);
271
272 rc = stat(CPU4_HISPEED_FREQ_PATH, &sb);
273 if (rc == 0) {
274 sysfs_read(CPU4_HISPEED_FREQ_PATH, samsung_pwr->cpu4_hispeed_freq,
275 sizeof(samsung_pwr->cpu4_hispeed_freq));
276 sysfs_read(CPU4_MAX_FREQ_PATH, samsung_pwr->cpu4_max_freq,
277 sizeof(samsung_pwr->cpu4_max_freq));
278 ALOGV("%s: CPU 4 hispeed freq: %s\n", __func__, samsung_pwr->cpu4_hispeed_freq);
279 ALOGV("%s: CPU 4 max freq: %s\n", __func__, samsung_pwr->cpu4_max_freq);
280 }
281}
282
283static void init_touch_input_power_path(struct samsung_power_module *samsung_pwr)
284{
285 char dir[1024];
286 char errno_str[64];
287 uint32_t i;
288
289 for (i = 0; i < 20; i++) {
290 snprintf(dir, sizeof(dir), "/sys/class/input/input%d", i);
291 find_input_nodes(samsung_pwr, dir);
292 }
293}
294
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100295static void samsung_power_init(struct power_module *module)
296{
297 struct samsung_power_module *samsung_pwr = (struct samsung_power_module *) module;
298
299 init_cpufreqs(samsung_pwr);
300 init_touch_input_power_path(samsung_pwr);
301}
302
Christopher N. Hessef6296722017-01-16 22:47:11 +0100303/**********************************************************
304 *** API FUNCTIONS
305 ***
306 *** Refer to power.h for documentation.
307 **********************************************************/
308
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100309static void samsung_power_set_interactive(struct power_module *module, int on)
310{
311 struct samsung_power_module *samsung_pwr = (struct samsung_power_module *) module;
312 struct stat sb;
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100313 char touchkey_node[2];
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100314 int rc;
315
316 ALOGV("power_set_interactive: %d\n", on);
317
Christopher N. Hesse12263502016-12-07 12:18:20 +0100318 // Get panel backlight brightness from lights HAL
Christopher N. Hesse3360b092016-07-11 15:48:35 +0200319 // Do not disable any input devices if the screen is on but we are in a non-interactive state
320 if (!on) {
Christopher N. Hesse12263502016-12-07 12:18:20 +0100321 if (get_cur_panel_brightness() > 0) {
Christopher N. Hesse3360b092016-07-11 15:48:35 +0200322 ALOGV("%s: Moving to non-interactive state, but screen is still on,"
323 " not disabling input devices\n", __func__);
324 goto out;
325 }
326 }
327
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100328 sysfs_write(samsung_pwr->touchscreen_power_path, on ? "1" : "0");
329
330 rc = stat(samsung_pwr->touchkey_power_path, &sb);
331 if (rc < 0) {
332 goto out;
333 }
334
335 if (!on) {
336 if (sysfs_read(samsung_pwr->touchkey_power_path, touchkey_node,
337 sizeof(touchkey_node)) == 0) {
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100338 /*
Christopher N. Hesse3360b092016-07-11 15:48:35 +0200339 * If touchkey_node is 0, the keys have been disabled by another component
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100340 * (for example cmhw), which means we don't want them to be enabled when resuming
341 * from suspend.
342 */
Christopher N. Hessed9285742017-01-16 23:59:20 +0100343 if (touchkey_node[0] == '0') {
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100344 samsung_pwr->touchkey_blocked = true;
345 } else {
346 samsung_pwr->touchkey_blocked = false;
347 sysfs_write(samsung_pwr->touchkey_power_path, "0");
348 }
349 }
350 } else if (!samsung_pwr->touchkey_blocked) {
351 sysfs_write(samsung_pwr->touchkey_power_path, "1");
352 }
353
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100354out:
Christopher N. Hesse2879c692016-07-11 15:49:49 +0200355 sysfs_write(IO_IS_BUSY_PATH, on ? "1" : "0");
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100356 ALOGV("power_set_interactive: %d done\n", on);
357}
358
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100359static void samsung_power_hint(struct power_module *module,
360 power_hint_t hint,
361 void *data)
362{
363 struct samsung_power_module *samsung_pwr = (struct samsung_power_module *) module;
364 char errno_str[64];
365 int len;
366
367 switch (hint) {
368 case POWER_HINT_INTERACTION: {
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100369 if (current_power_profile == PROFILE_POWER_SAVE) {
370 return;
371 }
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100372
373 ALOGV("%s: POWER_HINT_INTERACTION", __func__);
374
375 if (boostpulse_open(samsung_pwr) >= 0) {
376 len = write(samsung_pwr->boostpulse_fd, "1", 1);
377
378 if (len < 0) {
379 strerror_r(errno, errno_str, sizeof(errno_str));
380 ALOGE("Error writing to %s: %s\n", BOOSTPULSE_PATH, errno_str);
381 }
382 }
383
384 break;
385 }
386 case POWER_HINT_VSYNC: {
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100387 ALOGV("%s: POWER_HINT_VSYNC", __func__);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100388 break;
389 }
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100390 case POWER_HINT_SET_PROFILE: {
391 int profile = *((intptr_t *)data);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100392
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100393 ALOGV("%s: POWER_HINT_SET_PROFILE", __func__);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100394
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100395 set_power_profile(samsung_pwr, profile);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100396 break;
397 }
398 default:
399 break;
400 }
401}
402
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100403static int samsung_get_feature(struct power_module *module __unused,
404 feature_t feature)
405{
406 if (feature == POWER_FEATURE_SUPPORTED_PROFILES) {
407 return 3;
408 }
409
410 return -1;
411}
412
Christopher N. Hesse1c474662016-11-18 18:59:06 +0100413static void samsung_set_feature(struct power_module *module, feature_t feature, int state __unused)
Christopher N. Hessee480d892016-06-22 23:04:39 +0200414{
415 struct samsung_power_module *samsung_pwr = (struct samsung_power_module *) module;
416
417 switch (feature) {
ishantvivek987dcca2016-11-21 06:05:47 +0000418#ifdef TARGET_TAP_TO_WAKE_NODE
Christopher N. Hessee480d892016-06-22 23:04:39 +0200419 case POWER_FEATURE_DOUBLE_TAP_TO_WAKE:
420 ALOGV("%s: %s double tap to wake", __func__, state ? "enabling" : "disabling");
ishantvivek987dcca2016-11-21 06:05:47 +0000421 sysfs_write(TARGET_TAP_TO_WAKE_NODE, state > 0 ? "1" : "0");
Christopher N. Hessee480d892016-06-22 23:04:39 +0200422 break;
423#endif
424 default:
425 break;
426 }
427}
428
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100429static struct hw_module_methods_t power_module_methods = {
430 .open = NULL,
431};
432
433struct samsung_power_module HAL_MODULE_INFO_SYM = {
434 .base = {
435 .common = {
436 .tag = HARDWARE_MODULE_TAG,
437 .module_api_version = POWER_MODULE_API_VERSION_0_2,
438 .hal_api_version = HARDWARE_HAL_API_VERSION,
439 .id = POWER_HARDWARE_MODULE_ID,
440 .name = "Samsung Power HAL",
Christopher N. Hesseaa75be42017-01-16 22:47:59 +0100441 .author = "The LineageOS Project",
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100442 .methods = &power_module_methods,
443 },
444
445 .init = samsung_power_init,
446 .setInteractive = samsung_power_set_interactive,
447 .powerHint = samsung_power_hint,
Christopher N. Hessee480d892016-06-22 23:04:39 +0200448 .getFeature = samsung_get_feature,
449 .setFeature = samsung_set_feature
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100450 },
451
452 .lock = PTHREAD_MUTEX_INITIALIZER,
453 .boostpulse_fd = -1,
454 .boostpulse_warned = 0,
455};