diff options
author | Ziyan <jaraidaniel@gmail.com> | 2016-09-03 23:17:01 +0200 |
---|---|---|
committer | Andreas Blaesius <skate4life@gmx.de> | 2017-02-06 21:31:59 +0000 |
commit | 8d973f30c67070cab51ccb7387e71d6e6dddfaac (patch) | |
tree | 6a82222f6c8fda476100fad5f7dd84e4b50d1711 /libpower | |
parent | ebb9b5217814c518054dc4d34aabc2946e8079f2 (diff) | |
download | hardware_ti_omap4-8d973f30c67070cab51ccb7387e71d6e6dddfaac.zip hardware_ti_omap4-8d973f30c67070cab51ccb7387e71d6e6dddfaac.tar.gz hardware_ti_omap4-8d973f30c67070cab51ccb7387e71d6e6dddfaac.tar.bz2 |
Add OMAP PowerHAL
Based on the OMAP PowerHAL from omapzoom.org.
Change-Id: Ibc6c6779c9004dc442db436f8b68cbb191b16662
Diffstat (limited to 'libpower')
-rw-r--r-- | libpower/Android.mk | 29 | ||||
-rw-r--r-- | libpower/power.c | 240 |
2 files changed, 269 insertions, 0 deletions
diff --git a/libpower/Android.mk b/libpower/Android.mk new file mode 100644 index 0000000..c6fb0f7 --- /dev/null +++ b/libpower/Android.mk @@ -0,0 +1,29 @@ +# +# Copyright 2016 The Android Open-Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_MODULE := power.omap4 +LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw +LOCAL_SRC_FILES := power.c +LOCAL_SHARED_LIBRARIES := liblog +LOCAL_MODULE_TAGS := optional + +LOCAL_CFLAGS := -Wall -Werror + +include $(BUILD_SHARED_LIBRARY) diff --git a/libpower/power.c b/libpower/power.c new file mode 100644 index 0000000..bb481e7 --- /dev/null +++ b/libpower/power.c @@ -0,0 +1,240 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include <errno.h> +#include <string.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> + +#define LOG_TAG "TI OMAP PowerHAL" +#include <utils/Log.h> + +#include <hardware/hardware.h> +#include <hardware/power.h> + +#define CPUFREQ_INTERACTIVE "/sys/devices/system/cpu/cpufreq/interactive/" +#define CPUFREQ_CPU0 "/sys/devices/system/cpu/cpu0/cpufreq/" +#define BOOSTPULSE_PATH (CPUFREQ_INTERACTIVE "boostpulse") + +#define MAX_FREQ_NUMBER 10 +#define NOM_FREQ_INDEX 2 + +static int freq_num; +static char *freq_list[MAX_FREQ_NUMBER]; +static char *max_freq, *nom_freq; + +struct omap_power_module { + struct power_module base; + pthread_mutex_t lock; + int boostpulse_fd; + int boostpulse_warned; + int inited; +}; + +static int str_to_tokens(char *str, char **token, int max_token_idx) +{ + char *pos, *start_pos = str; + char *token_pos; + int token_idx = 0; + + if (!str || !token || !max_token_idx) + return 0; + + do { + token_pos = strtok_r(start_pos, " \t\r\n", &pos); + + if (token_pos) + token[token_idx++] = strdup(token_pos); + start_pos = NULL; + } while (token_pos && token_idx < max_token_idx); + + return token_idx; +} + +static void sysfs_write(char *path, char *s) +{ + char buf[80]; + int len; + int fd = open(path, O_WRONLY); + + if (fd < 0) { + strerror_r(errno, buf, sizeof(buf)); + ALOGE("Error opening %s: %s\n", path, buf); + return; + } + + len = write(fd, s, strlen(s)); + if (len < 0) { + strerror_r(errno, buf, sizeof(buf)); + ALOGE("Error writing to %s: %s\n", path, buf); + } + + close(fd); +} + +static int sysfs_read(char *path, char *s, int s_size) +{ + char buf[80]; + int len; + int fd; + + if (!path || !s || !s_size) + return -1; + + fd = open(path, O_RDONLY); + if (fd < 0) { + strerror_r(errno, buf, sizeof(buf)); + ALOGE("Error opening %s: %s\n", path, buf); + return fd; + } + + len = read(fd, s, s_size-1); + if (len < 0) { + strerror_r(errno, buf, sizeof(buf)); + ALOGE("Error reading from %s: %s\n", path, buf); + } else { + s[len] = '\0'; + } + + close(fd); + return len; +} + +static void omap_power_init(struct power_module *module) +{ + struct omap_power_module *omap_device = + (struct omap_power_module *) module; + int tmp; + char freq_buf[MAX_FREQ_NUMBER*10]; + + tmp = sysfs_read(CPUFREQ_CPU0 "scaling_available_frequencies", + freq_buf, sizeof(freq_buf)); + if (tmp <= 0) + return; + + freq_num = str_to_tokens(freq_buf, freq_list, MAX_FREQ_NUMBER); + if (!freq_num) + return; + + max_freq = freq_list[freq_num - 1]; + tmp = (NOM_FREQ_INDEX > freq_num) ? freq_num : NOM_FREQ_INDEX; + nom_freq = freq_list[tmp - 1]; + + sysfs_write(CPUFREQ_INTERACTIVE "timer_rate", "20000"); + sysfs_write(CPUFREQ_INTERACTIVE "min_sample_time","60000"); + sysfs_write(CPUFREQ_INTERACTIVE "hispeed_freq", nom_freq); + sysfs_write(CPUFREQ_INTERACTIVE "go_hispeed_load", "50"); + sysfs_write(CPUFREQ_INTERACTIVE "above_hispeed_delay", "100000"); + + ALOGI("Initialized successfully"); + omap_device->inited = 1; +} + +static int boostpulse_open(struct omap_power_module *omap_device) +{ + char buf[80]; + + pthread_mutex_lock(&omap_device->lock); + + if (omap_device->boostpulse_fd < 0) { + omap_device->boostpulse_fd = open(BOOSTPULSE_PATH, O_WRONLY); + + if (omap_device->boostpulse_fd < 0) { + if (!omap_device->boostpulse_warned) { + strerror_r(errno, buf, sizeof(buf)); + ALOGE("Error opening %s: %s\n", BOOSTPULSE_PATH, buf); + omap_device->boostpulse_warned = 1; + } + } + } + + pthread_mutex_unlock(&omap_device->lock); + return omap_device->boostpulse_fd; +} + +static void omap_power_set_interactive(struct power_module *module, + int on) +{ + struct omap_power_module *omap_device = + (struct omap_power_module *) module; + + if (!omap_device->inited) + return; + + /* + * Lower maximum frequency when screen is off. CPU 0 and 1 share a + * cpufreq policy. + */ + + sysfs_write(CPUFREQ_CPU0 "scaling_max_freq", on ? max_freq : nom_freq); +} + +static void omap_power_hint(struct power_module *module, + power_hint_t hint, void *data __unused) +{ + struct omap_power_module *omap_device = + (struct omap_power_module *) module; + char buf[80]; + int len; + + if (!omap_device->inited) + return; + + switch (hint) { + case POWER_HINT_INTERACTION: + if (boostpulse_open(omap_device) >= 0) { + len = write(omap_device->boostpulse_fd, "1", 1); + + if (len < 0) { + strerror_r(errno, buf, sizeof(buf)); + ALOGE("Error writing to %s: %s\n", BOOSTPULSE_PATH, buf); + } + } + break; + + case POWER_HINT_VSYNC: + break; + + default: + break; + } +} + +static struct hw_module_methods_t power_module_methods = { + .open = NULL, +}; + +struct omap_power_module HAL_MODULE_INFO_SYM = { + .base = { + .common = { + .tag = HARDWARE_MODULE_TAG, + .module_api_version = POWER_MODULE_API_VERSION_0_2, + .hal_api_version = HARDWARE_HAL_API_VERSION, + .id = POWER_HARDWARE_MODULE_ID, + .name = "OMAP Power HAL", + .author = "The Android Open Source Project", + .methods = &power_module_methods, + }, + + .init = omap_power_init, + .setInteractive = omap_power_set_interactive, + .powerHint = omap_power_hint, + }, + + .lock = PTHREAD_MUTEX_INITIALIZER, + .boostpulse_fd = -1, + .boostpulse_warned = 0, +}; |