blob: 9b28e7380e481738ac9b7e85d44a5b99285343fd [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
John Stultz127e1cc2016-08-18 12:20:40 -070037#define INTERACTIVE_BOOSTPULSE_PATH "/sys/devices/system/cpu/cpufreq/interactive/boostpulse"
38#define INTERACTIVE_IO_IS_BUSY_PATH "/sys/devices/system/cpu/cpufreq/interactive/io_is_busy"
John Stultz7266c5f2016-06-09 14:18:57 -070039#define CPU_MAX_FREQ_PATH "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq"
John Stultz7266c5f2016-06-09 14:18:57 -070040#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
John Stultze150ab32016-08-18 14:04:34 -070058
59#define container_of(addr, struct_name, field_name) \
60 ((struct_name *)((char *)(addr) - offsetof(struct_name, field_name)))
61
62
John Stultz7266c5f2016-06-09 14:18:57 -070063static void sysfs_write(const char *path, char *s)
64{
65 char buf[80];
66 int len;
67 int fd = open(path, O_WRONLY);
68
69 if (fd < 0) {
70 strerror_r(errno, buf, sizeof(buf));
71 ALOGE("Error opening %s: %s\n", path, buf);
72 return;
73 }
74
75 len = write(fd, s, strlen(s));
76 if (len < 0) {
77 strerror_r(errno, buf, sizeof(buf));
78 ALOGE("Error writing to %s: %s\n", path, buf);
79 }
80
81 close(fd);
82}
83
John Stultzc5793712016-08-18 14:00:49 -070084/*[interactive cpufreq gov funcs]*********************************************/
John Stultze150ab32016-08-18 14:04:34 -070085static void interactive_power_init(struct hikey_power_module __unused *hikey)
John Stultz7266c5f2016-06-09 14:18:57 -070086{
John Stultz65de27c2016-08-18 11:50:42 -070087 int32_t is_svelte = property_get_int32(SVELTE_PROP, 0);
88
John Stultz7266c5f2016-06-09 14:18:57 -070089 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/timer_rate",
90 "20000");
91 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/timer_slack",
92 "20000");
93 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/min_sample_time",
94 "80000");
95 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/hispeed_freq",
96 "1200000");
97 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/go_hispeed_load",
98 "99");
99 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/target_loads",
100 "65 729000:75 960000:85");
101 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/above_hispeed_delay",
102 "20000");
103 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/boostpulse_duration",
104 "1000000");
105 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/io_is_busy", "0");
106
John Stultz65de27c2016-08-18 11:50:42 -0700107 if (is_svelte) {
108 char prop_buffer[PROPERTY_VALUE_MAX];
John Stultz127e1cc2016-08-18 12:20:40 -0700109 int len = property_get(SVELTE_MAX_FREQ_PROP, prop_buffer,
110 LOW_POWER_MAX_FREQ);
John Stultz65de27c2016-08-18 11:50:42 -0700111
112 max_cpu_freq = strndup(prop_buffer, len);
John Stultz127e1cc2016-08-18 12:20:40 -0700113 len = property_get(SVELTE_LOW_POWER_MAX_FREQ_PROP, prop_buffer,
114 LOW_POWER_MAX_FREQ);
John Stultz65de27c2016-08-18 11:50:42 -0700115 low_power_max_cpu_freq = strndup(prop_buffer, len);
116 }
John Stultz7266c5f2016-06-09 14:18:57 -0700117}
118
119static void power_set_interactive(struct power_module __unused *module, int on)
120{
121 ALOGV("power_set_interactive: %d\n", on);
122
123 /*
124 * Lower maximum frequency when screen is off.
125 */
126 sysfs_write(CPU_MAX_FREQ_PATH,
127 (!on || low_power_mode) ? low_power_max_cpu_freq : max_cpu_freq);
John Stultz127e1cc2016-08-18 12:20:40 -0700128 sysfs_write(INTERACTIVE_IO_IS_BUSY_PATH, on ? "1" : "0");
John Stultz7266c5f2016-06-09 14:18:57 -0700129 ALOGV("power_set_interactive: %d done\n", on);
130}
131
John Stultz5e1546b2016-08-18 11:56:31 -0700132static int interactive_boostpulse(struct hikey_power_module *hikey)
133{
134 char buf[80];
135 int len;
136
John Stultz64b54ca2016-08-18 15:01:33 -0700137 if (hikey->boostpulse_fd < 0)
John Stultz127e1cc2016-08-18 12:20:40 -0700138 hikey->boostpulse_fd = open(INTERACTIVE_BOOSTPULSE_PATH, O_WRONLY);
John Stultz5e1546b2016-08-18 11:56:31 -0700139
John Stultz64b54ca2016-08-18 15:01:33 -0700140 if (hikey->boostpulse_fd < 0) {
141 if (!hikey->boostpulse_warned) {
John Stultz5e1546b2016-08-18 11:56:31 -0700142 strerror_r(errno, buf, sizeof(buf));
John Stultz127e1cc2016-08-18 12:20:40 -0700143 ALOGE("Error opening %s: %s\n", INTERACTIVE_BOOSTPULSE_PATH,
John Stultz64b54ca2016-08-18 15:01:33 -0700144 buf);
145 hikey->boostpulse_warned = 1;
John Stultz5e1546b2016-08-18 11:56:31 -0700146 }
John Stultz64b54ca2016-08-18 15:01:33 -0700147 return hikey->boostpulse_fd;
148 }
149
150 len = write(hikey->boostpulse_fd, "1", 1);
151 if (len < 0) {
152 strerror_r(errno, buf, sizeof(buf));
153 ALOGE("Error writing to %s: %s\n",
John Stultz127e1cc2016-08-18 12:20:40 -0700154 INTERACTIVE_BOOSTPULSE_PATH, buf);
John Stultz64b54ca2016-08-18 15:01:33 -0700155 return -1;
John Stultz5e1546b2016-08-18 11:56:31 -0700156 }
157 return 0;
158}
159
John Stultzc5793712016-08-18 14:00:49 -0700160/*[generic functions]*********************************************************/
161
162static void hikey_power_init(struct power_module __unused *module)
163{
John Stultze150ab32016-08-18 14:04:34 -0700164 struct hikey_power_module *hikey = container_of(module,
165 struct hikey_power_module, base);
166 interactive_power_init(hikey);
John Stultzc5793712016-08-18 14:00:49 -0700167}
168
John Stultz7266c5f2016-06-09 14:18:57 -0700169static void hikey_power_hint(struct power_module *module, power_hint_t hint,
170 void *data)
171{
John Stultze150ab32016-08-18 14:04:34 -0700172 struct hikey_power_module *hikey = container_of(module,
173 struct hikey_power_module, base);
John Stultz7266c5f2016-06-09 14:18:57 -0700174
175 pthread_mutex_lock(&hikey->lock);
176 switch (hint) {
177 case POWER_HINT_INTERACTION:
John Stultz5e1546b2016-08-18 11:56:31 -0700178 interactive_boostpulse(hikey);
John Stultz7266c5f2016-06-09 14:18:57 -0700179 break;
180
181 case POWER_HINT_VSYNC:
182 break;
183
184 case POWER_HINT_LOW_POWER:
185 if (data) {
186 sysfs_write(CPU_MAX_FREQ_PATH, low_power_max_cpu_freq);
187 } else {
188 sysfs_write(CPU_MAX_FREQ_PATH, max_cpu_freq);
189 }
190 low_power_mode = data;
191 break;
192
193 default:
194 break;
195 }
196 pthread_mutex_unlock(&hikey->lock);
197}
198
John Stultzab1f6a72016-08-18 12:28:55 -0700199static void set_feature(struct power_module *module, feature_t feature, int state)
200{
John Stultze150ab32016-08-18 14:04:34 -0700201 struct hikey_power_module *hikey = container_of(module,
202 struct hikey_power_module, base);
John Stultzab1f6a72016-08-18 12:28:55 -0700203 switch (feature) {
204 default:
205 ALOGW("Error setting the feature, it doesn't exist %d\n", feature);
206 break;
207 }
208}
209
John Stultz7266c5f2016-06-09 14:18:57 -0700210static struct hw_module_methods_t power_module_methods = {
211 .open = NULL,
212};
213
214struct hikey_power_module HAL_MODULE_INFO_SYM = {
215 base: {
216 common: {
217 tag: HARDWARE_MODULE_TAG,
218 module_api_version: POWER_MODULE_API_VERSION_0_2,
219 hal_api_version: HARDWARE_HAL_API_VERSION,
220 id: POWER_HARDWARE_MODULE_ID,
221 name: "HiKey Power HAL",
222 author: "The Android Open Source Project",
223 methods: &power_module_methods,
224 },
225
John Stultzc5793712016-08-18 14:00:49 -0700226 init: hikey_power_init,
John Stultz7266c5f2016-06-09 14:18:57 -0700227 setInteractive: power_set_interactive,
228 powerHint: hikey_power_hint,
229 setFeature: set_feature,
230 },
231
232 lock: PTHREAD_MUTEX_INITIALIZER,
233 boostpulse_fd: -1,
234 boostpulse_warned: 0,
235};
236