1 /* 2 * Copyright (c) 2014 Intel Corporation All Rights Reserved 3 * Copyright (C) 2012 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 #include <errno.h> 19 #include <fcntl.h> 20 #include <stdlib.h> 21 #include <string.h> 22 #include <sys/types.h> 23 #include <sys/stat.h> 24 25 //#define LOG_NDEBUG 0 26 #define LOG_TAG "IntelPowerHAL" 27 #include <utils/Log.h> 28 29 #include <hardware/hardware.h> 30 #include <hardware/power.h> 31 32 #define BOOST_PULSE_SYSFS "/sys/devices/system/cpu/cpufreq/interactive/boostpulse" 33 #define BOOST_FREQ_SYSFS "/sys/devices/system/cpu/cpufreq/interactive/hispeed_freq" 34 #define BOOST_DURATION_SYSFS "/sys/devices/system/cpu/cpufreq/interactive/boostpulse_duration" 35 36 struct intel_power_module { 37 struct power_module container; 38 uint32_t pulse_duration; 39 struct timespec last_boost_time; /* latest POWER_HINT_INTERACTION boost */ 40 }; 41 42 static ssize_t sysfs_write(char *path, char *s) 43 { 44 char buf[80]; 45 ssize_t len; 46 int fd = open(path, O_WRONLY); 47 48 if (fd < 0) { 49 strerror_r(errno, buf, sizeof(buf)); 50 ALOGE("Error opening %s: %s\n", path, buf); 51 return -1; 52 } 53 54 if ((len = write(fd, s, strlen(s))) < 0) { 55 strerror_r(errno, buf, sizeof(buf)); 56 ALOGE("Error writing to %s: %s\n", path, buf); 57 } 58 59 close(fd); 60 ALOGV("wrote '%s' to %s", s, path); 61 62 return len; 63 } 64 65 static ssize_t sysfs_read(char *path, char *s, int num_bytes) 66 { 67 char buf[80]; 68 ssize_t count; 69 int fd = open(path, O_RDONLY); 70 71 if (fd < 0) { 72 strerror_r(errno, buf, sizeof(buf)); 73 ALOGE("Error reading from %s: %s\n", path, buf); 74 return -1; 75 } 76 77 if ((count = read(fd, s, (num_bytes - 1))) < 0) { 78 strerror_r(errno, buf, sizeof(buf)); 79 ALOGE("Error reading from %s: %s\n", path, buf); 80 } else { 81 if ((count >= 1) && (s[count-1] == '\n')) { 82 s[count-1] = '\0'; 83 } else { 84 s[count] = '\0'; 85 } 86 } 87 88 close(fd); 89 ALOGV("read '%s' from %s", s, path); 90 91 return count; 92 } 93 94 static void fugu_power_init(struct power_module *module) 95 { 96 struct intel_power_module *mod = (struct intel_power_module *) module; 97 char boost_freq[32]; 98 char boostpulse_duration[32]; 99 100 /* Keep default boost_freq for fugu => max freq */ 101 102 if (sysfs_read(BOOST_FREQ_SYSFS, boost_freq, 32) < 0) { 103 strcpy(boost_freq, "?"); 104 } 105 if (sysfs_read(BOOST_DURATION_SYSFS, boostpulse_duration, 32) < 0) { 106 /* above should not fail but just in case it does use an arbitrary 20ms value */ 107 snprintf(boostpulse_duration, 32, "%d", 20000); 108 } 109 mod->pulse_duration = atoi(boostpulse_duration); 110 /* initialize last_boost_time */ 111 clock_gettime(CLOCK_MONOTONIC, &mod->last_boost_time); 112 113 ALOGI("init done: will boost CPU to %skHz for %dus on input events", 114 boost_freq, mod->pulse_duration); 115 } 116 117 static void fugu_power_set_interactive(struct power_module *module, int on) 118 { 119 ALOGI("setInteractive: on=%d", on); 120 (void) module; /* unused */ 121 (void) on; /* unused */ 122 } 123 124 static inline void timespec_sub(struct timespec *res, struct timespec *a, struct timespec *b) 125 { 126 res->tv_sec = a->tv_sec - b->tv_sec; 127 if (a->tv_sec >= b->tv_sec) { 128 res->tv_nsec = a->tv_nsec - b->tv_nsec; 129 } else { 130 res->tv_nsec = 1000000000 - b->tv_nsec + a->tv_nsec; 131 res->tv_sec--; 132 } 133 } 134 135 static inline uint64_t timespec_to_us(struct timespec *t) 136 { 137 return t->tv_sec * 1000000 + t->tv_nsec / 1000; 138 } 139 140 static void fugu_power_hint(struct power_module *module, power_hint_t hint, void *data) 141 { 142 struct intel_power_module *mod = (struct intel_power_module *) module; 143 struct timespec curr_time; 144 struct timespec diff_time; 145 uint64_t diff; 146 147 (void) data; 148 149 switch (hint) { 150 case POWER_HINT_INTERACTION: 151 clock_gettime(CLOCK_MONOTONIC, &curr_time); 152 timespec_sub(&diff_time, &curr_time, &mod->last_boost_time); 153 diff = timespec_to_us(&diff_time); 154 155 ALOGV("POWER_HINT_INTERACTION: diff=%llu", diff); 156 157 if (diff > mod->pulse_duration) { 158 sysfs_write(BOOST_PULSE_SYSFS, "1"); 159 mod->last_boost_time = curr_time; 160 } 161 break; 162 case POWER_HINT_VSYNC: 163 break; 164 default: 165 break; 166 } 167 } 168 169 static struct hw_module_methods_t power_module_methods = { 170 .open = NULL, 171 }; 172 173 struct intel_power_module HAL_MODULE_INFO_SYM = { 174 container:{ 175 common: { 176 tag: HARDWARE_MODULE_TAG, 177 module_api_version: POWER_MODULE_API_VERSION_0_2, 178 hal_api_version: HARDWARE_HAL_API_VERSION, 179 id: POWER_HARDWARE_MODULE_ID, 180 name: "Fugu Power HAL", 181 author: "Intel", 182 methods: &power_module_methods, 183 }, 184 init: fugu_power_init, 185 setInteractive: fugu_power_set_interactive, 186 powerHint: fugu_power_hint, 187 }, 188 }; 189