1 /*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include <errno.h>
17 #include <string.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21
22 #define LOG_TAG "Grouper PowerHAL"
23 #include <utils/Log.h>
24
25 #include <hardware/hardware.h>
26 #include <hardware/power.h>
27
28 #define BOOST_PATH "/sys/devices/system/cpu/cpufreq/interactive/boost"
29 static int boost_fd = -1;
30 static int boost_warned;
31
sysfs_write(char * path,char * s)32 static void sysfs_write(char *path, char *s)
33 {
34 char buf[80];
35 int len;
36 int fd = open(path, O_WRONLY);
37
38 if (fd < 0) {
39 strerror_r(errno, buf, sizeof(buf));
40 ALOGE("Error opening %s: %s\n", path, buf);
41 return;
42 }
43
44 len = write(fd, s, strlen(s));
45 if (len < 0) {
46 strerror_r(errno, buf, sizeof(buf));
47 ALOGE("Error writing to %s: %s\n", path, buf);
48 }
49
50 close(fd);
51 }
52
grouper_power_init(struct power_module * module)53 static void grouper_power_init(struct power_module *module)
54 {
55 /*
56 * cpufreq interactive governor: timer 20ms, min sample 100ms,
57 * hispeed 700MHz at load 40%
58 */
59
60 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/timer_rate",
61 "20000");
62 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/min_sample_time",
63 "30000");
64 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/go_hispeed_load",
65 "85");
66 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/boost_factor",
67 "0");
68 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/input_boost",
69 "1");
70 }
71
grouper_power_set_interactive(struct power_module * module,int on)72 static void grouper_power_set_interactive(struct power_module *module, int on)
73 {
74 /*
75 * Lower maximum frequency when screen is off. CPU 0 and 1 share a
76 * cpufreq policy.
77 */
78
79 sysfs_write("/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq",
80 on ? "1300000" : "700000");
81
82 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/input_boost",
83 on ? "1" : "0");
84
85 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/boost_factor",
86 on ? "0" : "2");
87
88 }
89
grouper_power_hint(struct power_module * module,power_hint_t hint,void * data)90 static void grouper_power_hint(struct power_module *module, power_hint_t hint,
91 void *data)
92 {
93 char buf[80];
94 int len;
95
96 switch (hint) {
97 case POWER_HINT_VSYNC:
98 break;
99
100 default:
101 break;
102 }
103 }
104
105 static struct hw_module_methods_t power_module_methods = {
106 .open = NULL,
107 };
108
109 struct power_module HAL_MODULE_INFO_SYM = {
110 .common = {
111 .tag = HARDWARE_MODULE_TAG,
112 .module_api_version = POWER_MODULE_API_VERSION_0_2,
113 .hal_api_version = HARDWARE_HAL_API_VERSION,
114 .id = POWER_HARDWARE_MODULE_ID,
115 .name = "Grouper Power HAL",
116 .author = "The Android Open Source Project",
117 .methods = &power_module_methods,
118 },
119
120 .init = grouper_power_init,
121 .setInteractive = grouper_power_set_interactive,
122 .powerHint = grouper_power_hint,
123 };
124