pass: Add support of battery charging properties 48/262548/1 accepted/tizen/unified/20210816.122910 submit/tizen/20210813.100724
authorChanwoo Choi <cw00.choi@samsung.com>
Fri, 13 Aug 2021 04:23:46 +0000 (13:23 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Fri, 13 Aug 2021 06:37:21 +0000 (15:37 +0900)
In order to control the battery charging status and charging current,
add new properties as following:

[Newly added properties for battery h/w resource]
- 'battery,charging_status' property in 'level_list' section
- 'battery,charging_current_limit' property in 'level_list' section

[Example of charging properties]
In case of scripts/pass-battery.json,
"support" : true,
"init_level" : 0,
"level_list" :
[
{
"level" : 0,
"battery,charging_status" : 0, // POWER_SUPPLY_STATUS_UNKNOWN
"battery_charging_currnt_uA" : 2048000
}, {
"level" : 1,
"battery,charging_status" : 0, // POWER_SUPPLY_STATUS_UNKNOWN
"battery_charging_currnt_uA" : 1024000
}, {
"level" : 2,
"battery,charging_status" : 3, // POWER_SUPPLY_STATUS_NOT_CHARGING
"battery_charging_currnt_uA" : 1024000
}
],

Change-Id: I5a762d3127c2b156be377846a8ab19c4e582b247
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
src/pass/pass-hal.c
src/pass/pass-hal.h
src/pass/pass-parser.c
src/pass/pass-rescon.c
src/pass/pass.h

index 8a30041dd9ad393d69c2231b56e7916baa56e147..81e984eb342350cf5e33301be3d07dd415842446 100644 (file)
@@ -469,6 +469,79 @@ int pass_hal_get_cooling_device_max_state(struct pass_resource *res)
                                        res->config_data.res_cooling_name);
 }
 
+/**
+ * @brief      Set the battery charging state
+ * @param      [in] res Instance of h/w resource
+ * @param      [in] state State of cooling device
+ * @return     @c positive integer on success, otherwise error value
+ * @retval     -22 Invalid argument (-EINVAL)
+ * @retval     -1 Operation not permitted (-EPERM)
+ * @retval     -19 Operation not supported (-ENOTSUP)
+ */
+int pass_hal_set_battery_charging_status(struct pass_resource *res, int charging_status)
+{
+       if (!res)
+               return -EINVAL;
+
+       return hal_power_battery_set_charging_status(res->config_data.res_type,
+                                       res->config_data.res_name,
+                                       charging_status);
+}
+
+/**
+ * @brief      Get the battery charging state
+ * @param      [in] res Instance of h/w resource
+ * @return     @c positive integer on success, otherwise error value
+ * @retval     -22 Invalid argument (-EINVAL)
+ * @retval     -1 Operation not permitted (-EPERM)
+ * @retval     -19 Operation not supported (-ENOTSUP)
+ */
+int pass_hal_get_battery_charging_status(struct pass_resource *res)
+{
+       if (!res)
+               return -EINVAL;
+
+       return hal_power_battery_get_charging_status(res->config_data.res_type,
+                                       res->config_data.res_name);
+}
+
+/**
+ * @brief      Set the battery charging current (unit: uA)
+ * @param      [in] res Instance of h/w resource
+ * @param      [in] state State of cooling device
+ * @return     @c positive integer on success, otherwise error value
+ * @retval     -22 Invalid argument (-EINVAL)
+ * @retval     -1 Operation not permitted (-EPERM)
+ * @retval     -19 Operation not supported (-ENOTSUP)
+ */
+int pass_hal_set_battery_charging_current(struct pass_resource *res,
+                                       int charging_current_uA)
+{
+       if (!res)
+               return -EINVAL;
+
+       return hal_power_battery_set_charging_current(res->config_data.res_type,
+                                       res->config_data.res_name,
+                                       charging_current_uA);
+}
+
+/**
+ * @brief      Get the battery charging current (unit: uA)
+ * @param      [in] res Instance of h/w resource
+ * @return     @c positive integer on success, otherwise error value
+ * @retval     -22 Invalid argument (-EINVAL)
+ * @retval     -1 Operation not permitted (-EPERM)
+ * @retval     -19 Operation not supported (-ENOTSUP)
+ */
+int pass_hal_get_battery_charging_current(struct pass_resource *res)
+{
+       if (!res)
+               return -EINVAL;
+
+       return hal_power_battery_get_charging_current(res->config_data.res_type,
+                                       res->config_data.res_name);
+}
+
 /**
  * @brief      Set the fault_around_bytes for memory h/w resource
  * @param      [in] res Instance of h/w resource
@@ -624,6 +697,24 @@ static int pass_hal_save_thermal_initdata(struct pass_resource *res)
        return 0;
 }
 
+/**
+ * @brief      Restore the saved state of charging for battery h/w resource
+ *             before PASS (Power Aware System Service) daemon starting
+ * @param      [in] res Instance of h/w resource
+ * @return     @c 0 on success, otherwise error value
+ */
+static int pass_hal_save_battery_initdata(struct pass_resource *res)
+{
+       struct pass_resource_init_data *initdata = &res->init_data;
+
+       initdata->battery.charging_status
+               = pass_hal_get_battery_charging_status(res);
+       initdata->battery.charging_current_uA
+               = pass_hal_get_battery_charging_current(res);
+
+       return 0;
+}
+
 /**
  * @brief      Restore the saved state of DVFS(Dynamic Voltage and Frequency
  *             Scaling) resource when PASS (Power Aware System Service) daemon
@@ -750,6 +841,34 @@ static int pass_hal_restore_thermal_initdata(struct pass_resource *res)
        return 0;
 }
 
+/**
+ * @brief      Restore the saved state of charging for battery h/w resource
+ *             before PASS (Power Aware System Service) daemon starting
+ * @param      [in] res Instance of h/w resource
+ * @return     @c 0 on success, otherwise error value
+ */
+static int pass_hal_restore_battery_initdata(struct pass_resource *res)
+{
+       struct pass_resource_init_data *initdata = &res->init_data;
+       int charging_status = initdata->battery.charging_status;
+       int charging_current_uA = initdata->battery.charging_current_uA;
+       int ret;
+
+       if (charging_status >= 0) {
+               ret = pass_hal_set_battery_charging_status(res, charging_status);
+               if (ret < 0)
+                       return ret;
+       }
+
+       if (charging_current_uA >= 0) {
+               ret = pass_hal_set_battery_charging_current(res, charging_current_uA);
+               if (ret < 0)
+                       return ret;
+       }
+
+       return 0;
+}
+
 /**
  * @brief      Save the initial state of h/w resource before PASS
  *             (Power Aware System Service) daemon starting
@@ -783,6 +902,13 @@ int pass_hal_save_initdata(struct pass_resource *res)
                /* fall through */
        case PASS_RESOURCE_BATTERY_ID:
        case PASS_RESOURCE_NONSTANDARD_ID:
