blob: 2ec1d80e90fc0e3bc9cb0524b07d774056775483 [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>
Christopher N. Hessede5e3c62015-12-21 21:28:23 +010031
32#define LOG_TAG "SamsungPowerHAL"
33/* #define LOG_NDEBUG 0 */
34#include <utils/Log.h>
35
36#include <hardware/hardware.h>
37#include <hardware/power.h>
Christopher N. Hesse12263502016-12-07 12:18:20 +010038#include <liblights/samsung_lights_helper.h>
Christopher N. Hessede5e3c62015-12-21 21:28:23 +010039
Christopher N. Hesse4139d852016-12-07 12:21:44 +010040#include "samsung_power.h"
Christopher N. Hessede5e3c62015-12-21 21:28:23 +010041
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +020042#define BOOST_PATH "/boost"
43#define BOOSTPULSE_PATH "/boostpulse"
Christopher N. Hesse81f714d2017-03-12 17:11:04 +010044
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +020045#define IO_IS_BUSY_PATH "/io_is_busy"
46#define HISPEED_FREQ_PATH "/hispeed_freq"
Christopher N. Hesse81f714d2017-03-12 17:11:04 +010047
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +020048#define MAX_FREQ_PATH "/cpufreq/scaling_max_freq"
49
50#define CLUSTER_COUNT ARRAY_SIZE(CPU_SYSFS_PATHS)
51#define PARAM_MAXLEN 10
52
53#define ARRAY_SIZE(a) sizeof(a) / sizeof(a[0])
Christopher N. Hessed9106e92017-03-04 01:05:14 +010054
Christopher N. Hessede5e3c62015-12-21 21:28:23 +010055struct samsung_power_module {
56 struct power_module base;
57 pthread_mutex_t lock;
58 int boostpulse_fd;
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +020059 char hispeed_freqs[CLUSTER_COUNT][PARAM_MAXLEN];
60 char max_freqs[CLUSTER_COUNT][PARAM_MAXLEN];
Christopher N. Hessede5e3c62015-12-21 21:28:23 +010061 char* touchscreen_power_path;
62 char* touchkey_power_path;
Christopher N. Hessede5e3c62015-12-21 21:28:23 +010063};
64
Andreas Schneiderf15d7f42016-02-03 10:43:47 +010065enum power_profile_e {
66 PROFILE_POWER_SAVE = 0,
67 PROFILE_BALANCED,
Christopher N. Hesse58f2ca02017-01-17 00:01:15 +010068 PROFILE_HIGH_PERFORMANCE,
69 PROFILE_MAX
Andreas Schneiderf15d7f42016-02-03 10:43:47 +010070};
Christopher N. Hesseccd970d2017-01-25 22:41:05 +010071
Andreas Schneiderf15d7f42016-02-03 10:43:47 +010072static enum power_profile_e current_power_profile = PROFILE_BALANCED;
Christopher N. Hessede5e3c62015-12-21 21:28:23 +010073
74/**********************************************************
75 *** HELPER FUNCTIONS
76 **********************************************************/
77
78static int sysfs_read(char *path, char *s, int num_bytes)
79{
80 char errno_str[64];
81 int len;
82 int ret = 0;
83 int fd;
84
85 fd = open(path, O_RDONLY);
86 if (fd < 0) {
87 strerror_r(errno, errno_str, sizeof(errno_str));
Christopher N. Hesse354f7132017-03-04 01:06:22 +010088 ALOGE("Error opening %s: %s", path, errno_str);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +010089
90 return -1;
91 }
92
93 len = read(fd, s, num_bytes - 1);
94 if (len < 0) {
95 strerror_r(errno, errno_str, sizeof(errno_str));
Christopher N. Hesse354f7132017-03-04 01:06:22 +010096 ALOGE("Error reading from %s: %s", path, errno_str);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +010097
98 ret = -1;
99 } else {
Christopher N. Hesse0da5fbf2017-07-08 13:56:25 +0200100 // do not store newlines, but terminate the string instead
101 if (s[len-1] == '\n') {
102 s[len-1] = '\0';
103 } else {
104 s[len] = '\0';
105 }
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100106 }
107
108 close(fd);
109
110 return ret;
111}
112
113static void sysfs_write(const char *path, char *s)
114{
115 char errno_str[64];
116 int len;
117 int fd;
118
119 fd = open(path, O_WRONLY);
120 if (fd < 0) {
121 strerror_r(errno, errno_str, sizeof(errno_str));
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100122 ALOGE("Error opening %s: %s", path, errno_str);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100123 return;
124 }
125
126 len = write(fd, s, strlen(s));
127 if (len < 0) {
128 strerror_r(errno, errno_str, sizeof(errno_str));
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100129 ALOGE("Error writing to %s: %s", path, errno_str);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100130 }
131
132 close(fd);
133}
134
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200135static void cpu_sysfs_read(const char *param, char s[CLUSTER_COUNT][PARAM_MAXLEN])
136{
137 char path[PATH_MAX];
138
139 for (unsigned int i = 0; i < ARRAY_SIZE(CPU_SYSFS_PATHS); i++) {
140 sprintf(path, "%s%s", CPU_SYSFS_PATHS[i], param);
141 sysfs_read(path, s[i], PARAM_MAXLEN);
142 }
143}
144
145static void cpu_sysfs_write(const char *param, char s[CLUSTER_COUNT][PARAM_MAXLEN])
146{
147 char path[PATH_MAX];
148
149 for (unsigned int i = 0; i < ARRAY_SIZE(CPU_SYSFS_PATHS); i++) {
150 sprintf(path, "%s%s", CPU_SYSFS_PATHS[i], param);
151 sysfs_write(path, s[i]);
152 }
153}
154
155static void cpu_interactive_read(const char *param, char s[CLUSTER_COUNT][PARAM_MAXLEN])
156{
157 char path[PATH_MAX];
158
159 for (unsigned int i = 0; i < ARRAY_SIZE(CPU_INTERACTIVE_PATHS); i++) {
160 sprintf(path, "%s%s", CPU_INTERACTIVE_PATHS[i], param);
161 sysfs_read(path, s[i], PARAM_MAXLEN);
162 }
163}
164
165static void cpu_interactive_write(const char *param, char s[CLUSTER_COUNT][PARAM_MAXLEN])
166{
167 char path[PATH_MAX];
168
169 for (unsigned int i = 0; i < ARRAY_SIZE(CPU_INTERACTIVE_PATHS); i++) {
170 sprintf(path, "%s%s", CPU_INTERACTIVE_PATHS[i], param);
171 sysfs_write(path, s[i]);
172 }
173}
174
Christopher N. Hesse5fada9b2017-01-16 23:39:48 +0100175static void boost(int32_t duration_us)
176{
177 int fd;
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200178 char path[PATH_MAX];
Christopher N. Hesse5fada9b2017-01-16 23:39:48 +0100179
180 if (duration_us <= 0)
181 return;
182
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200183 // the boost node is only valid for the LITTLE cluster
184 sprintf(path, "%s%s", CPU_INTERACTIVE_PATHS[0], BOOST_PATH);
185
186 fd = open(path, O_WRONLY);
Christopher N. Hesse5fada9b2017-01-16 23:39:48 +0100187 if (fd < 0) {
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200188 ALOGE("Error opening %s", path);
Christopher N. Hesse5fada9b2017-01-16 23:39:48 +0100189 return;
190 }
191
192 write(fd, "1", 1);
193 usleep(duration_us);
194 write(fd, "0", 1);
195
196 close(fd);
197}
198
Christopher N. Hessef21fbdb2017-03-04 01:41:58 +0100199static void boostpulse_open(struct samsung_power_module *samsung_pwr)
200{
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200201 char path[PATH_MAX];
202
203 // the boostpulse node is only valid for the LITTLE cluster
204 sprintf(path, "%s%s", CPU_INTERACTIVE_PATHS[0], BOOSTPULSE_PATH);
205
206 samsung_pwr->boostpulse_fd = open(path, O_WRONLY);
Christopher N. Hessef21fbdb2017-03-04 01:41:58 +0100207 if (samsung_pwr->boostpulse_fd < 0) {
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200208 ALOGE("Error opening %s: %s\n", path, strerror(errno));
Christopher N. Hessef21fbdb2017-03-04 01:41:58 +0100209 }
210}
211
212static void send_boostpulse(int boostpulse_fd)
213{
214 int len;
215
216 if (boostpulse_fd < 0) {
217 return;
218 }
219
220 len = write(boostpulse_fd, "1", 1);
221 if (len < 0) {
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200222 ALOGE("Error writing to %s%s: %s", CPU_INTERACTIVE_PATHS[0], BOOSTPULSE_PATH,
223 strerror(errno));
Christopher N. Hessef21fbdb2017-03-04 01:41:58 +0100224 }
225}
226
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100227/**********************************************************
228 *** POWER FUNCTIONS
229 **********************************************************/
230
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100231static void set_power_profile(struct samsung_power_module *samsung_pwr,
Christopher N. Hesse58f2ca02017-01-17 00:01:15 +0100232 int profile)
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100233{
234 int rc;
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100235
Christopher N. Hesse58f2ca02017-01-17 00:01:15 +0100236 if (profile < 0 || profile >= PROFILE_MAX) {
237 return;
238 }
239
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100240 if (current_power_profile == profile) {
241 return;
242 }
243
244 ALOGV("%s: profile=%d", __func__, profile);
245
246 switch (profile) {
247 case PROFILE_POWER_SAVE:
Martin Bouchet4bf8a792017-03-28 04:00:04 -0300248 // Grab value set by init.*.rc
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200249 cpu_interactive_read(HISPEED_FREQ_PATH, samsung_pwr->hispeed_freqs);
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100250 // Limit to hispeed freq
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200251 cpu_sysfs_write(MAX_FREQ_PATH, samsung_pwr->hispeed_freqs);
Christopher N. Hessef05f9022017-01-16 22:49:06 +0100252 ALOGV("%s: set powersave mode", __func__);
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100253 break;
254 case PROFILE_BALANCED:
255 // Restore normal max freq
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200256 cpu_sysfs_write(MAX_FREQ_PATH, samsung_pwr->max_freqs);
Christopher N. Hessef05f9022017-01-16 22:49:06 +0100257 ALOGV("%s: set balanced mode", __func__);
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100258 break;
259 case PROFILE_HIGH_PERFORMANCE:
260 // Restore normal max freq
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200261 cpu_sysfs_write(MAX_FREQ_PATH, samsung_pwr->max_freqs);
Christopher N. Hessef05f9022017-01-16 22:49:06 +0100262 ALOGV("%s: set performance mode", __func__);
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100263 break;
Christopher N. Hesse2981f792017-03-12 17:08:47 +0100264 default:
265 ALOGW("%s: Unknown power profile: %d", __func__, profile);
266 return;
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100267 }
268
269 current_power_profile = profile;
270}
271
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100272static void find_input_nodes(struct samsung_power_module *samsung_pwr, char *dir)
273{
274 const char filename[] = "name";
275 char errno_str[64];
276 struct dirent *de;
277 char file_content[20];
278 char *path = NULL;
279 char *node_path = NULL;
280 size_t pathsize;
281 size_t node_pathsize;
282 DIR *d;
283
284 d = opendir(dir);
285 if (d == NULL) {
286 return;
287 }
288
289 while ((de = readdir(d)) != NULL) {
290 if (strncmp(filename, de->d_name, sizeof(filename)) == 0) {
291 pathsize = strlen(dir) + strlen(de->d_name) + 2;
292 node_pathsize = strlen(dir) + strlen("enabled") + 2;
293
294 path = malloc(pathsize);
295 node_path = malloc(node_pathsize);
296 if (path == NULL || node_path == NULL) {
297 strerror_r(errno, errno_str, sizeof(errno_str));
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100298 ALOGE("Out of memory: %s", errno_str);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100299 return;
300 }
301
302 snprintf(path, pathsize, "%s/%s", dir, filename);
303 sysfs_read(path, file_content, sizeof(file_content));
304
305 snprintf(node_path, node_pathsize, "%s/%s", dir, "enabled");
306
307 if (strncmp(file_content, "sec_touchkey", 12) == 0) {
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100308 ALOGV("%s: found touchkey path: %s", __func__, node_path);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100309 samsung_pwr->touchkey_power_path = malloc(node_pathsize);
310 if (samsung_pwr->touchkey_power_path == NULL) {
311 strerror_r(errno, errno_str, sizeof(errno_str));
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100312 ALOGE("Out of memory: %s", errno_str);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100313 return;
314 }
Christopher N. Hesse22da3132016-02-01 12:36:54 +0100315 snprintf(samsung_pwr->touchkey_power_path, node_pathsize,
316 "%s", node_path);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100317 }
318
319 if (strncmp(file_content, "sec_touchscreen", 15) == 0) {
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100320 ALOGV("%s: found touchscreen path: %s", __func__, node_path);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100321 samsung_pwr->touchscreen_power_path = malloc(node_pathsize);
322 if (samsung_pwr->touchscreen_power_path == NULL) {
323 strerror_r(errno, errno_str, sizeof(errno_str));
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100324 ALOGE("Out of memory: %s", errno_str);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100325 return;
326 }
Christopher N. Hesse22da3132016-02-01 12:36:54 +0100327 snprintf(samsung_pwr->touchscreen_power_path, node_pathsize,
328 "%s", node_path);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100329 }
330 }
331 }
332
333 if (path)
334 free(path);
335 if (node_path)
336 free(node_path);
337 closedir(d);
338}
339
340/**********************************************************
341 *** INIT FUNCTIONS
342 **********************************************************/
343
344static void init_cpufreqs(struct samsung_power_module *samsung_pwr)
345{
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200346 cpu_interactive_read(HISPEED_FREQ_PATH, samsung_pwr->hispeed_freqs);
347 cpu_sysfs_read(MAX_FREQ_PATH, samsung_pwr->max_freqs);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100348}
349
350static void init_touch_input_power_path(struct samsung_power_module *samsung_pwr)
351{
352 char dir[1024];
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100353 uint32_t i;
354
355 for (i = 0; i < 20; i++) {
356 snprintf(dir, sizeof(dir), "/sys/class/input/input%d", i);
357 find_input_nodes(samsung_pwr, dir);
358 }
359}
360
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100361static void samsung_power_init(struct power_module *module)
362{
363 struct samsung_power_module *samsung_pwr = (struct samsung_power_module *) module;
364
365 init_cpufreqs(samsung_pwr);
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100366
367 boostpulse_open(samsung_pwr);
368
Christopher N. Hesse65c65bd2017-03-07 23:01:12 +0100369 samsung_pwr->touchscreen_power_path = NULL;
370 samsung_pwr->touchkey_power_path = NULL;
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100371 init_touch_input_power_path(samsung_pwr);
Christopher N. Hesse775bce82017-07-08 13:46:35 +0200372
373 ALOGI("Initialized settings:");
Christopher N. Hesse1146f1a2017-07-13 21:14:57 +0200374 char max_freqs[PATH_MAX];
375 sprintf(max_freqs, "max_freqs: cluster[0]: %s", samsung_pwr->max_freqs[0]);
376 for (unsigned int i = 1; i < CLUSTER_COUNT; i++) {
377 sprintf(max_freqs, "%s, %s[%d]: %s", max_freqs, "cluster", i, samsung_pwr->max_freqs[i]);
378 }
379 ALOGI("%s", max_freqs);
380 char hispeed_freqs[PATH_MAX];
381 sprintf(hispeed_freqs, "hispeed_freqs: cluster[0]: %s", samsung_pwr->hispeed_freqs[0]);
382 for (unsigned int i = 1; i < CLUSTER_COUNT; i++) {
383 sprintf(hispeed_freqs, "%s, %s[%d]: %s", hispeed_freqs, "cluster", i,
384 samsung_pwr->hispeed_freqs[i]);
385 }
386 ALOGI("%s", hispeed_freqs);
Christopher N. Hesseff868462017-07-09 23:07:09 +0200387 ALOGI("boostpulse_fd: %d", samsung_pwr->boostpulse_fd);
Christopher N. Hesse775bce82017-07-08 13:46:35 +0200388 ALOGI("touchscreen_power_path: %s",
389 samsung_pwr->touchscreen_power_path ? samsung_pwr->touchscreen_power_path : "NULL");
390 ALOGI("touchkey_power_path: %s",
391 samsung_pwr->touchkey_power_path ? samsung_pwr->touchkey_power_path : "NULL");
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100392}
393
Christopher N. Hessef6296722017-01-16 22:47:11 +0100394/**********************************************************
395 *** API FUNCTIONS
396 ***
397 *** Refer to power.h for documentation.
398 **********************************************************/
399
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100400static void samsung_power_set_interactive(struct power_module *module, int on)
401{
402 struct samsung_power_module *samsung_pwr = (struct samsung_power_module *) module;
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100403 int panel_brightness;
404 char button_state[2];
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100405 int rc;
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100406 static bool touchkeys_blocked = false;
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200407 char ON[CLUSTER_COUNT][PARAM_MAXLEN] = {"1", "1"};
408 char OFF[CLUSTER_COUNT][PARAM_MAXLEN] = {"0", "0"};
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100409
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100410 ALOGV("power_set_interactive: %d", on);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100411
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100412 /*
413 * Do not disable any input devices if the screen is on but we are in a non-interactive
414 * state.
415 */
Christopher N. Hesse3360b092016-07-11 15:48:35 +0200416 if (!on) {
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100417 panel_brightness = get_cur_panel_brightness();
418 if (panel_brightness < 0) {
419 ALOGE("%s: Failed to read panel brightness", __func__);
420 } else if (panel_brightness > 0) {
Christopher N. Hesse3360b092016-07-11 15:48:35 +0200421 ALOGV("%s: Moving to non-interactive state, but screen is still on,"
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100422 " not disabling input devices", __func__);
Christopher N. Hesse3360b092016-07-11 15:48:35 +0200423 goto out;
424 }
425 }
426
Paul Keith5e4fbe02017-04-21 21:34:17 -0500427 /* Sanity check the touchscreen path */
428 if (samsung_pwr->touchscreen_power_path) {
429 sysfs_write(samsung_pwr->touchscreen_power_path, on ? "1" : "0");
430 }
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100431
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100432 /* Bail out if the device does not have touchkeys */
433 if (samsung_pwr->touchkey_power_path == NULL) {
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100434 goto out;
435 }
436
437 if (!on) {
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100438 rc = sysfs_read(samsung_pwr->touchkey_power_path, button_state, ARRAY_SIZE(button_state));
439 if (rc < 0) {
440 ALOGE("%s: Failed to read touchkey state", __func__);
441 goto out;
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100442 }
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100443 /*
444 * If button_state is 0, the keys have been disabled by another component
445 * (for example cmhw), which means we don't want them to be enabled when resuming
446 * from suspend.
447 */
448 if (button_state[0] == '0') {
449 touchkeys_blocked = true;
450 } else {
451 touchkeys_blocked = false;
452 }
453 }
454
455 if (!touchkeys_blocked) {
456 sysfs_write(samsung_pwr->touchkey_power_path, on ? "1" : "0");
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100457 }
458
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100459out:
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200460 cpu_interactive_write(IO_IS_BUSY_PATH, on ? ON : OFF);
Christopher N. Hesse63b5bd72017-05-29 20:45:14 +0200461
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100462 ALOGV("power_set_interactive: %d done", on);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100463}
464
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100465static void samsung_power_hint(struct power_module *module,
466 power_hint_t hint,
467 void *data)
468{
469 struct samsung_power_module *samsung_pwr = (struct samsung_power_module *) module;
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100470 int len;
471
Christopher N. Hesse15b63092017-03-12 17:04:53 +0100472 /* Bail out if low-power mode is active */
Christopher N. Hesse5eca5aa2017-04-26 21:42:10 +0200473 if (current_power_profile == PROFILE_POWER_SAVE && hint != POWER_HINT_LOW_POWER
474 && hint != POWER_HINT_SET_PROFILE) {
Christopher N. Hesse15b63092017-03-12 17:04:53 +0100475 ALOGW("%s: PROFILE_POWER_SAVE active, ignoring hint %d", __func__, hint);
476 return;
477 }
478
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100479 switch (hint) {
Christopher N. Hesse2981f792017-03-12 17:08:47 +0100480 case POWER_HINT_VSYNC:
481 ALOGV("%s: POWER_HINT_VSYNC", __func__);
482 break;
483 case POWER_HINT_INTERACTION:
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100484 ALOGV("%s: POWER_HINT_INTERACTION", __func__);
Christopher N. Hessef21fbdb2017-03-04 01:41:58 +0100485 send_boostpulse(samsung_pwr->boostpulse_fd);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100486 break;
Christopher N. Hesse2981f792017-03-12 17:08:47 +0100487 case POWER_HINT_LOW_POWER:
488 ALOGV("%s: POWER_HINT_LOW_POWER", __func__);
Christopher N. Hesse3fa2b692017-04-05 18:52:48 +0200489 set_power_profile(samsung_pwr, data ? PROFILE_POWER_SAVE : PROFILE_BALANCED);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100490 break;
Christopher N. Hesse2981f792017-03-12 17:08:47 +0100491 case POWER_HINT_LAUNCH:
Christopher N. Hesse3fa2b692017-04-05 18:52:48 +0200492 ALOGV("%s: POWER_HINT_LAUNCH", __func__);
493 send_boostpulse(samsung_pwr->boostpulse_fd);
494 break;
Christopher N. Hesse5fada9b2017-01-16 23:39:48 +0100495 case POWER_HINT_CPU_BOOST:
Christopher N. Hesse3fa2b692017-04-05 18:52:48 +0200496 ALOGV("%s: POWER_HINT_CPU_BOOST", __func__);
Christopher N. Hesse5fada9b2017-01-16 23:39:48 +0100497 boost((*(int32_t *)data));
498 break;
Christopher N. Hesse2981f792017-03-12 17:08:47 +0100499 case POWER_HINT_SET_PROFILE:
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100500 ALOGV("%s: POWER_HINT_SET_PROFILE", __func__);
Christopher N. Hesse2981f792017-03-12 17:08:47 +0100501 int profile = *((intptr_t *)data);
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100502 set_power_profile(samsung_pwr, profile);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100503 break;
Christopher N. Hessef9b8e832017-01-17 00:04:45 +0100504 case POWER_HINT_DISABLE_TOUCH:
505 ALOGV("%s: POWER_HINT_DISABLE_TOUCH", __func__);
Christopher N. Hesse3fa2b692017-04-05 18:52:48 +0200506 sysfs_write(samsung_pwr->touchscreen_power_path, data ? "0" : "1");
Christopher N. Hessef9b8e832017-01-17 00:04:45 +0100507 break;
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100508 default:
Christopher N. Hesse2981f792017-03-12 17:08:47 +0100509 ALOGW("%s: Unknown power hint: %d", __func__, hint);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100510 break;
511 }
512}
513
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100514static int samsung_get_feature(struct power_module *module __unused,
515 feature_t feature)
516{
517 if (feature == POWER_FEATURE_SUPPORTED_PROFILES) {
Christopher N. Hesse58f2ca02017-01-17 00:01:15 +0100518 return PROFILE_MAX;
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100519 }
520
521 return -1;
522}
523
Christopher N. Hesse1c474662016-11-18 18:59:06 +0100524static void samsung_set_feature(struct power_module *module, feature_t feature, int state __unused)
Christopher N. Hessee480d892016-06-22 23:04:39 +0200525{
526 struct samsung_power_module *samsung_pwr = (struct samsung_power_module *) module;
527
528 switch (feature) {
ishantvivek987dcca2016-11-21 06:05:47 +0000529#ifdef TARGET_TAP_TO_WAKE_NODE
Christopher N. Hessee480d892016-06-22 23:04:39 +0200530 case POWER_FEATURE_DOUBLE_TAP_TO_WAKE:
531 ALOGV("%s: %s double tap to wake", __func__, state ? "enabling" : "disabling");
ishantvivek987dcca2016-11-21 06:05:47 +0000532 sysfs_write(TARGET_TAP_TO_WAKE_NODE, state > 0 ? "1" : "0");
Christopher N. Hessee480d892016-06-22 23:04:39 +0200533 break;
534#endif
535 default:
536 break;
537 }
538}
539
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100540static struct hw_module_methods_t power_module_methods = {
541 .open = NULL,
542};
543
544struct samsung_power_module HAL_MODULE_INFO_SYM = {
545 .base = {
546 .common = {
547 .tag = HARDWARE_MODULE_TAG,
548 .module_api_version = POWER_MODULE_API_VERSION_0_2,
549 .hal_api_version = HARDWARE_HAL_API_VERSION,
550 .id = POWER_HARDWARE_MODULE_ID,
551 .name = "Samsung Power HAL",
Christopher N. Hesseaa75be42017-01-16 22:47:59 +0100552 .author = "The LineageOS Project",
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100553 .methods = &power_module_methods,
554 },
555
556 .init = samsung_power_init,
557 .setInteractive = samsung_power_set_interactive,
558 .powerHint = samsung_power_hint,
Christopher N. Hessee480d892016-06-22 23:04:39 +0200559 .getFeature = samsung_get_feature,
560 .setFeature = samsung_set_feature
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100561 },
562
563 .lock = PTHREAD_MUTEX_INITIALIZER,
564 .boostpulse_fd = -1,
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100565};