blob: 0b939b871d75b215f43b160473df9403ee8243a0 [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>
5 * Copyright (C) 2014-2015 Christopher N. Hesse <raymanfx@gmail.com>
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#include <dirent.h>
21#include <errno.h>
22#include <fcntl.h>
23#include <malloc.h>
24#include <stdbool.h>
25#include <string.h>
26#include <unistd.h>
27
28#include <sys/types.h>
29#include <sys/stat.h>
30
31#define LOG_TAG "SamsungPowerHAL"
32/* #define LOG_NDEBUG 0 */
33#include <utils/Log.h>
34
35#include <hardware/hardware.h>
36#include <hardware/power.h>
37
38#define BOOSTPULSE_PATH "/sys/devices/system/cpu/cpu0/cpufreq/interactive/boostpulse"
39
40#define IO_IS_BUSY_PATH "/sys/devices/system/cpu/cpu0/cpufreq/interactive/io_is_busy"
41
42#define CPU0_HISPEED_FREQ_PATH "/sys/devices/system/cpu/cpu0/cpufreq/interactive/hispeed_freq"
43#define CPU0_MAX_FREQ_PATH "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq"
44#define CPU4_HISPEED_FREQ_PATH "/sys/devices/system/cpu/cpu4/cpufreq/interactive/hispeed_freq"
45#define CPU4_MAX_FREQ_PATH "/sys/devices/system/cpu/cpu4/cpufreq/scaling_max_freq"
46
47struct samsung_power_module {
48 struct power_module base;
49 pthread_mutex_t lock;
50 int boostpulse_fd;
51 int boostpulse_warned;
52 char cpu0_hispeed_freq[10];
53 char cpu0_max_freq[10];
54 char cpu4_hispeed_freq[10];
55 char cpu4_max_freq[10];
56 char* touchscreen_power_path;
57 char* touchkey_power_path;
58 bool touchkey_blocked;
59};
60
61/* POWER_HINT_LOW_POWER */
62static bool low_power_mode = false;
63
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
143static void find_input_nodes(struct samsung_power_module *samsung_pwr, char *dir)
144{
145 const char filename[] = "name";
146 char errno_str[64];
147 struct dirent *de;
148 char file_content[20];
149 char *path = NULL;
150 char *node_path = NULL;
151 size_t pathsize;
152 size_t node_pathsize;
153 DIR *d;
154
155 d = opendir(dir);
156 if (d == NULL) {
157 return;
158 }
159
160 while ((de = readdir(d)) != NULL) {
161 if (strncmp(filename, de->d_name, sizeof(filename)) == 0) {
162 pathsize = strlen(dir) + strlen(de->d_name) + 2;
163 node_pathsize = strlen(dir) + strlen("enabled") + 2;
164
165 path = malloc(pathsize);
166 node_path = malloc(node_pathsize);
167 if (path == NULL || node_path == NULL) {
168 strerror_r(errno, errno_str, sizeof(errno_str));
169 ALOGE("Out of memory: %s\n", errno_str);
170 return;
171 }
172
173 snprintf(path, pathsize, "%s/%s", dir, filename);
174 sysfs_read(path, file_content, sizeof(file_content));
175
176 snprintf(node_path, node_pathsize, "%s/%s", dir, "enabled");
177
178 if (strncmp(file_content, "sec_touchkey", 12) == 0) {
179 ALOGV("%s: found touchkey path: %s\n", __func__, node_path);
180 samsung_pwr->touchkey_power_path = malloc(node_pathsize);
181 if (samsung_pwr->touchkey_power_path == NULL) {
182 strerror_r(errno, errno_str, sizeof(errno_str));
183 ALOGE("Out of memory: %s\n", errno_str);
184 return;
185 }
186 snprintf(samsung_pwr->touchkey_power_path, node_pathsize, node_path);
187 }
188
189 if (strncmp(file_content, "sec_touchscreen", 15) == 0) {
190 ALOGV("%s: found touchscreen path: %s\n", __func__, node_path);
191 samsung_pwr->touchscreen_power_path = malloc(node_pathsize);
192 if (samsung_pwr->touchscreen_power_path == NULL) {
193 strerror_r(errno, errno_str, sizeof(errno_str));
194 ALOGE("Out of memory: %s\n", errno_str);
195 return;
196 }
197 snprintf(samsung_pwr->touchscreen_power_path, node_pathsize, node_path);
198 }
199 }
200 }
201
202 if (path)
203 free(path);
204 if (node_path)
205 free(node_path);
206 closedir(d);
207}
208
209/**********************************************************
210 *** INIT FUNCTIONS
211 **********************************************************/
212
213static void init_cpufreqs(struct samsung_power_module *samsung_pwr)
214{
215 int rc;
216 struct stat sb;
217
218 sysfs_read(CPU0_HISPEED_FREQ_PATH, samsung_pwr->cpu0_hispeed_freq,
219 sizeof(samsung_pwr->cpu0_hispeed_freq));
220 sysfs_read(CPU0_MAX_FREQ_PATH, samsung_pwr->cpu0_max_freq,
221 sizeof(samsung_pwr->cpu0_max_freq));
222 ALOGV("%s: CPU 0 hispeed freq: %s\n", __func__, samsung_pwr->cpu0_hispeed_freq);
223 ALOGV("%s: CPU 0 max freq: %s\n", __func__, samsung_pwr->cpu0_max_freq);
224
225 rc = stat(CPU4_HISPEED_FREQ_PATH, &sb);
226 if (rc == 0) {
227 sysfs_read(CPU4_HISPEED_FREQ_PATH, samsung_pwr->cpu4_hispeed_freq,
228 sizeof(samsung_pwr->cpu4_hispeed_freq));
229 sysfs_read(CPU4_MAX_FREQ_PATH, samsung_pwr->cpu4_max_freq,
230 sizeof(samsung_pwr->cpu4_max_freq));
231 ALOGV("%s: CPU 4 hispeed freq: %s\n", __func__, samsung_pwr->cpu4_hispeed_freq);
232 ALOGV("%s: CPU 4 max freq: %s\n", __func__, samsung_pwr->cpu4_max_freq);
233 }
234}
235
236static void init_touch_input_power_path(struct samsung_power_module *samsung_pwr)
237{
238 char dir[1024];
239 char errno_str[64];
240 uint32_t i;
241
242 for (i = 0; i < 20; i++) {
243 snprintf(dir, sizeof(dir), "/sys/class/input/input%d", i);
244 find_input_nodes(samsung_pwr, dir);
245 }
246}
247
248/*
249 * The init function performs power management setup actions at runtime
250 * startup, such as to set default cpufreq parameters. This is called only by
251 * the Power HAL instance loaded by PowerManagerService.
252 */
253static void samsung_power_init(struct power_module *module)
254{
255 struct samsung_power_module *samsung_pwr = (struct samsung_power_module *) module;
256
257 init_cpufreqs(samsung_pwr);
258 init_touch_input_power_path(samsung_pwr);
259}
260
261/*
262 * The setInteractive function performs power management actions upon the
263 * system entering interactive state (that is, the system is awake and ready
264 * for interaction, often with UI devices such as display and touchscreen
265 * enabled) or non-interactive state (the system appears asleep, display
266 * usually turned off). The non-interactive state is usually entered after a
267 * period of inactivity, in order to conserve battery power during such
268 * inactive periods.
269 *
270 * Typical actions are to turn on or off devices and adjust cpufreq parameters.
271 * This function may also call the appropriate interfaces to allow the kernel
272 * to suspend the system to low-power sleep state when entering non-interactive
273 * state, and to disallow low-power suspend when the system is in interactive
274 * state. When low-power suspend state is allowed, the kernel may suspend the
275 * system whenever no wakelocks are held.
276 *
277 * on is non-zero when the system is transitioning to an interactive / awake
278 * state, and zero when transitioning to a non-interactive / asleep state.
279 *
280 * This function is called to enter non-interactive state after turning off the
281 * screen (if present), and called to enter interactive state prior to turning
282 * on the screen.
283 */
284static void samsung_power_set_interactive(struct power_module *module, int on)
285{
286 struct samsung_power_module *samsung_pwr = (struct samsung_power_module *) module;
287 struct stat sb;
288 char buf[80];
289 char touchkey_node[2];
290 int touchkey_enabled;
291 int rc;
292
293 ALOGV("power_set_interactive: %d\n", on);
294
295 sysfs_write(samsung_pwr->touchscreen_power_path, on ? "1" : "0");
296
297 rc = stat(samsung_pwr->touchkey_power_path, &sb);
298 if (rc < 0) {
299 goto out;
300 }
301
302 if (!on) {
303 if (sysfs_read(samsung_pwr->touchkey_power_path, touchkey_node,
304 sizeof(touchkey_node)) == 0) {
305 touchkey_enabled = touchkey_node[0] - '0';
306 /*
307 * If touchkey_enabled is 0, the keys have been disabled by another component
308 * (for example cmhw), which means we don't want them to be enabled when resuming
309 * from suspend.
310 */
311 if (touchkey_enabled == 0) {
312 samsung_pwr->touchkey_blocked = true;
313 } else {
314 samsung_pwr->touchkey_blocked = false;
315 sysfs_write(samsung_pwr->touchkey_power_path, "0");
316 }
317 }
318 } else if (!samsung_pwr->touchkey_blocked) {
319 sysfs_write(samsung_pwr->touchkey_power_path, "1");
320 }
321
322 sysfs_write(IO_IS_BUSY_PATH, on ? "1" : "0");
323
324out:
325 ALOGV("power_set_interactive: %d done\n", on);
326}
327
328/*
329 * The powerHint function is called to pass hints on power requirements, which
330 * may result in adjustment of power/performance parameters of the cpufreq
331 * governor and other controls.
332 *
333 * The possible hints are:
334 *
335 * POWER_HINT_VSYNC
336 *
337 * Foreground app has started or stopped requesting a VSYNC pulse
338 * from SurfaceFlinger. If the app has started requesting VSYNC
339 * then CPU and GPU load is expected soon, and it may be appropriate
340 * to raise speeds of CPU, memory bus, etc. The data parameter is
341 * non-zero to indicate VSYNC pulse is now requested, or zero for
342 * VSYNC pulse no longer requested.
343 *
344 * POWER_HINT_INTERACTION
345 *
346 * User is interacting with the device, for example, touchscreen
347 * events are incoming. CPU and GPU load may be expected soon,
348 * and it may be appropriate to raise speeds of CPU, memory bus,
349 * etc. The data parameter is unused.
350 *
351 * POWER_HINT_LOW_POWER
352 *
353 * Low power mode is activated or deactivated. Low power mode
354 * is intended to save battery at the cost of performance. The data
355 * parameter is non-zero when low power mode is activated, and zero
356 * when deactivated.
357 *
358 * POWER_HINT_CPU_BOOST
359 *
360 * An operation is happening where it would be ideal for the CPU to
361 * be boosted for a specific duration. The data parameter is an
362 * integer value of the boost duration in microseconds.
363 */
364static void samsung_power_hint(struct power_module *module,
365 power_hint_t hint,
366 void *data)
367{
368 struct samsung_power_module *samsung_pwr = (struct samsung_power_module *) module;
369 char errno_str[64];
370 int len;
371
372 switch (hint) {
373 case POWER_HINT_INTERACTION: {
374
375 ALOGV("%s: POWER_HINT_INTERACTION", __func__);
376
377 if (boostpulse_open(samsung_pwr) >= 0) {
378 len = write(samsung_pwr->boostpulse_fd, "1", 1);
379
380 if (len < 0) {
381 strerror_r(errno, errno_str, sizeof(errno_str));
382 ALOGE("Error writing to %s: %s\n", BOOSTPULSE_PATH, errno_str);
383 }
384 }
385
386 break;
387 }
388 case POWER_HINT_VSYNC: {
389
390 ALOGV("%s: POWER_HINT_VSYNC", __func__);
391
392 break;
393 }
394 case POWER_HINT_LOW_POWER: {
395 int rc;
396 struct stat sb;
397
398 ALOGV("%s: POWER_HINT_LOW_POWER", __func__);
399
400 pthread_mutex_lock(&samsung_pwr->lock);
401
402 /*
403 * TODO: We fail to restore the max freqs after low power mode has been
404 * disabled for some reason (big.LITTLE specific issue?)
405 *
406 if (data) {
407 sysfs_write(CPU0_MAX_FREQ_PATH, samsung_pwr->cpu0_hispeed_freq);
408 rc = stat(CPU4_MAX_FREQ_PATH, &sb);
409 if (rc == 0) {
410 sysfs_write(CPU4_MAX_FREQ_PATH, samsung_pwr->cpu4_hispeed_freq);
411 }
412 } else {
413 sysfs_write(CPU0_MAX_FREQ_PATH, samsung_pwr->cpu0_max_freq);
414 rc = stat(CPU4_MAX_FREQ_PATH, &sb);
415 if (rc == 0) {
416 sysfs_write(CPU4_MAX_FREQ_PATH, samsung_pwr->cpu4_max_freq);
417 }
418 }
419 */
420 low_power_mode = data;
421
422 pthread_mutex_unlock(&samsung_pwr->lock);
423 break;
424 }
425 default:
426 break;
427 }
428}
429
430static struct hw_module_methods_t power_module_methods = {
431 .open = NULL,
432};
433
434struct samsung_power_module HAL_MODULE_INFO_SYM = {
435 .base = {
436 .common = {
437 .tag = HARDWARE_MODULE_TAG,
438 .module_api_version = POWER_MODULE_API_VERSION_0_2,
439 .hal_api_version = HARDWARE_HAL_API_VERSION,
440 .id = POWER_HARDWARE_MODULE_ID,
441 .name = "Samsung Power HAL",
442 .author = "The CyanogenMod Project",
443 .methods = &power_module_methods,
444 },
445
446 .init = samsung_power_init,
447 .setInteractive = samsung_power_set_interactive,
448 .powerHint = samsung_power_hint,
449 },
450
451 .lock = PTHREAD_MUTEX_INITIALIZER,
452 .boostpulse_fd = -1,
453 .boostpulse_warned = 0,
454};