Skip to content

Commit

Permalink
[charging] Auto-detect charging hysteresis controls. Fixes JB#57297
Browse files Browse the repository at this point in the history
Adding device specific configuration files for all currently supported
device types would be burdensome task.

Add auto-detect logic for the two kernel types where enabling/disabling
charging is known to work.

If control path is defined in mce configuration files, auto-detect
logic is disabled.

Signed-off-by: Simo Piiroinen <[email protected]>
  • Loading branch information
spiiroin committed Mar 9, 2022
1 parent e5297ca commit 35c5052
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 23 deletions.
8 changes: 8 additions & 0 deletions inifiles/charging.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@

[Charging]

# Devices with kernels that utilize
# - POWER_SUPPLY_PROP_CHARGING_ENABLED
# - POWER_SUPPLY_PROP_INPUT_SUSPEND
# are detected automatically and do not need to be configured.
#
# If configuration data is present, it takes precedence over
# autodetect logic.

# Devices where kernel utilizes POWER_SUPPLY_PROP_CHARGING_ENABLED
# For example: f5121 (Xperia X), l500d (Jolla C)

Expand Down
81 changes: 58 additions & 23 deletions modules/charging.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,45 +528,80 @@ mch_settings_quit(void)
* MCH_CONFIG
* ========================================================================= */

static const struct {
const char *control_path;
const char *enable_value;
const char *disable_value;
} mch_autoconfig[] = {
{
.control_path = "/sys/class/power_supply/battery/charging_enabled",
.enable_value = "1",
.disable_value = "0",
},
{
.control_path = "/sys/class/power_supply/battery/input_suspend",
.enable_value = "0",
.disable_value = "1",
},
{
.control_path = NULL,
}
};

static void
mch_config_init(void)
{
bool ack = false;

if( !mce_conf_has_group(MCE_CONF_CHARGING_GROUP) ) {
mce_log(LL_DEBUG, "[%s]: config block does not exist",
MCE_CONF_CHARGING_GROUP);
goto EXIT;
if( mce_conf_has_group(MCE_CONF_CHARGING_GROUP) ) {
mch_control_path =
mce_conf_get_string(MCE_CONF_CHARGING_GROUP,
MCE_CONF_CHARGING_CONTROL_PATH,
NULL);
}

mch_control_path = mce_conf_get_string(MCE_CONF_CHARGING_GROUP,
MCE_CONF_CHARGING_CONTROL_PATH, 0);
if( !mch_control_path ) {
mce_log(LL_WARN, "[%s] %s: config item not defined",
MCE_CONF_CHARGING_GROUP,
MCE_CONF_CHARGING_CONTROL_PATH);
goto EXIT;
if( mch_control_path ) {
if( access(mch_control_path, W_OK) == -1 ) {
mce_log(LL_ERR, "%s: not writable: %m", mch_control_path);
goto EXIT;
}
mch_control_enable_value =
mce_conf_get_string(MCE_CONF_CHARGING_GROUP,
MCE_CONF_CHARGING_ENABLE_VALUE,
DEFAULT_CHARGING_ENABLE_VALUE);

mch_control_disable_value =
mce_conf_get_string(MCE_CONF_CHARGING_GROUP,
MCE_CONF_CHARGING_DISABLE_VALUE,
DEFAULT_CHARGING_DISABLE_VALUE);
}

if( access(mch_control_path, W_OK) == -1 ) {
mce_log(LL_ERR, "%s: not writable: %m", mch_control_path);
goto EXIT;
else {
for( size_t i = 0; ; ++i ) {
if( !mch_autoconfig[i].control_path )
goto EXIT;

if( access(mch_autoconfig[i].control_path, W_OK) == -1 )
continue;

mch_control_path = g_strdup(mch_autoconfig[i].control_path);
mch_control_enable_value =
g_strdup(mch_autoconfig[i].enable_value);
mch_control_disable_value =
g_strdup(mch_autoconfig[i].disable_value);
break;
}
}

mch_control_enable_value = mce_conf_get_string(MCE_CONF_CHARGING_GROUP,
MCE_CONF_CHARGING_ENABLE_VALUE,
DEFAULT_CHARGING_ENABLE_VALUE);

mch_control_disable_value = mce_conf_get_string(MCE_CONF_CHARGING_GROUP,
MCE_CONF_CHARGING_DISABLE_VALUE,
DEFAULT_CHARGING_DISABLE_VALUE);

ack = true;

EXIT:
if( !ack )
mch_config_quit();

mce_log(LL_DEBUG, "control: %s", mch_control_path ?: "N/A");
mce_log(LL_DEBUG, "enable: %s", mch_control_enable_value ?: "N/A");
mce_log(LL_DEBUG, "disable: %s", mch_control_disable_value ?: "N/A");

return;
}

Expand Down

0 comments on commit 35c5052

Please sign in to comment.