blob: ea63916c005d6409a47d6ed96e1d046a49afcb6a [file] [log] [blame]
John Stultz7266c5f2016-06-09 14:18:57 -07001/*
2 * Copyright (C) 2013 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 * Based on the FlounderPowerHAL
17 */
18
19#include <dirent.h>
20#include <errno.h>
21#include <string.h>
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <fcntl.h>
25#include <unistd.h>
26#include <stdlib.h>
27#include <stdbool.h>
28#include <cutils/properties.h>
29//#define LOG_NDEBUG 0
30
31#define LOG_TAG "HiKeyPowerHAL"
32#include <utils/Log.h>
33
34#include <hardware/hardware.h>
35#include <hardware/power.h>
36
37#define BOOSTPULSE_PATH "/sys/devices/system/cpu/cpufreq/interactive/boostpulse"
38#define CPU_MAX_FREQ_PATH "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq"
39#define IO_IS_BUSY_PATH "/sys/devices/system/cpu/cpufreq/interactive/io_is_busy"
40#define LOW_POWER_MAX_FREQ "729000"
41#define NORMAL_MAX_FREQ "1200000"
42#define SVELTE_PROP "ro.boot.svelte"
43#define SVELTE_MAX_FREQ_PROP "ro.config.svelte.max_cpu_freq"
44#define SVELTE_LOW_POWER_MAX_FREQ_PROP "ro.config.svelte.low_power_max_cpu_freq"
45
46struct hikey_power_module {
47 struct power_module base;
48 pthread_mutex_t lock;
49 int boostpulse_fd;
50 int boostpulse_warned;
51};
52
53static bool low_power_mode = false;
54
55static char *max_cpu_freq = NORMAL_MAX_FREQ;
56static char *low_power_max_cpu_freq = LOW_POWER_MAX_FREQ;
57
58static void sysfs_write(const char *path, char *s)
59{
60 char buf[80];
61 int len;
62 int fd = open(path, O_WRONLY);
63
64 if (fd < 0) {
65 strerror_r(errno, buf, sizeof(buf));
66 ALOGE("Error opening %s: %s\n", path, buf);
67 return;
68 }
69
70 len = write(fd, s, strlen(s));
71 if (len < 0) {
72 strerror_r(errno, buf, sizeof(buf));
73 ALOGE("Error writing to %s: %s\n", path, buf);
74 }
75
76 close(fd);
77}
78
79static void calculate_max_cpu_freq() {
80 int32_t is_svelte = property_get_int32(SVELTE_PROP, 0);
81
82 if (is_svelte) {
83 char prop_buffer[PROPERTY_VALUE_MAX];
84 int len = property_get(SVELTE_MAX_FREQ_PROP, prop_buffer, LOW_POWER_MAX_FREQ);
85 max_cpu_freq = strndup(prop_buffer, len);
86 len = property_get(SVELTE_LOW_POWER_MAX_FREQ_PROP, prop_buffer, LOW_POWER_MAX_FREQ);
87 low_power_max_cpu_freq = strndup(prop_buffer, len);
88 }
89}
90
91static void power_init(struct power_module __unused *module)
92{
93 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/timer_rate",
94 "20000");
95 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/timer_slack",
96 "20000");
97 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/min_sample_time",
98 "80000");
99 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/hispeed_freq",
100 "1200000");
101 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/go_hispeed_load",
102 "99");
103 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/target_loads",
104 "65 729000:75 960000:85");
105 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/above_hispeed_delay",
106 "20000");
107 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/boostpulse_duration",
108 "1000000");
109 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/io_is_busy", "0");
110
111 calculate_max_cpu_freq();
112}
113
114static void power_set_interactive(struct power_module __unused *module, int on)
115{
116 ALOGV("power_set_interactive: %d\n", on);
117
118 /*
119 * Lower maximum frequency when screen is off.
120 */
121 sysfs_write(CPU_MAX_FREQ_PATH,
122 (!on || low_power_mode) ? low_power_max_cpu_freq : max_cpu_freq);
123 sysfs_write(IO_IS_BUSY_PATH, on ? "1" : "0");
124 ALOGV("power_set_interactive: %d done\n", on);
125}
126
127static int boostpulse_open(struct hikey_power_module *hikey)
128{
129 char buf[80];
130 int len;
131
132 if (hikey->boostpulse_fd < 0) {
133 hikey->boostpulse_fd = open(BOOSTPULSE_PATH, O_WRONLY);
134
135 if (hikey->boostpulse_fd < 0) {
136 if (!hikey->boostpulse_warned) {
137 strerror_r(errno, buf, sizeof(buf));
138 ALOGE("Error opening %s: %s\n", BOOSTPULSE_PATH, buf);
139 hikey->boostpulse_warned = 1;
140 }
141 }
142 }
143 return hikey->boostpulse_fd;
144}
145
146static void set_feature(struct power_module *module, feature_t feature, int state)
147{
148 struct hikey_power_module *hikey =
149 (struct hikey_power_module *) module;
150 switch (feature) {
151 default:
152 ALOGW("Error setting the feature, it doesn't exist %d\n", feature);
153 break;
154 }
155}
156
157static void hikey_power_hint(struct power_module *module, power_hint_t hint,
158 void *data)
159{
160 struct hikey_power_module *hikey =
161 (struct hikey_power_module *) module;
162 char buf[80];
163 int len;
164
165 pthread_mutex_lock(&hikey->lock);
166 switch (hint) {
167 case POWER_HINT_INTERACTION:
168 if (boostpulse_open(hikey) >= 0) {
169 len = write(hikey->boostpulse_fd, "1", 1);
170
171 if (len < 0) {
172 strerror_r(errno, buf, sizeof(buf));
173 ALOGE("Error writing to %s: %s\n", BOOSTPULSE_PATH, buf);
174 }
175 }
176
177 break;
178
179 case POWER_HINT_VSYNC:
180 break;
181
182 case POWER_HINT_LOW_POWER:
183 if (data) {
184 sysfs_write(CPU_MAX_FREQ_PATH, low_power_max_cpu_freq);
185 } else {
186 sysfs_write(CPU_MAX_FREQ_PATH, max_cpu_freq);
187 }
188 low_power_mode = data;
189 break;
190
191 default:
192 break;
193 }
194 pthread_mutex_unlock(&hikey->lock);
195}
196
197static struct hw_module_methods_t power_module_methods = {
198 .open = NULL,
199};
200
201struct hikey_power_module HAL_MODULE_INFO_SYM = {
202 base: {
203 common: {
204 tag: HARDWARE_MODULE_TAG,
205 module_api_version: POWER_MODULE_API_VERSION_0_2,
206 hal_api_version: HARDWARE_HAL_API_VERSION,
207 id: POWER_HARDWARE_MODULE_ID,
208 name: "HiKey Power HAL",
209 author: "The Android Open Source Project",
210 methods: &power_module_methods,
211 },
212
213 init: power_init,
214 setInteractive: power_set_interactive,
215 powerHint: hikey_power_hint,
216 setFeature: set_feature,
217 },
218
219 lock: PTHREAD_MUTEX_INITIALIZER,
220 boostpulse_fd: -1,
221 boostpulse_warned: 0,
222};
223