Home | History | Annotate | Download | only in health
      1 /*
      2  * Copyright (C) 2016 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 
     17 #define LOG_TAG "healthd-dragon"
     18 #include <healthd/healthd.h>
     19 #include <sys/stat.h>
     20 #include <fcntl.h>
     21 #include <cutils/klog.h>
     22 #include <sys/types.h>
     23 
     24 #define PSU_SYSFS_PATH "/sys/class/power_supply/bq27742-0"
     25 #define PSU_SYSFS_MAX_CURRENT_PATH PSU_SYSFS_PATH "/current_max"
     26 #define BATTERY_CRITICAL_LOW_CAP 10
     27 #define BATTERY_CRITICAL_LOW_IMAX_MA 5000
     28 #define BATTERY_MAX_IMAX_MA          9000
     29 
     30 using namespace android;
     31 
     32 static int read_sysfs(const char *path, char *buf, size_t size) {
     33     char *cp = NULL;
     34 
     35     int fd = open(path, O_RDONLY);
     36     if (fd == -1) {
     37         KLOG_ERROR(LOG_TAG, "Could not open '%s'\n", path);
     38         return -1;
     39     }
     40 
     41     ssize_t count = TEMP_FAILURE_RETRY(read(fd, buf, size));
     42     if (count > 0)
     43         cp = (char *)memrchr(buf, '\n', count);
     44 
     45     if (cp)
     46         *cp = '\0';
     47     else
     48         buf[0] = '\0';
     49 
     50     close(fd);
     51     return count;
     52 }
     53 
     54 static int read_current_max_ma() {
     55     const int SIZE = 10;
     56     char buf[SIZE];
     57     if (read_sysfs(PSU_SYSFS_MAX_CURRENT_PATH, buf, SIZE) > 0)
     58         return atoi(buf) / 1000;
     59 
     60     return 0;
     61 }
     62 
     63 static void dragon_soc_adjust(struct BatteryProperties *props)
     64 {
     65     int soc;
     66     int current_max_ma;
     67 
     68     soc = props->batteryLevel;
     69     /*
     70      * if not charging and State-Of-Charge (soc) is below
     71      * BATTERY_CRITICAL_LOW_CAP shrink soc based on imax value
     72      */
     73     if ((soc < BATTERY_CRITICAL_LOW_CAP) &&
     74         ((props->batteryStatus == BATTERY_STATUS_DISCHARGING) ||
     75          (props->batteryStatus == BATTERY_STATUS_NOT_CHARGING) ||
     76          (props->batteryStatus == BATTERY_STATUS_UNKNOWN))) {
     77 
     78         current_max_ma = read_current_max_ma();
     79         if (current_max_ma == 0)
     80             /*
     81              * Either it failed to read sysfs or its really zero.  In either
     82              * case lets just warn so logs will identify for further debug.
     83              */
     84             KLOG_WARNING(LOG_TAG, "imax=0\n");
     85         else if (current_max_ma < BATTERY_CRITICAL_LOW_IMAX_MA)
     86             /* force shutdown */
     87             soc = 0;
     88         else if (current_max_ma < BATTERY_MAX_IMAX_MA)
     89             /* decrease soc towards zero */
     90             soc = soc * current_max_ma / BATTERY_MAX_IMAX_MA;
     91 
     92         KLOG_INFO(LOG_TAG, "imax=%d soc=%d\n", current_max_ma, soc);
     93     }
     94     props->batteryLevel = soc;
     95 }
     96 
     97 int healthd_board_battery_update(struct BatteryProperties *props)
     98 {
     99 
    100     dragon_soc_adjust(props);
    101 
    102     // return 0 to log periodic polled battery status to kernel log
    103     return 0;
    104 }
    105 
    106 void healthd_board_init(struct healthd_config *config) {}
    107