+               ret = pass_hal_save_battery_initdata(res);
+               if (ret < 0) {
+                       _E("Failed to save battery initdata for '%s' resource",
+                                                       res->config_data.res_name);
+                       return ret;
+               }
+
                ret = pass_hal_save_thermal_initdata(res);
                if (ret < 0) {
                        _E("Failed to save thermal initdata for '%s' resource",
@@ -840,6 +966,13 @@ int pass_hal_restore_initdata(struct pass_resource *res)
                /* fall through */
        case PASS_RESOURCE_BATTERY_ID:
        case PASS_RESOURCE_NONSTANDARD_ID:
+               ret = pass_hal_restore_battery_initdata(res);
+               if (ret < 0) {
+                       _E("Failed to restore battery initdata for '%s' resource",
+                                                       res->config_data.res_name);
+                       return ret;
+               }
+
                ret = pass_hal_restore_thermal_initdata(res);
                if (ret < 0) {
                        _E("Failed to restore thermal initdata for '%s' resource",
index f5e2f264db669a256a3e52d08fca6b1a301df545..459f04fb66bfe2409c78cd87202759b16d2bcfc0 100644 (file)
@@ -81,6 +81,14 @@ int pass_hal_get_cooling_device_state(struct pass_resource *res);
 int pass_hal_set_cooling_device_state(struct pass_resource *res, int state);
 int pass_hal_get_cooling_device_max_state(struct pass_resource *res);
 
+/* Get and set the battery charging state. */
+int pass_hal_set_battery_charging_status(struct pass_resource *res,int charging_status);
+int pass_hal_get_battery_charging_status(struct pass_resource *res);
+
+/* Get and set the battery charging current. */
+int pass_hal_set_battery_charging_current(struct pass_resource *res, int charging_current_uA);
+int pass_hal_get_battery_charging_current(struct pass_resource *res);
+
 /***
  * Functions for CPU H/W resources
  */
index e639777a450ce77196c40a852f03fbc8c4ad85a2..0aeb97b90d0324d71b6d8495802b9dd90c612f8d 100644 (file)
@@ -208,6 +208,8 @@ static void initialize_level(struct pass_level *level)
        level->fault_around_bytes = INIT_VALUE;
 
        level->cooling_device_state = INIT_VALUE;
+       level->charging_status = INIT_VALUE;
+       level->charging_current_uA = INIT_VALUE;
 }
 
 static int parse_level(struct pass_resource *res, json_object *obj,
@@ -237,6 +239,8 @@ static int parse_level(struct pass_resource *res, json_object *obj,
        double governor_timeout_sec;
        int fault_around_bytes;
        int cooling_device_state;
+       int charging_status;
+       int charging_current_uA;
 
        /* Get property values */
        level = get_int_from_object(obj, "level");
@@ -270,6 +274,8 @@ static int parse_level(struct pass_resource *res, json_object *obj,
        fault_around_bytes = get_int_from_object(obj, "memory,fault_around_bytes");
 
        cooling_device_state = get_int_from_object(obj, "thermal,cooling_device_state");
+       charging_status = get_int_from_object(obj, "battery,charging_status");
+       charging_current_uA = get_int_from_object(obj, "battery,charging_current_uA");
 
        /* Check the mandatory property values are valid or not */
        if (level < 0) {
@@ -364,6 +370,16 @@ static int parse_level(struct pass_resource *res, json_object *obj,
        if (cooling_device_state >= 0)
                target_level->cooling_device_state = cooling_device_state;
 
+       /*
+        * Properties for the following h/w resources:
+        * - PASS_RESOURCE_BATTERY_ID
+        * - PASS_RESOURCE_NONSTANDARD_ID
+        */
+       if (charging_status >= 0)
+               target_level->charging_status = charging_status;
+       if (charging_current_uA >= 0)
+               target_level->charging_current_uA = charging_current_uA;
+
        return 0;
 }
 
index 5e9b0edc060168dbb82abc2b9e22d465d620bd04..c406d97ac3527c5889a3cd036c9259786f5fe84f 100644 (file)
@@ -80,6 +80,17 @@ static void rescon_print_level(struct pass_resource *res,
                                res->config_data.res_name);
        }
 
+       if (level->charging_status >= 0) {
+               _D("charging_status     is %10d of '%s' resource\n",
+                               level->charging_status,
+                               res->config_data.res_name);
+       }
+
+       if (level->charging_current_uA >= 0) {
+               _D("charging_current_uA is %10d of '%s' resource\n",
+                               level->charging_current_uA,
+                               res->config_data.res_name);
+       }
 }
 
 static void rescon_adjust_level(struct pass_level *a, struct pass_level *b)
@@ -112,6 +123,11 @@ static void rescon_adjust_level(struct pass_level *a, struct pass_level *b)
        /* Adjust cooling_device_state */
        a->cooling_device_state = MAX(a->cooling_device_state,
                                        b->cooling_device_state);
+
+       /* Adjust charging_status and charging_current_uA */
+       a->charging_status = MAX(a->charging_status, b->charging_status);
+       a->charging_current_uA = MIN(a->charging_current_uA,
+                                       b->charging_current_uA);
 }
 
 static int rescon_update(struct pass_resource *res)
@@ -128,6 +144,8 @@ static int rescon_update(struct pass_resource *res)
        int limit_max_cpu = -1;
        int fault_around_bytes = -1;
        int cooling_device_state = -1;
+       int charging_status = -1;
+       int charging_current_uA = -1;
        int ret, i;
        int failed = 0;
 
@@ -143,6 +161,8 @@ static int rescon_update(struct pass_resource *res)
        adjusted_level.limit_max_cpu = MAX_INT;
        adjusted_level.fault_around_bytes = MIN_INT;
        adjusted_level.cooling_device_state = MIN_INT;
+       adjusted_level.charging_status = MIN_INT;
+       adjusted_level.charging_current_uA = MAX_INT;
 
        /* Adjust with pass_level */
        if (res->rescon.curr_level >= 0)
@@ -173,6 +193,8 @@ static int rescon_update(struct pass_resource *res)
        case PASS_RESOURCE_BATTERY_ID:
        case PASS_RESOURCE_NONSTANDARD_ID:
                cooling_device_state = adjusted_level.cooling_device_state;
+               charging_status = adjusted_level.charging_status;
+               charging_current_uA = adjusted_level.charging_current_uA;
                break;
        case PASS_RESOURCE_MEMORY_ID:
                fault_around_bytes = adjusted_level.fault_around_bytes;
@@ -288,6 +310,28 @@ static int rescon_update(struct pass_resource *res)
                }
        }
 
+       /* Set charging_status for battery h/w */
+       if (charging_status >= 0) {
+               ret = pass_hal_set_battery_charging_status(res, charging_status);
+               if (ret < 0) {
+                       _W("failed to set the charging_status(%d) of %s",
+                                               charging_status,
+                                               res->config_data.res_name);
+                       failed = 1;
+               }
+       }
+
+       /* Set charging_current_uA for battery h/w */
+       if (charging_current_uA >= 0) {
+               ret = pass_hal_set_battery_charging_current(res, charging_current_uA);
+               if (ret < 0) {
+                       _W("failed to set the charging_current_uA(%d) of %s",
+                                               charging_current_uA,
+                                               res->config_data.res_name);
+                       failed = 1;
+               }
+       }
+
        if (!failed)
                rescon_print_level(res, &adjusted_level);
        else
index 3399167e534ad3713c91e16a670d6e7ecc24580b..e5be2a782ffd125354442dc7cb9623a57f0df709 100644 (file)
@@ -161,6 +161,15 @@ struct pass_level {
         */
        int cooling_device_state;
 
+       /**
+        * The current state of battery charging device
+        * and this property is used for the following resources:
+        * - PASS_RESOURCE_BATTERY_ID
+        * - PASS_RESOURCE_NONSTANDARD_ID
+        */
+       int charging_status;
+       int charging_current_uA;
+
        /* Properties for the timer */
 
        /**
@@ -541,6 +550,13 @@ struct pass_resource_init_data {
                int cooling_device_state;
        } tmu;
 
+       /** Initial state of battery charging configuration */
+       struct {
+               /** Charging state */
+               int charging_status;
+               int charging_current_uA;
+       } battery;
+
        /** Initial state of memory configuration */
        struct {
                /** the number of bytes to be mapped around the fault */