blob: b6e8502b13b6f1c9cd32ba1f2c28940829c0eb71 [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
75/**********************************************************
76 *** HELPER FUNCTIONS
77 **********************************************************/
78
79static int sysfs_read(char *path, char *s, int num_bytes)
80{
81 char errno_str[64];
82 int len;
83 int ret = 0;
84 int fd;
85
86 fd = open(path, O_RDONLY);
87 if (fd < 0) {
88 strerror_r(errno, errno_str, sizeof(errno_str));
Christopher N. Hesse354f7132017-03-04 01:06:22 +010089 ALOGE("Error opening %s: %s", path, errno_str);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +010090
91 return -1;
92 }
93
94 len = read(fd, s, num_bytes - 1);
95 if (len < 0) {
96 strerror_r(errno, errno_str, sizeof(errno_str));
Christopher N. Hesse354f7132017-03-04 01:06:22 +010097 ALOGE("Error reading from %s: %s", path, errno_str);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +010098
99 ret = -1;
100 } else {
Christopher N. Hesse0da5fbf2017-07-08 13:56:25 +0200101 // do not store newlines, but terminate the string instead
102 if (s[len-1] == '\n') {
103 s[len-1] = '\0';
104 } else {
105 s[len] = '\0';
106 }
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100107 }
108
109 close(fd);
110
111 return ret;
112}
113
114static void sysfs_write(const char *path, char *s)
115{
116 char errno_str[64];
117 int len;
118 int fd;
119
120 fd = open(path, O_WRONLY);
121 if (fd < 0) {
122 strerror_r(errno, errno_str, sizeof(errno_str));
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100123 ALOGE("Error opening %s: %s", path, errno_str);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100124 return;
125 }
126
127 len = write(fd, s, strlen(s));
128 if (len < 0) {
129 strerror_r(errno, errno_str, sizeof(errno_str));
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100130 ALOGE("Error writing to %s: %s", path, errno_str);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100131 }
132
133 close(fd);
134}
135
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200136static void cpu_sysfs_read(const char *param, char s[CLUSTER_COUNT][PARAM_MAXLEN])
137{
138 char path[PATH_MAX];
139
140 for (unsigned int i = 0; i < ARRAY_SIZE(CPU_SYSFS_PATHS); i++) {
141 sprintf(path, "%s%s", CPU_SYSFS_PATHS[i], param);
142 sysfs_read(path, s[i], PARAM_MAXLEN);
143 }
144}
145
146static void cpu_sysfs_write(const char *param, char s[CLUSTER_COUNT][PARAM_MAXLEN])
147{
148 char path[PATH_MAX];
149
150 for (unsigned int i = 0; i < ARRAY_SIZE(CPU_SYSFS_PATHS); i++) {
151 sprintf(path, "%s%s", CPU_SYSFS_PATHS[i], param);
152 sysfs_write(path, s[i]);
153 }
154}
155
156static void cpu_interactive_read(const char *param, char s[CLUSTER_COUNT][PARAM_MAXLEN])
157{
158 char path[PATH_MAX];
159
160 for (unsigned int i = 0; i < ARRAY_SIZE(CPU_INTERACTIVE_PATHS); i++) {
161 sprintf(path, "%s%s", CPU_INTERACTIVE_PATHS[i], param);
162 sysfs_read(path, s[i], PARAM_MAXLEN);
163 }
164}
165
166static void cpu_interactive_write(const char *param, char s[CLUSTER_COUNT][PARAM_MAXLEN])
167{
168 char path[PATH_MAX];
169
170 for (unsigned int i = 0; i < ARRAY_SIZE(CPU_INTERACTIVE_PATHS); i++) {
171 sprintf(path, "%s%s", CPU_INTERACTIVE_PATHS[i], param);
172 sysfs_write(path, s[i]);
173 }
174}
175
Christopher N. Hessefb5669e2017-07-08 12:44:39 +0200176static void send_boost(int boost_fd, int32_t duration_us)
Christopher N. Hesse5fada9b2017-01-16 23:39:48 +0100177{
Christopher N. Hessefb5669e2017-07-08 12:44:39 +0200178 int len;
Christopher N. Hesse5fada9b2017-01-16 23:39:48 +0100179
Christopher N. Hessefb5669e2017-07-08 12:44:39 +0200180 if (boost_fd < 0) {
Christopher N. Hesse5fada9b2017-01-16 23:39:48 +0100181 return;
182 }
183
Christopher N. Hessefb5669e2017-07-08 12:44:39 +0200184 len = write(boost_fd, "1", 1);
185 if (len < 0) {
186 ALOGE("Error writing to %s%s: %s", CPU_INTERACTIVE_PATHS[0], BOOST_PATH, strerror(errno));
187 return;
188 }
189
Christopher N. Hesse5fada9b2017-01-16 23:39:48 +0100190 usleep(duration_us);
Christopher N. Hessefb5669e2017-07-08 12:44:39 +0200191 len = write(boost_fd, "0", 1);
Christopher N. Hessef21fbdb2017-03-04 01:41:58 +0100192}
193
194static void send_boostpulse(int boostpulse_fd)
195{
196 int len;
197
198 if (boostpulse_fd < 0) {
199 return;
200 }
201
202 len = write(boostpulse_fd, "1", 1);
203 if (len < 0) {
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200204 ALOGE("Error writing to %s%s: %s", CPU_INTERACTIVE_PATHS[0], BOOSTPULSE_PATH,
205 strerror(errno));
Christopher N. Hessef21fbdb2017-03-04 01:41:58 +0100206 }
207}
208
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100209/**********************************************************
210 *** POWER FUNCTIONS
211 **********************************************************/
212
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100213static void set_power_profile(struct samsung_power_module *samsung_pwr,
Christopher N. Hesse58f2ca02017-01-17 00:01:15 +0100214 int profile)
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100215{
216 int rc;
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100217
Christopher N. Hesse58f2ca02017-01-17 00:01:15 +0100218 if (profile < 0 || profile >= PROFILE_MAX) {
219 return;
220 }
221
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100222 if (current_power_profile == profile) {
223 return;
224 }
225
226 ALOGV("%s: profile=%d", __func__, profile);
227
228 switch (profile) {
229 case PROFILE_POWER_SAVE:
Martin Bouchet4bf8a792017-03-28 04:00:04 -0300230 // Grab value set by init.*.rc
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200231 cpu_interactive_read(HISPEED_FREQ_PATH, samsung_pwr->hispeed_freqs);
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100232 // Limit to hispeed freq
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200233 cpu_sysfs_write(MAX_FREQ_PATH, samsung_pwr->hispeed_freqs);
Christopher N. Hessef05f9022017-01-16 22:49:06 +0100234 ALOGV("%s: set powersave mode", __func__);
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100235 break;
236 case PROFILE_BALANCED:
237 // Restore normal max freq
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200238 cpu_sysfs_write(MAX_FREQ_PATH, samsung_pwr->max_freqs);
Christopher N. Hessef05f9022017-01-16 22:49:06 +0100239 ALOGV("%s: set balanced mode", __func__);
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100240 break;
241 case PROFILE_HIGH_PERFORMANCE:
242 // Restore normal max freq
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200243 cpu_sysfs_write(MAX_FREQ_PATH, samsung_pwr->max_freqs);
Christopher N. Hessef05f9022017-01-16 22:49:06 +0100244 ALOGV("%s: set performance mode", __func__);
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100245 break;
Christopher N. Hesse2981f792017-03-12 17:08:47 +0100246 default:
247 ALOGW("%s: Unknown power profile: %d", __func__, profile);
248 return;
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100249 }
250
251 current_power_profile = profile;
252}
253
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100254static void find_input_nodes(struct samsung_power_module *samsung_pwr, char *dir)
255{
256 const char filename[] = "name";
257 char errno_str[64];
258 struct dirent *de;
259 char file_content[20];
260 char *path = NULL;
261 char *node_path = NULL;
262 size_t pathsize;
263 size_t node_pathsize;
264 DIR *d;
265
266 d = opendir(dir);
267 if (d == NULL) {
268 return;
269 }
270
271 while ((de = readdir(d)) != NULL) {
272 if (strncmp(filename, de->d_name, sizeof(filename)) == 0) {
273 pathsize = strlen(dir) + strlen(de->d_name) + 2;
274 node_pathsize = strlen(dir) + strlen("enabled") + 2;
275
276 path = malloc(pathsize);
277 node_path = malloc(node_pathsize);
278 if (path == NULL || node_path == NULL) {
279 strerror_r(errno, errno_str, sizeof(errno_str));
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100280 ALOGE("Out of memory: %s", errno_str);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100281 return;
282 }
283
284 snprintf(path, pathsize, "%s/%s", dir, filename);
285 sysfs_read(path, file_content, sizeof(file_content));
286
287 snprintf(node_path, node_pathsize, "%s/%s", dir, "enabled");
288
289 if (strncmp(file_content, "sec_touchkey", 12) == 0) {
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100290 ALOGV("%s: found touchkey path: %s", __func__, node_path);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100291 samsung_pwr->touchkey_power_path = malloc(node_pathsize);
292 if (samsung_pwr->touchkey_power_path == NULL) {
293 strerror_r(errno, errno_str, sizeof(errno_str));
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100294 ALOGE("Out of memory: %s", errno_str);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100295 return;
296 }
Christopher N. Hesse22da3132016-02-01 12:36:54 +0100297 snprintf(samsung_pwr->touchkey_power_path, node_pathsize,
298 "%s", node_path);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100299 }
300
301 if (strncmp(file_content, "sec_touchscreen", 15) == 0) {
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100302 ALOGV("%s: found touchscreen path: %s", __func__, node_path);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100303 samsung_pwr->touchscreen_power_path = malloc(node_pathsize);
304 if (samsung_pwr->touchscreen_power_path == NULL) {
305 strerror_r(errno, errno_str, sizeof(errno_str));
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100306 ALOGE("Out of memory: %s", errno_str);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100307 return;
308 }
Christopher N. Hesse22da3132016-02-01 12:36:54 +0100309 snprintf(samsung_pwr->touchscreen_power_path, node_pathsize,
310 "%s", node_path);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100311 }
312 }
313 }
314
315 if (path)
316 free(path);
317 if (node_path)
318 free(node_path);
319 closedir(d);
320}
321
322/**********************************************************
323 *** INIT FUNCTIONS
324 **********************************************************/
325
326static void init_cpufreqs(struct samsung_power_module *samsung_pwr)
327{
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200328 cpu_interactive_read(HISPEED_FREQ_PATH, samsung_pwr->hispeed_freqs);
329 cpu_sysfs_read(MAX_FREQ_PATH, samsung_pwr->max_freqs);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100330}
331
332static void init_touch_input_power_path(struct samsung_power_module *samsung_pwr)
333{
334 char dir[1024];
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100335 uint32_t i;
336
337 for (i = 0; i < 20; i++) {
338 snprintf(dir, sizeof(dir), "/sys/class/input/input%d", i);
339 find_input_nodes(samsung_pwr, dir);
340 }
341}
342
Christopher N. Hessefb5669e2017-07-08 12:44:39 +0200343static void boost_open(struct samsung_power_module *samsung_pwr)
344{
345 char path[PATH_MAX];
346
347 // the boost node is only valid for the LITTLE cluster
348 sprintf(path, "%s%s", CPU_INTERACTIVE_PATHS[0], BOOST_PATH);
349
350 samsung_pwr->boost_fd = open(path, O_WRONLY);
351 if (samsung_pwr->boost_fd < 0) {
352 ALOGE("Error opening %s: %s\n", path, strerror(errno));
353 }
354}
355
356static void boostpulse_open(struct samsung_power_module *samsung_pwr)
357{
358 char path[PATH_MAX];
359
360 // the boostpulse node is only valid for the LITTLE cluster
361 sprintf(path, "%s%s", CPU_INTERACTIVE_PATHS[0], BOOSTPULSE_PATH);
362
363 samsung_pwr->boostpulse_fd = open(path, O_WRONLY);
364 if (samsung_pwr->boostpulse_fd < 0) {
365 ALOGE("Error opening %s: %s\n", path, strerror(errno));
366 }
367}
368
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100369static void samsung_power_init(struct power_module *module)
370{
371 struct samsung_power_module *samsung_pwr = (struct samsung_power_module *) module;
372
373 init_cpufreqs(samsung_pwr);
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100374
Christopher N. Hessefb5669e2017-07-08 12:44:39 +0200375 // keep interactive boost fds opened
376 boost_open(samsung_pwr);
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100377 boostpulse_open(samsung_pwr);
378
Christopher N. Hesse65c65bd2017-03-07 23:01:12 +0100379 samsung_pwr->touchscreen_power_path = NULL;
380 samsung_pwr->touchkey_power_path = NULL;
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100381 init_touch_input_power_path(samsung_pwr);
Christopher N. Hesse775bce82017-07-08 13:46:35 +0200382
383 ALOGI("Initialized settings:");
Christopher N. Hesse1146f1a2017-07-13 21:14:57 +0200384 char max_freqs[PATH_MAX];
385 sprintf(max_freqs, "max_freqs: cluster[0]: %s", samsung_pwr->max_freqs[0]);
386 for (unsigned int i = 1; i < CLUSTER_COUNT; i++) {
387 sprintf(max_freqs, "%s, %s[%d]: %s", max_freqs, "cluster", i, samsung_pwr->max_freqs[i]);
388 }
389 ALOGI("%s", max_freqs);
390 char hispeed_freqs[PATH_MAX];
391 sprintf(hispeed_freqs, "hispeed_freqs: cluster[0]: %s", samsung_pwr->hispeed_freqs[0]);
392 for (unsigned int i = 1; i < CLUSTER_COUNT; i++) {
393 sprintf(hispeed_freqs, "%s, %s[%d]: %s", hispeed_freqs, "cluster", i,
394 samsung_pwr->hispeed_freqs[i]);
395 }
396 ALOGI("%s", hispeed_freqs);
Christopher N. Hesseff868462017-07-09 23:07:09 +0200397 ALOGI("boostpulse_fd: %d", samsung_pwr->boostpulse_fd);
Christopher N. Hesse775bce82017-07-08 13:46:35 +0200398 ALOGI("touchscreen_power_path: %s",
399 samsung_pwr->touchscreen_power_path ? samsung_pwr->touchscreen_power_path : "NULL");
400 ALOGI("touchkey_power_path: %s",
401 samsung_pwr->touchkey_power_path ? samsung_pwr->touchkey_power_path : "NULL");
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100402}
403
Christopher N. Hessef6296722017-01-16 22:47:11 +0100404/**********************************************************
405 *** API FUNCTIONS
406 ***
407 *** Refer to power.h for documentation.
408 **********************************************************/
409
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100410static void samsung_power_set_interactive(struct power_module *module, int on)
411{
412 struct samsung_power_module *samsung_pwr = (struct samsung_power_module *) module;
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100413 int panel_brightness;
414 char button_state[2];
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100415 int rc;
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100416 static bool touchkeys_blocked = false;
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200417 char ON[CLUSTER_COUNT][PARAM_MAXLEN] = {"1", "1"};
418 char OFF[CLUSTER_COUNT][PARAM_MAXLEN] = {"0", "0"};
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100419
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100420 ALOGV("power_set_interactive: %d", on);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100421
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100422 /*
423 * Do not disable any input devices if the screen is on but we are in a non-interactive
424 * state.
425 */
Christopher N. Hesse3360b092016-07-11 15:48:35 +0200426 if (!on) {
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100427 panel_brightness = get_cur_panel_brightness();
428 if (panel_brightness < 0) {
429 ALOGE("%s: Failed to read panel brightness", __func__);
430 } else if (panel_brightness > 0) {
Christopher N. Hesse3360b092016-07-11 15:48:35 +0200431 ALOGV("%s: Moving to non-interactive state, but screen is still on,"
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100432 " not disabling input devices", __func__);
Christopher N. Hesse3360b092016-07-11 15:48:35 +0200433 goto out;
434 }
435 }
436
Paul Keith5e4fbe02017-04-21 21:34:17 -0500437 /* Sanity check the touchscreen path */
438 if (samsung_pwr->touchscreen_power_path) {
439 sysfs_write(samsung_pwr->touchscreen_power_path, on ? "1" : "0");
440 }
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100441
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100442 /* Bail out if the device does not have touchkeys */
443 if (samsung_pwr->touchkey_power_path == NULL) {
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100444 goto out;
445 }
446
447 if (!on) {
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100448 rc = sysfs_read(samsung_pwr->touchkey_power_path, button_state, ARRAY_SIZE(button_state));
449 if (rc < 0) {
450 ALOGE("%s: Failed to read touchkey state", __func__);
451 goto out;
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100452 }
Christopher N. Hessed9106e92017-03-04 01:05:14 +0100453 /*
454 * If button_state is 0, the keys have been disabled by another component
455 * (for example cmhw), which means we don't want them to be enabled when resuming
456 * from suspend.
457 */
458 if (button_state[0] == '0') {
459 touchkeys_blocked = true;
460 } else {
461 touchkeys_blocked = false;
462 }
463 }
464
465 if (!touchkeys_blocked) {
466 sysfs_write(samsung_pwr->touchkey_power_path, on ? "1" : "0");
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100467 }
468
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100469out:
Christopher N. Hesse7ad73bc2017-06-28 22:53:03 +0200470 cpu_interactive_write(IO_IS_BUSY_PATH, on ? ON : OFF);
Christopher N. Hesse63b5bd72017-05-29 20:45:14 +0200471
Christopher N. Hesse354f7132017-03-04 01:06:22 +0100472 ALOGV("power_set_interactive: %d done", on);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100473}
474
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100475static void samsung_power_hint(struct power_module *module,
476 power_hint_t hint,
477 void *data)
478{
479 struct samsung_power_module *samsung_pwr = (struct samsung_power_module *) module;
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100480 int len;
481
Christopher N. Hesse15b63092017-03-12 17:04:53 +0100482 /* Bail out if low-power mode is active */
Christopher N. Hesse5eca5aa2017-04-26 21:42:10 +0200483 if (current_power_profile == PROFILE_POWER_SAVE && hint != POWER_HINT_LOW_POWER
484 && hint != POWER_HINT_SET_PROFILE) {
Christopher N. Hesse15b63092017-03-12 17:04:53 +0100485 ALOGW("%s: PROFILE_POWER_SAVE active, ignoring hint %d", __func__, hint);
486 return;
487 }
488
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100489 switch (hint) {
Christopher N. Hesse2981f792017-03-12 17:08:47 +0100490 case POWER_HINT_VSYNC:
491 ALOGV("%s: POWER_HINT_VSYNC", __func__);
492 break;
493 case POWER_HINT_INTERACTION:
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100494 ALOGV("%s: POWER_HINT_INTERACTION", __func__);
Christopher N. Hessef21fbdb2017-03-04 01:41:58 +0100495 send_boostpulse(samsung_pwr->boostpulse_fd);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100496 break;
Christopher N. Hesse2981f792017-03-12 17:08:47 +0100497 case POWER_HINT_LOW_POWER:
498 ALOGV("%s: POWER_HINT_LOW_POWER", __func__);
Christopher N. Hesse3fa2b692017-04-05 18:52:48 +0200499 set_power_profile(samsung_pwr, data ? PROFILE_POWER_SAVE : PROFILE_BALANCED);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100500 break;
Christopher N. Hesse2981f792017-03-12 17:08:47 +0100501 case POWER_HINT_LAUNCH:
Christopher N. Hesse3fa2b692017-04-05 18:52:48 +0200502 ALOGV("%s: POWER_HINT_LAUNCH", __func__);
503 send_boostpulse(samsung_pwr->boostpulse_fd);
504 break;
Christopher N. Hesse5fada9b2017-01-16 23:39:48 +0100505 case POWER_HINT_CPU_BOOST:
Christopher N. Hesse3fa2b692017-04-05 18:52:48 +0200506 ALOGV("%s: POWER_HINT_CPU_BOOST", __func__);
Christopher N. Hessefb5669e2017-07-08 12:44:39 +0200507 int32_t duration_us = *((int32_t *)data);
508 send_boost(samsung_pwr->boost_fd, duration_us);
Christopher N. Hesse5fada9b2017-01-16 23:39:48 +0100509 break;
Christopher N. Hesse2981f792017-03-12 17:08:47 +0100510 case POWER_HINT_SET_PROFILE:
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100511 ALOGV("%s: POWER_HINT_SET_PROFILE", __func__);
Christopher N. Hesse2981f792017-03-12 17:08:47 +0100512 int profile = *((intptr_t *)data);
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100513 set_power_profile(samsung_pwr, profile);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100514 break;
Christopher N. Hessef9b8e832017-01-17 00:04:45 +0100515 case POWER_HINT_DISABLE_TOUCH:
516 ALOGV("%s: POWER_HINT_DISABLE_TOUCH", __func__);
Christopher N. Hesse3fa2b692017-04-05 18:52:48 +0200517 sysfs_write(samsung_pwr->touchscreen_power_path, data ? "0" : "1");
Christopher N. Hessef9b8e832017-01-17 00:04:45 +0100518 break;
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100519 default:
Christopher N. Hesse2981f792017-03-12 17:08:47 +0100520 ALOGW("%s: Unknown power hint: %d", __func__, hint);
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100521 break;
522 }
523}
524
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100525static int samsung_get_feature(struct power_module *module __unused,
526 feature_t feature)
527{
528 if (feature == POWER_FEATURE_SUPPORTED_PROFILES) {
Christopher N. Hesse58f2ca02017-01-17 00:01:15 +0100529 return PROFILE_MAX;
Andreas Schneiderf15d7f42016-02-03 10:43:47 +0100530 }
531
532 return -1;
533}
534
Christopher N. Hesse1c474662016-11-18 18:59:06 +0100535static void samsung_set_feature(struct power_module *module, feature_t feature, int state __unused)
Christopher N. Hessee480d892016-06-22 23:04:39 +0200536{
537 struct samsung_power_module *samsung_pwr = (struct samsung_power_module *) module;
538
539 switch (feature) {
ishantvivek987dcca2016-11-21 06:05:47 +0000540#ifdef TARGET_TAP_TO_WAKE_NODE
Christopher N. Hessee480d892016-06-22 23:04:39 +0200541 case POWER_FEATURE_DOUBLE_TAP_TO_WAKE:
542 ALOGV("%s: %s double tap to wake", __func__, state ? "enabling" : "disabling");
ishantvivek987dcca2016-11-21 06:05:47 +0000543 sysfs_write(TARGET_TAP_TO_WAKE_NODE, state > 0 ? "1" : "0");
Christopher N. Hessee480d892016-06-22 23:04:39 +0200544 break;
545#endif
546 default:
547 break;
548 }
549}
550
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100551static struct hw_module_methods_t power_module_methods = {
552 .open = NULL,
553};
554
555struct samsung_power_module HAL_MODULE_INFO_SYM = {
556 .base = {
557 .common = {
558 .tag = HARDWARE_MODULE_TAG,
559 .module_api_version = POWER_MODULE_API_VERSION_0_2,
560 .hal_api_version = HARDWARE_HAL_API_VERSION,
561 .id = POWER_HARDWARE_MODULE_ID,
562 .name = "Samsung Power HAL",
Christopher N. Hesseaa75be42017-01-16 22:47:59 +0100563 .author = "The LineageOS Project",
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100564 .methods = &power_module_methods,
565 },
566
567 .init = samsung_power_init,
568 .setInteractive = samsung_power_set_interactive,
569 .powerHint = samsung_power_hint,
Christopher N. Hessee480d892016-06-22 23:04:39 +0200570 .getFeature = samsung_get_feature,
571 .setFeature = samsung_set_feature
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100572 },
573
574 .lock = PTHREAD_MUTEX_INITIALIZER,
575 .boostpulse_fd = -1,
Christopher N. Hessede5e3c62015-12-21 21:28:23 +0100576};