blob: f9b268c184d569d4c0f890a7b8933def4a29a6ab [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;
Christopher N. Hessefb5669e2017-07-08 12:44:39 +020058 int boost_fd;
Christopher N. Hessede5e3c62015-12-21 21:28:23 +010059 int boostpulse_fd;
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +020060 char hispeed_freqs[CLUSTER_COUNT][PARAM_MAXLEN];
61 char max_freqs[CLUSTER_COUNT][PARAM_MAXLEN];
Christopher N. Hessede5e3c62015-12-21 21:28:23 +010062 char* touchscreen_power_path;
63 char* touchkey_power_path;
Christopher N. Hessede5e3c62015-12-21 21:28:23 +010064};
65
Andreas Schneiderf15d7f42016-02-03 10:43:47 +010066enum power_profile_e {
67 PROFILE_POWER_SAVE = 0,
68 PROFILE_BALANCED,
Christopher N. Hesse58f2ca02017-01-17 00:01:15 +010069 PROFILE_HIGH_PERFORMANCE,
70 PROFILE_MAX
Andreas Schneiderf15d7f42016-02-03 10:43:47 +010071};
Christopher N. Hesseccd970d2017-01-25 22:41:05 +010072
Andreas Schneiderf15d7f42016-02-03 10:43:47 +010073static enum power_profile_e current_power_profile = PROFILE_BALANCED;
Christopher N. Hessede5e3c62015-12-21 21:28:23 +010074
Stricted404b41c2018-09-07 01:16:30 +020075// Custom Lineage hints
76const static power_hint_t POWER_HINT_CPU_BOOST = (power_hint_t)0x00000110;
77const static power_hint_t POWER_HINT_SET_PROFILE = (power_hint_t)0x00000111;
78
Christopher N. Hessede5e3c62015-12-21 21:28:23 +010079/**********************************************************
80 *** HELPER FUNCTIONS
81 **********************************************************/
82
83static int sysfs_read(char *path, char *s, int num_bytes)
84{
85 char errno_str[64];
86 int len;
87 int ret = 0;
88 int fd;
89
90 fd = open(path, O_RDONLY);
91 if (fd < 0) {
92 strerror_r(errno, errno_str, sizeof(errno_str));
Christopher N. Hesse354f7132017-03-04 01:06:22 +010093 ALOGE("Error opening %s: %s", path, errno_str);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +010094
95 return -1;
96 }
97
98 len = read(fd, s, num_bytes - 1);
99 if (len < 0) {
100 strerror_r(errno, errno_str, sizeof(errno_str));
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100101 ALOGE("Error reading from %s: %s", path, errno_str);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100102
103 ret = -1;
104 } else {
Christopher N. Hesse0da5fbf2017-07-08 13:56:25 +0200105 // do not store newlines, but terminate the string instead
106 if (s[len-1] == '\n') {
107 s[len-1] = '\0';
108 } else {
109 s[len] = '\0';
110 }
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100111 }
112
113 close(fd);
114
115 return ret;
116}
117
118static void sysfs_write(const char *path, char *s)
119{
120 char errno_str[64];
121 int len;
122 int fd;
123
124 fd = open(path, O_WRONLY);
125 if (fd < 0) {
126 strerror_r(errno, errno_str, sizeof(errno_str));
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100127 ALOGE("Error opening %s: %s", path, errno_str);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100128 return;
129 }
130
131 len = write(fd, s, strlen(s));
132 if (len < 0) {
133 strerror_r(errno, errno_str, sizeof(errno_str));
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100134 ALOGE("Error writing to %s: %s", path, errno_str);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100135 }
136
137 close(fd);
138}
139
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200140static void cpu_sysfs_read(const char *param, char s[CLUSTER_COUNT][PARAM_MAXLEN])
141{
142 char path[PATH_MAX];
143
144 for (unsigned int i = 0; i < ARRAY_SIZE(CPU_SYSFS_PATHS); i++) {
145 sprintf(path, "%s%s", CPU_SYSFS_PATHS[i], param);
146 sysfs_read(path, s[i], PARAM_MAXLEN);
147 }
148}
149
150static void cpu_sysfs_write(const char *param, char s[CLUSTER_COUNT][PARAM_MAXLEN])
151{
152 char path[PATH_MAX];
153
154 for (unsigned int i = 0; i < ARRAY_SIZE(CPU_SYSFS_PATHS); i++) {
155 sprintf(path, "%s%s", CPU_SYSFS_PATHS[i], param);
156 sysfs_write(path, s[i]);
157 }
158}
159
160static void cpu_interactive_read(const char *param, char s[CLUSTER_COUNT][PARAM_MAXLEN])
161{
162 char path[PATH_MAX];
163
164 for (unsigned int i = 0; i < ARRAY_SIZE(CPU_INTERACTIVE_PATHS); i++) {
165 sprintf(path, "%s%s", CPU_INTERACTIVE_PATHS[i], param);
166 sysfs_read(path, s[i], PARAM_MAXLEN);
167 }
168}
169
170static void cpu_interactive_write(const char *param, char s[CLUSTER_COUNT][PARAM_MAXLEN])
171{
172 char path[PATH_MAX];
173
174 for (unsigned int i = 0; i < ARRAY_SIZE(CPU_INTERACTIVE_PATHS); i++) {
175 sprintf(path, "%s%s", CPU_INTERACTIVE_PATHS[i], param);
176 sysfs_write(path, s[i]);
177 }
178}
179
Christopher N. Hessefb5669e2017-07-08 12:44:39 +0200180static void send_boost(int boost_fd, int32_t duration_us)
Christopher N. Hesse5fada9b2017-01-16 23:39:48 +0100181{
Christopher N. Hessefb5669e2017-07-08 12:44:39 +0200182 int len;
Christopher N. Hesse5fada9b2017-01-16 23:39:48 +0100183
Christopher N. Hessefb5669e2017-07-08 12:44:39 +0200184 if (boost_fd < 0) {
Christopher N. Hesse5fada9b2017-01-16 23:39:48 +0100185 return;
186 }
187
Christopher N. Hessefb5669e2017-07-08 12:44:39 +0200188 len = write(boost_fd, "1", 1);
189 if (len < 0) {
190 ALOGE("Error writing to %s%s: %s", CPU_INTERACTIVE_PATHS[0], BOOST_PATH, strerror(errno));
191 return;
192 }
193
Christopher N. Hesse5fada9b2017-01-16 23:39:48 +0100194 usleep(duration_us);
Christopher N. Hessefb5669e2017-07-08 12:44:39 +0200195 len = write(boost_fd, "0", 1);
Christopher N. Hessef21fbdb2017-03-04 01:41:58 +0100196}
197
198static void send_boostpulse(int boostpulse_fd)
199{
200 int len;
201
202 if (boostpulse_fd < 0) {
203 return;
204 }
205
206 len = write(boostpulse_fd, "1", 1);
207 if (len < 0) {
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200208 ALOGE("Error writing to %s%s: %s", CPU_INTERACTIVE_PATHS[0], BOOSTPULSE_PATH,
209 strerror(errno));
Christopher N. Hessef21fbdb2017-03-04 01:41:58 +0100210 }
211}
212
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100213/**********************************************************
214 *** POWER FUNCTIONS
215 **********************************************************/
216
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100217static void set_power_profile(struct samsung_power_module *samsung_pwr,
Christopher N. Hesse58f2ca02017-01-17 00:01:15 +0100218 int profile)
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100219{
Christopher N. Hesse58f2ca02017-01-17 00:01:15 +0100220 if (profile < 0 || profile >= PROFILE_MAX) {
221 return;
222 }
223
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100224 if (current_power_profile == profile) {
225 return;
226 }
227
228 ALOGV("%s: profile=%d", __func__, profile);
229
230 switch (profile) {
231 case PROFILE_POWER_SAVE:
Martin Bouchet4bf8a792017-03-28 04:00:04 -0300232 // Grab value set by init.*.rc
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200233 cpu_interactive_read(HISPEED_FREQ_PATH, samsung_pwr->hispeed_freqs);
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100234 // Limit to hispeed freq
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200235 cpu_sysfs_write(MAX_FREQ_PATH, samsung_pwr->hispeed_freqs);
Christopher N. Hessef05f9022017-01-16 22:49:06 +0100236 ALOGV("%s: set powersave mode", __func__);
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100237 break;
238 case PROFILE_BALANCED:
239 // Restore normal max freq
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200240 cpu_sysfs_write(MAX_FREQ_PATH, samsung_pwr->max_freqs);
Christopher N. Hessef05f9022017-01-16 22:49:06 +0100241 ALOGV("%s: set balanced mode", __func__);
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100242 break;
243 case PROFILE_HIGH_PERFORMANCE:
244 // Restore normal max freq
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200245 cpu_sysfs_write(MAX_FREQ_PATH, samsung_pwr->max_freqs);
Christopher N. Hessef05f9022017-01-16 22:49:06 +0100246 ALOGV("%s: set performance mode", __func__);
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100247 break;
Christopher N. Hesse2981f792017-03-12 17:08:47 +0100248 default:
249 ALOGW("%s: Unknown power profile: %d", __func__, profile);
250 return;
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100251 }
252
253 current_power_profile = profile;
254}
255
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100256static void find_input_nodes(struct samsung_power_module *samsung_pwr, char *dir)
257{
258 const char filename[] = "name";
259 char errno_str[64];
260 struct dirent *de;
261 char file_content[20];
262 char *path = NULL;
263 char *node_path = NULL;
264 size_t pathsize;
265 size_t node_pathsize;
266 DIR *d;
267
268 d = opendir(dir);
269 if (d == NULL) {
270 return;
271 }
272
273 while ((de = readdir(d)) != NULL) {
274 if (strncmp(filename, de->d_name, sizeof(filename)) == 0) {
275 pathsize = strlen(dir) + strlen(de->d_name) + 2;
276 node_pathsize = strlen(dir) + strlen("enabled") + 2;
277
278 path = malloc(pathsize);
279 node_path = malloc(node_pathsize);
280 if (path == NULL || node_path == NULL) {
281 strerror_r(errno, errno_str, sizeof(errno_str));
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100282 ALOGE("Out of memory: %s", errno_str);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100283 return;
284 }
285
286 snprintf(path, pathsize, "%s/%s", dir, filename);
287 sysfs_read(path, file_content, sizeof(file_content));
288
289 snprintf(node_path, node_pathsize, "%s/%s", dir, "enabled");
290
291 if (strncmp(file_content, "sec_touchkey", 12) == 0) {
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100292 ALOGV("%s: found touchkey path: %s", __func__, node_path);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100293 samsung_pwr->touchkey_power_path = malloc(node_pathsize);
294 if (samsung_pwr->touchkey_power_path == NULL) {
295 strerror_r(errno, errno_str, sizeof(errno_str));
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100296 ALOGE("Out of memory: %s", errno_str);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100297 return;
298 }
Christopher N. Hesse22da3132016-02-01 12:36:54 +0100299 snprintf(samsung_pwr->touchkey_power_path, node_pathsize,
300 "%s", node_path);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100301 }
302
303 if (strncmp(file_content, "sec_touchscreen", 15) == 0) {
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100304 ALOGV("%s: found touchscreen path: %s", __func__, node_path);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100305 samsung_pwr->touchscreen_power_path = malloc(node_pathsize);
306 if (samsung_pwr->touchscreen_power_path == NULL) {
307 strerror_r(errno, errno_str, sizeof(errno_str));
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100308 ALOGE("Out of memory: %s", errno_str);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100309 return;
310 }
Christopher N. Hesse22da3132016-02-01 12:36:54 +0100311 snprintf(samsung_pwr->touchscreen_power_path, node_pathsize,
312 "%s", node_path);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100313 }
314 }
315 }
316
317 if (path)
318 free(path);
319 if (node_path)
320 free(node_path);
321 closedir(d);
322}
323
324/**********************************************************
325 *** INIT FUNCTIONS
326 **********************************************************/
327
328static void init_cpufreqs(struct samsung_power_module *samsung_pwr)
329{
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200330 cpu_interactive_read(HISPEED_FREQ_PATH, samsung_pwr->hispeed_freqs);
331 cpu_sysfs_read(MAX_FREQ_PATH, samsung_pwr->max_freqs);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100332}
333
334static void init_touch_input_power_path(struct samsung_power_module *samsung_pwr)
335{
336 char dir[1024];
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100337 uint32_t i;
338
339 for (i = 0; i < 20; i++) {
340 snprintf(dir, sizeof(dir), "/sys/class/input/input%d", i);
341 find_input_nodes(samsung_pwr, dir);
342 }
343}
344
Christopher N. Hessefb5669e2017-07-08 12:44:39 +0200345static void boost_open(struct samsung_power_module *samsung_pwr)
346{
347 char path[PATH_MAX];
348
349 // the boost node is only valid for the LITTLE cluster
350 sprintf(path, "%s%s", CPU_INTERACTIVE_PATHS[0], BOOST_PATH);
351
352 samsung_pwr->boost_fd = open(path, O_WRONLY);
353 if (samsung_pwr->boost_fd < 0) {
354 ALOGE("Error opening %s: %s\n", path, strerror(errno));
355 }
356}
357
358static void boostpulse_open(struct samsung_power_module *samsung_pwr)
359{
360 char path[PATH_MAX];
361
362 // the boostpulse node is only valid for the LITTLE cluster
363 sprintf(path, "%s%s", CPU_INTERACTIVE_PATHS[0], BOOSTPULSE_PATH);
364
365 samsung_pwr->boostpulse_fd = open(path, O_WRONLY);
366 if (samsung_pwr->boostpulse_fd < 0) {
367 ALOGE("Error opening %s: %s\n", path, strerror(errno));
368 }
369}
370
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100371static void samsung_power_init(struct power_module *module)
372{
373 struct samsung_power_module *samsung_pwr = (struct samsung_power_module *) module;
374
375 init_cpufreqs(samsung_pwr);
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100376
Christopher N. Hessefb5669e2017-07-08 12:44:39 +0200377 // keep interactive boost fds opened
378 boost_open(samsung_pwr);
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100379 boostpulse_open(samsung_pwr);
380
Christopher N. Hesse65c65bd2017-03-07 23:01:12 +0100381 samsung_pwr->touchscreen_power_path = NULL;
382 samsung_pwr->touchkey_power_path = NULL;
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100383 init_touch_input_power_path(samsung_pwr);
Christopher N. Hesse775bce82017-07-08 13:46:35 +0200384
385 ALOGI("Initialized settings:");
Christopher N. Hesse1146f1a2017-07-13 21:14:57 +0200386 char max_freqs[PATH_MAX];
387 sprintf(max_freqs, "max_freqs: cluster[0]: %s", samsung_pwr->max_freqs[0]);
388 for (unsigned int i = 1; i < CLUSTER_COUNT; i++) {
389 sprintf(max_freqs, "%s, %s[%d]: %s", max_freqs, "cluster", i, samsung_pwr->max_freqs[i]);
390 }
391 ALOGI("%s", max_freqs);
392 char hispeed_freqs[PATH_MAX];
393 sprintf(hispeed_freqs, "hispeed_freqs: cluster[0]: %s", samsung_pwr->hispeed_freqs[0]);
394 for (unsigned int i = 1; i < CLUSTER_COUNT; i++) {
395 sprintf(hispeed_freqs, "%s, %s[%d]: %s", hispeed_freqs, "cluster", i,
396 samsung_pwr->hispeed_freqs[i]);
397 }
398 ALOGI("%s", hispeed_freqs);
Christopher N. Hesseff868462017-07-09 23:07:09 +0200399 ALOGI("boostpulse_fd: %d", samsung_pwr->boostpulse_fd);
Christopher N. Hesse775bce82017-07-08 13:46:35 +0200400 ALOGI("touchscreen_power_path: %s",
401 samsung_pwr->touchscreen_power_path ? samsung_pwr->touchscreen_power_path : "NULL");
402 ALOGI("touchkey_power_path: %s",
403 samsung_pwr->touchkey_power_path ? samsung_pwr->touchkey_power_path : "NULL");
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100404}
405
Christopher N. Hessef6296722017-01-16 22:47:11 +0100406/**********************************************************
407 *** API FUNCTIONS
408 ***
409 *** Refer to power.h for documentation.
410 **********************************************************/
411
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100412static void samsung_power_set_interactive(struct power_module *module, int on)
413{
414 struct samsung_power_module *samsung_pwr = (struct samsung_power_module *) module;
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100415 int panel_brightness;
416 char button_state[2];
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100417 int rc;
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100418 static bool touchkeys_blocked = false;
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200419 char ON[CLUSTER_COUNT][PARAM_MAXLEN] = {"1", "1"};
420 char OFF[CLUSTER_COUNT][PARAM_MAXLEN] = {"0", "0"};
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100421
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100422 ALOGV("power_set_interactive: %d", on);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100423
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100424 /*
425 * Do not disable any input devices if the screen is on but we are in a non-interactive
426 * state.
427 */
Christopher N. Hesse3360b092016-07-11 15:48:35 +0200428 if (!on) {
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100429 panel_brightness = get_cur_panel_brightness();
430 if (panel_brightness < 0) {
431 ALOGE("%s: Failed to read panel brightness", __func__);
432 } else if (panel_brightness > 0) {
Christopher N. Hesse3360b092016-07-11 15:48:35 +0200433 ALOGV("%s: Moving to non-interactive state, but screen is still on,"
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100434 " not disabling input devices", __func__);
Christopher N. Hesse3360b092016-07-11 15:48:35 +0200435 goto out;
436 }
437 }
438
Paul Keith5e4fbe02017-04-21 21:34:17 -0500439 /* Sanity check the touchscreen path */
440 if (samsung_pwr->touchscreen_power_path) {
441 sysfs_write(samsung_pwr->touchscreen_power_path, on ? "1" : "0");
442 }
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100443
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100444 /* Bail out if the device does not have touchkeys */
445 if (samsung_pwr->touchkey_power_path == NULL) {
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100446 goto out;
447 }
448
449 if (!on) {
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100450 rc = sysfs_read(samsung_pwr->touchkey_power_path, button_state, ARRAY_SIZE(button_state));
451 if (rc < 0) {
452 ALOGE("%s: Failed to read touchkey state", __func__);
453 goto out;
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100454 }
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100455 /*
456 * If button_state is 0, the keys have been disabled by another component
457 * (for example cmhw), which means we don't want them to be enabled when resuming
458 * from suspend.
459 */
460 if (button_state[0] == '0') {
461 touchkeys_blocked = true;
462 } else {
463 touchkeys_blocked = false;
464 }
465 }
466
467 if (!touchkeys_blocked) {
468 sysfs_write(samsung_pwr->touchkey_power_path, on ? "1" : "0");
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100469 }
470
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100471out:
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200472 cpu_interactive_write(IO_IS_BUSY_PATH, on ? ON : OFF);
Christopher N. Hesse63b5bd72017-05-29 20:45:14 +0200473
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100474 ALOGV("power_set_interactive: %d done", on);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100475}
476
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100477static void samsung_power_hint(struct power_module *module,
478 power_hint_t hint,
479 void *data)
480{
481 struct samsung_power_module *samsung_pwr = (struct samsung_power_module *) module;
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100482
Christopher N. Hesse15b63092017-03-12 17:04:53 +0100483 /* Bail out if low-power mode is active */
Christopher N. Hesse5eca5aa2017-04-26 21:42:10 +0200484 if (current_power_profile == PROFILE_POWER_SAVE && hint != POWER_HINT_LOW_POWER
Christopher N. Hesse62b70f82018-02-19 23:20:14 +0100485 && hint != POWER_HINT_SET_PROFILE && hint != POWER_HINT_DISABLE_TOUCH) {
Christopher N. Hesse4f7dc6f2018-02-19 23:17:09 +0100486 ALOGV("%s: PROFILE_POWER_SAVE active, ignoring hint %d", __func__, hint);
Christopher N. Hesse15b63092017-03-12 17:04:53 +0100487 return;
488 }
489
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100490 switch (hint) {
Christopher N. Hesse2981f792017-03-12 17:08:47 +0100491 case POWER_HINT_VSYNC:
492 ALOGV("%s: POWER_HINT_VSYNC", __func__);
493 break;
494 case POWER_HINT_INTERACTION:
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100495 ALOGV("%s: POWER_HINT_INTERACTION", __func__);
Christopher N. Hessef21fbdb2017-03-04 01:41:58 +0100496 send_boostpulse(samsung_pwr->boostpulse_fd);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100497 break;
Christopher N. Hesse2981f792017-03-12 17:08:47 +0100498 case POWER_HINT_LOW_POWER:
499 ALOGV("%s: POWER_HINT_LOW_POWER", __func__);
Christopher N. Hesse3fa2b692017-04-05 18:52:48 +0200500 set_power_profile(samsung_pwr, data ? PROFILE_POWER_SAVE : PROFILE_BALANCED);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100501 break;
Christopher N. Hesse2981f792017-03-12 17:08:47 +0100502 case POWER_HINT_LAUNCH:
Christopher N. Hesse3fa2b692017-04-05 18:52:48 +0200503 ALOGV("%s: POWER_HINT_LAUNCH", __func__);
504 send_boostpulse(samsung_pwr->boostpulse_fd);
505 break;
Christopher N. Hesse5fada9b2017-01-16 23:39:48 +0100506 case POWER_HINT_CPU_BOOST:
Christopher N. Hesse3fa2b692017-04-05 18:52:48 +0200507 ALOGV("%s: POWER_HINT_CPU_BOOST", __func__);
Christopher N. Hessefb5669e2017-07-08 12:44:39 +0200508 int32_t duration_us = *((int32_t *)data);
509 send_boost(samsung_pwr->boost_fd, duration_us);
Christopher N. Hesse5fada9b2017-01-16 23:39:48 +0100510 break;
Christopher N. Hesse2981f792017-03-12 17:08:47 +0100511 case POWER_HINT_SET_PROFILE:
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100512 ALOGV("%s: POWER_HINT_SET_PROFILE", __func__);
Christopher N. Hesse2981f792017-03-12 17:08:47 +0100513 int profile = *((intptr_t *)data);
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100514 set_power_profile(samsung_pwr, profile);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100515 break;
Christopher N. Hessef9b8e832017-01-17 00:04:45 +0100516 case POWER_HINT_DISABLE_TOUCH:
517 ALOGV("%s: POWER_HINT_DISABLE_TOUCH", __func__);
Christopher N. Hesse3fa2b692017-04-05 18:52:48 +0200518 sysfs_write(samsung_pwr->touchscreen_power_path, data ? "0" : "1");
Christopher N. Hessef9b8e832017-01-17 00:04:45 +0100519 break;
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100520 default:
Christopher N. Hesse2981f792017-03-12 17:08:47 +0100521 ALOGW("%s: Unknown power hint: %d", __func__, hint);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100522 break;
523 }
524}
525
Stricted1a871752018-08-28 07:30:40 +0200526static void samsung_set_feature(struct power_module *module __unused, feature_t feature, int state __unused)
Christopher N. Hessee480d892016-06-22 23:04:39 +0200527{
Christopher N. Hessee480d892016-06-22 23:04:39 +0200528 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 .setFeature = samsung_set_feature
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100560 },
561
562 .lock = PTHREAD_MUTEX_INITIALIZER,
563 .boostpulse_fd = -1,
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100564};