Home | History | Annotate | Download | only in power
      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 <sys/types.h>
     19 #include <sys/stat.h>
     20 #include <fcntl.h>
     21 #include <stdlib.h>
     22 
     23 //#define LOG_NDEBUG 0
     24 #define LOG_TAG "IntelPowerHAL"
     25 #include <utils/Log.h>
     26 
     27 #include <hardware/hardware.h>
     28 #include <hardware/power.h>
     29 
     30 #define BOOST_PULSE_SYSFS    "/sys/devices/system/cpu/cpufreq/interactive/boostpulse"
     31 #define BOOST_FREQ_SYSFS     "/sys/devices/system/cpu/cpufreq/interactive/hispeed_freq"
     32 #define BOOST_DURATION_SYSFS "/sys/devices/system/cpu/cpufreq/interactive/boostpulse_duration"
     33 
     34 struct intel_power_module {
     35     struct power_module container;
     36     uint32_t pulse_duration;
     37     struct timespec last_boost_time; /* latest POWER_HINT_INTERACTION boost */
     38 };
     39 
     40 static ssize_t sysfs_write(char *path, char *s)
     41 {
     42     char buf[80];
     43     ssize_t len;
     44     int fd = open(path, O_WRONLY);
     45 
     46     if (fd < 0) {
     47         strerror_r(errno, buf, sizeof(buf));
     48         ALOGE("Error opening %s: %s\n", path, buf);
     49         return -1;
     50     }
     51 
     52     if ((len = write(fd, s, strlen(s))) < 0) {
     53         strerror_r(errno, buf, sizeof(buf));
     54         ALOGE("Error writing to %s: %s\n", path, buf);
     55     }
     56 
     57     close(fd);
     58     ALOGV("wrote '%s' to %s", s, path);
     59 
     60     return len;
     61 }
     62 
     63 static ssize_t sysfs_read(char *path, char *s, int num_bytes)
     64 {
     65     char buf[80];
     66     ssize_t count;
     67     int fd = open(path, O_RDONLY);
     68 
     69     if (fd < 0) {
     70         strerror_r(errno, buf, sizeof(buf));
     71         ALOGE("Error reading from %s: %s\n", path, buf);
     72         return -1;
     73     }
     74 
     75     if ((count = read(fd, s, (num_bytes - 1))) < 0) {
     76         strerror_r(errno, buf, sizeof(buf));
     77         ALOGE("Error reading from  %s: %s\n", path, buf);
     78     } else {
     79         if ((count >= 1) && (s[count-1] == '\n')) {
     80             s[count-1] = '\0';
     81         } else {
     82             s[count] = '\0';
     83         }
     84     }
     85 
     86     close(fd);
     87     ALOGV("read '%s' from %s", s, path);
     88 
     89     return count;
     90 }
     91 
     92 static void fugu_power_init(struct power_module *module)
     93 {
     94     struct intel_power_module *mod = (struct intel_power_module *) module;
     95     char boost_freq[32];
     96     char boostpulse_duration[32];
     97 
     98     /* Keep default boost_freq for fugu => max freq */
     99 
    100     if (sysfs_read(BOOST_FREQ_SYSFS, boost_freq, 32) < 0) {
    101         strcpy(boost_freq, "?");
    102     }
    103     if (sysfs_read(BOOST_DURATION_SYSFS, boostpulse_duration, 32) < 0) {
    104         /* above should not fail but just in case it does use an arbitrary 20ms value */
    105         snprintf(boostpulse_duration, 32, "%d", 20000);
    106     }
    107     mod->pulse_duration = atoi(boostpulse_duration);
    108     /* initialize last_boost_time */
    109     clock_gettime(CLOCK_MONOTONIC, &mod->last_boost_time);
    110 
    111     ALOGI("init done: will boost CPU to %skHz for %dus on input events",
    112             boost_freq, mod->pulse_duration);
    113 }
    114 
    115 static void fugu_power_set_interactive(struct power_module *module, int on)
    116 {
    117     struct dirent **device_list = NULL;
    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     char sysfs_val[80];
    144     struct timespec curr_time;
    145     struct timespec diff_time;
    146     uint64_t diff;
    147 
    148     (void) data;
    149 
    150     switch (hint) {
    151         case POWER_HINT_INTERACTION:
    152             clock_gettime(CLOCK_MONOTONIC, &curr_time);
    153             timespec_sub(&diff_time, &curr_time, &mod->last_boost_time);
    154             diff = timespec_to_us(&diff_time);
    155 
    156             ALOGV("POWER_HINT_INTERACTION: diff=%llu", diff);
    157 
    158             if (diff > mod->pulse_duration) {
    159                 sysfs_write(BOOST_PULSE_SYSFS, "1");
    160                 mod->last_boost_time = curr_time;
    161             }
    162             break;
    163         case POWER_HINT_VSYNC:
    164             break;
    165         default:
    166             break;
    167     }
    168 }
    169 
    170 static struct hw_module_methods_t power_module_methods = {
    171     .open = NULL,
    172 };
    173 
    174 struct intel_power_module HAL_MODULE_INFO_SYM = {
    175     container:{
    176         common: {
    177             tag: HARDWARE_MODULE_TAG,
    178             module_api_version: POWER_MODULE_API_VERSION_0_2,
    179             hal_api_version: HARDWARE_HAL_API_VERSION,
    180             id: POWER_HARDWARE_MODULE_ID,
    181             name: "Fugu Power HAL",
    182             author: "Intel",
    183             methods: &power_module_methods,
    184         },
    185         init: fugu_power_init,
    186         setInteractive: fugu_power_set_interactive,
    187         powerHint: fugu_power_hint,
    188     },
    189 };
    190