From 555299af9f0ee71c1ad86a6c6846748861be458b Mon Sep 17 00:00:00 2001 From: Solomon Peachy Date: Wed, 10 Mar 2021 18:10:00 -0500 Subject: [PATCH] hosted: Consolidate the code that polls the battery charging status affects all hiby targets, fiiom3k, and ibasso dx50/dx90 As well as deduplicating a small pile of code, this also implements hysteresis so we're not doing a sysfs read/lookup multiple times back-to-back every time the power management tick fires. Change-Id: I2f7672acbb36341becf67e07960c24c681270d09 --- firmware/SOURCES | 2 + firmware/export/config/fiiom3klinux.h | 4 ++ firmware/export/config/hibylinux.h | 4 ++ firmware/export/config/ibassodx50.h | 4 ++ firmware/export/config/ibassodx90.h | 4 ++ firmware/target/hosted/agptek/power-agptek.c | 22 ------- firmware/target/hosted/agptek/power-agptek.h | 3 - .../target/hosted/agptek/powermgmt-agptek.c | 11 ---- firmware/target/hosted/aigo/power-erosq.c | 22 ------- firmware/target/hosted/aigo/power-erosq.h | 3 - firmware/target/hosted/aigo/powermgmt-erosq.c | 11 ---- firmware/target/hosted/fiio/power-fiio.c | 24 ------- firmware/target/hosted/fiio/power-fiio.h | 3 - firmware/target/hosted/fiio/powermgmt-fiio.c | 11 ---- firmware/target/hosted/ibasso/power-ibasso.c | 41 ------------ firmware/target/hosted/power-linux.c | 62 +++++++++++++++++++ firmware/target/hosted/xduoo/power-xduoo.c | 22 ------- firmware/target/hosted/xduoo/power-xduoo.h | 3 - .../target/hosted/xduoo/powermgmt-xduoo.c | 11 ---- 19 files changed, 80 insertions(+), 187 deletions(-) create mode 100644 firmware/target/hosted/power-linux.c diff --git a/firmware/SOURCES b/firmware/SOURCES index 36a125783c..430eb4119b 100644 --- a/firmware/SOURCES +++ b/firmware/SOURCES @@ -127,6 +127,7 @@ target/hosted/sysfs.c target/hosted/backlight-unix.c target/hosted/system-hosted.c target/hosted/lcd-linuxfb.c +target/hosted/power-linux.c #endif #if defined(AGPTEK_ROCKER) && !defined(SIMULATOR) @@ -1919,6 +1920,7 @@ target/hosted/lc-unix.c target/hosted/ibasso/audiohw-ibasso.c target/hosted/ibasso/backlight-ibasso.c target/hosted/ibasso/button-ibasso.c +target/hosted/power-linux.c #ifdef DEBUG target/hosted/ibasso/debug-ibasso.c #endif diff --git a/firmware/export/config/fiiom3klinux.h b/firmware/export/config/fiiom3klinux.h index 51d2a222e6..0ce58be46f 100644 --- a/firmware/export/config/fiiom3klinux.h +++ b/firmware/export/config/fiiom3klinux.h @@ -128,3 +128,7 @@ #define BOOTFILE_EXT "m3k" #define BOOTFILE "rockbox." BOOTFILE_EXT #define BOOTDIR "/.rockbox" + +/* More stuff */ +#define BATTERY_DEV_NAME "battery" +#define POWER_DEV_NAME "ac" diff --git a/firmware/export/config/hibylinux.h b/firmware/export/config/hibylinux.h index e73faa7608..f1e52917ba 100644 --- a/firmware/export/config/hibylinux.h +++ b/firmware/export/config/hibylinux.h @@ -84,3 +84,7 @@ #define MULTIDRIVE_DIR "/mnt/usb" #define MULTIDRIVE_DEV "/sys/block/sda" #define ROOTDRIVE_DEV "/sys/block/mmcblk0" + +/* More common stuff */ +#define BATTERY_DEV_NAME "battery" +#define POWER_DEV_NAME "usb" diff --git a/firmware/export/config/ibassodx50.h b/firmware/export/config/ibassodx50.h index 0a7c04a739..6bebf5a278 100644 --- a/firmware/export/config/ibassodx50.h +++ b/firmware/export/config/ibassodx50.h @@ -143,3 +143,7 @@ /* No special storage */ #define CONFIG_STORAGE STORAGE_HOSTFS #define HAVE_STORAGE_FLUSH + +/* More common stuff */ +#define BATTERY_DEV_NAME "battery" +#define POWER_DEV_NAME "usb" diff --git a/firmware/export/config/ibassodx90.h b/firmware/export/config/ibassodx90.h index 7071f50959..0b33665eb4 100644 --- a/firmware/export/config/ibassodx90.h +++ b/firmware/export/config/ibassodx90.h @@ -140,3 +140,7 @@ /* No special storage */ #define CONFIG_STORAGE STORAGE_HOSTFS #define HAVE_STORAGE_FLUSH + +/* More common stuff */ +#define BATTERY_DEV_NAME "battery" +#define POWER_DEV_NAME "usb" diff --git a/firmware/target/hosted/agptek/power-agptek.c b/firmware/target/hosted/agptek/power-agptek.c index 023d3888b9..a327ba6d06 100644 --- a/firmware/target/hosted/agptek/power-agptek.c +++ b/firmware/target/hosted/agptek/power-agptek.c @@ -32,28 +32,6 @@ const char * const sysfs_bat_voltage = "/sys/class/power_supply/battery/voltage_now"; -const char * const sysfs_bat_status = - "/sys/class/power_supply/battery/status"; - -const char * const sysfs_pow_supply = - "/sys/class/power_supply/usb/present"; - -unsigned int agptek_power_input_status(void) -{ - int present = 0; - sysfs_get_int(sysfs_pow_supply, &present); - - return present ? POWER_INPUT_USB_CHARGER : POWER_INPUT_NONE; -} - -bool agptek_power_charging_status(void) -{ - char buf[12] = {0}; - sysfs_get_string(sysfs_bat_status, buf, sizeof(buf)); - - return (strncmp(buf, "Charging", 8) == 0); -} - unsigned int agptek_power_get_battery_voltage(void) { int battery_voltage; diff --git a/firmware/target/hosted/agptek/power-agptek.h b/firmware/target/hosted/agptek/power-agptek.h index 1ae2ff43d6..c6ca1eb9c9 100644 --- a/firmware/target/hosted/agptek/power-agptek.h +++ b/firmware/target/hosted/agptek/power-agptek.h @@ -23,8 +23,5 @@ #include #include "config.h" -unsigned int agptek_power_input_status(void); -bool agptek_power_charging_status(void); unsigned int agptek_power_get_battery_voltage(void); #endif /* _POWER_AGPTEK_H_ */ - diff --git a/firmware/target/hosted/agptek/powermgmt-agptek.c b/firmware/target/hosted/agptek/powermgmt-agptek.c index bd7e4157d6..beb2dc14b8 100644 --- a/firmware/target/hosted/agptek/powermgmt-agptek.c +++ b/firmware/target/hosted/agptek/powermgmt-agptek.c @@ -44,18 +44,7 @@ const unsigned short const percent_to_volt_charge[11] = 3485, 3780, 3836, 3857, 3890, 3930, 3986, 4062, 4158, 4185, 4196 }; -unsigned int power_input_status(void) -{ - /* POWER_INPUT_USB_CHARGER, POWER_INPUT_NONE */ - return agptek_power_input_status(); -} - int _battery_voltage(void) { return agptek_power_get_battery_voltage(); } - -bool charging_state(void) -{ - return agptek_power_charging_status(); -} diff --git a/firmware/target/hosted/aigo/power-erosq.c b/firmware/target/hosted/aigo/power-erosq.c index 0a4f820337..73fa5fe972 100644 --- a/firmware/target/hosted/aigo/power-erosq.c +++ b/firmware/target/hosted/aigo/power-erosq.c @@ -35,28 +35,6 @@ const char * const sysfs_bat_voltage = const char * const sysfs_bat_capacity = "/sys/class/power_supply/battery/capacity"; -const char * const sysfs_bat_status = - "/sys/class/power_supply/battery/status"; - -const char * const sysfs_pow_supply = - "/sys/class/power_supply/usb/present"; - -unsigned int erosq_power_input_status(void) -{ - int present = 0; - sysfs_get_int(sysfs_pow_supply, &present); - - return present ? POWER_INPUT_USB_CHARGER : POWER_INPUT_NONE; -} - -bool erosq_power_charging_status(void) -{ - char buf[12] = {0}; - sysfs_get_string(sysfs_bat_status, buf, sizeof(buf)); - - return (strncmp(buf, "Charging", 8) == 0); -} - unsigned int erosq_power_get_battery_voltage(void) { int battery_voltage; diff --git a/firmware/target/hosted/aigo/power-erosq.h b/firmware/target/hosted/aigo/power-erosq.h index d06b956924..213d55faa6 100644 --- a/firmware/target/hosted/aigo/power-erosq.h +++ b/firmware/target/hosted/aigo/power-erosq.h @@ -23,9 +23,6 @@ #include #include "config.h" -unsigned int erosq_power_input_status(void); -bool erosq_power_charging_status(void); unsigned int erosq_power_get_battery_voltage(void); unsigned int erosq_power_get_battery_capacity(void); #endif /* _POWER_XDUOO_H_ */ - diff --git a/firmware/target/hosted/aigo/powermgmt-erosq.c b/firmware/target/hosted/aigo/powermgmt-erosq.c index e02dbc6244..5067c66426 100644 --- a/firmware/target/hosted/aigo/powermgmt-erosq.c +++ b/firmware/target/hosted/aigo/powermgmt-erosq.c @@ -45,18 +45,7 @@ const unsigned short const percent_to_volt_charge[11] = 3485, 3780, 3836, 3857, 3890, 3930, 3986, 4062, 4158, 4185, 4196 }; -unsigned int power_input_status(void) -{ - /* POWER_INPUT_USB_CHARGER, POWER_INPUT_NONE */ - return erosq_power_input_status(); -} - int _battery_voltage(void) { return erosq_power_get_battery_voltage(); } - -bool charging_state(void) -{ - return erosq_power_charging_status(); -} diff --git a/firmware/target/hosted/fiio/power-fiio.c b/firmware/target/hosted/fiio/power-fiio.c index a2b19ce550..086c3bc9cb 100644 --- a/firmware/target/hosted/fiio/power-fiio.c +++ b/firmware/target/hosted/fiio/power-fiio.c @@ -37,30 +37,6 @@ const char * const sysfs_bat_voltage = const char * const sysfs_bat_capacity = "/sys/class/power_supply/battery/capacity"; -const char * const sysfs_bat_status = - "/sys/class/power_supply/battery/status"; - -const char * const sysfs_pow_supply = - "/sys/class/power_supply/ac/online"; - -unsigned int fiio_power_input_status(void) -{ - int present = 0; - sysfs_get_int(sysfs_pow_supply, &present); - - usb_enable(present ? true : false); - - return present ? POWER_INPUT_USB_CHARGER : POWER_INPUT_NONE; -} - -bool fiio_power_charging_status(void) -{ - char buf[12] = {0}; - sysfs_get_string(sysfs_bat_status, buf, sizeof(buf)); - - return (strncmp(buf, "Charging", 8) == 0); -} - unsigned int fiio_power_get_battery_voltage(void) { int battery_voltage; diff --git a/firmware/target/hosted/fiio/power-fiio.h b/firmware/target/hosted/fiio/power-fiio.h index c3085e9569..47143a3f67 100644 --- a/firmware/target/hosted/fiio/power-fiio.h +++ b/firmware/target/hosted/fiio/power-fiio.h @@ -23,9 +23,6 @@ #include #include "config.h" -unsigned int fiio_power_input_status(void); -bool fiio_power_charging_status(void); unsigned int fiio_power_get_battery_voltage(void); unsigned int fiio_power_get_battery_capacity(void); #endif /* _POWER_FIIO_H_ */ - diff --git a/firmware/target/hosted/fiio/powermgmt-fiio.c b/firmware/target/hosted/fiio/powermgmt-fiio.c index b7c1b5fde2..da6305d9c4 100644 --- a/firmware/target/hosted/fiio/powermgmt-fiio.c +++ b/firmware/target/hosted/fiio/powermgmt-fiio.c @@ -44,12 +44,6 @@ const unsigned short const percent_to_volt_charge[11] = 3485, 3780, 3836, 3857, 3890, 3930, 3986, 4062, 4158, 4185, 4196 }; -unsigned int power_input_status(void) -{ - /* POWER_INPUT_USB_CHARGER, POWER_INPUT_NONE */ - return fiio_power_input_status(); -} - int _battery_voltage(void) { return fiio_power_get_battery_voltage(); @@ -61,8 +55,3 @@ int _battery_level(void) return fiio_power_get_battery_capacity(); } #endif - -bool charging_state(void) -{ - return fiio_power_charging_status(); -} diff --git a/firmware/target/hosted/ibasso/power-ibasso.c b/firmware/target/hosted/ibasso/power-ibasso.c index 8257de5f33..7d37913c7b 100644 --- a/firmware/target/hosted/ibasso/power-ibasso.c +++ b/firmware/target/hosted/ibasso/power-ibasso.c @@ -36,27 +36,6 @@ #include "sysfs-ibasso.h" #include "vold-ibasso.h" - -unsigned int power_input_status(void) -{ - /*TRACE;*/ - - /* - /sys/class/power_supply/usb/present - 0: No external power supply connected. - 1: External power supply connected. - */ - int val = 0; - if(! sysfs_get_int(SYSFS_USB_POWER_PRESENT, &val)) - { - DEBUGF("ERROR %s: Can not get power supply status.", __func__); - return POWER_INPUT_NONE; - } - - return val ? POWER_INPUT_USB_CHARGER : POWER_INPUT_NONE; -} - - void power_off(void) { TRACE; @@ -75,23 +54,3 @@ void power_off(void) reboot(RB_POWER_OFF); } - - -/* Returns true, if battery is charging, false else. */ -bool charging_state(void) -{ - /*TRACE;*/ - - /* - /sys/class/power_supply/battery/status - "Full", "Charging", "Discharging" - */ - char state[9]; - if(! sysfs_get_string(SYSFS_BATTERY_STATUS, state, 9)) - { - DEBUGF("ERROR %s: Can not get battery charging state.", __func__); - return false; - } - - return(strcmp(state, "Charging") == 0);; -} diff --git a/firmware/target/hosted/power-linux.c b/firmware/target/hosted/power-linux.c new file mode 100644 index 0000000000..ea8d787ad5 --- /dev/null +++ b/firmware/target/hosted/power-linux.c @@ -0,0 +1,62 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * + * Copyright (C) 2021 by Solomon Peachy + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#include +#include +#include +#include +#include + +#include "system.h" +#include "power.h" +#include "panic.h" +#include "sysfs.h" + +#include "tick.h" + +#define BATTERY_STATUS_PATH "/sys/class/power_supply/" BATTERY_DEV_NAME "/status" +#define POWER_STATUS_PATH "/sys/class/power_supply/" POWER_DEV_NAME "/online" + +/* We get called multiple times per tick, let's cut that back! */ +static long last_tick = 0; +static bool last_power = false; + +bool charging_state(void) +{ + if ((current_tick - last_tick) > HZ/2 ) { + char buf[12] = {0}; + sysfs_get_string(BATTERY_STATUS_PATH, buf, sizeof(buf)); + + last_tick = current_tick; + last_power = (strncmp(buf, "Charging", 8) == 0); + } + return last_power; +} + +unsigned int power_input_status(void) +{ + int present = 0; + sysfs_get_int(POWER_STATUS_PATH, &present); + +#ifdef FIIO_M3K_LINUX + usb_enable(present ? true : false); +#endif + + return present ? POWER_INPUT_USB_CHARGER : POWER_INPUT_NONE; +} diff --git a/firmware/target/hosted/xduoo/power-xduoo.c b/firmware/target/hosted/xduoo/power-xduoo.c index 97a8dd4779..0b1e2f816e 100644 --- a/firmware/target/hosted/xduoo/power-xduoo.c +++ b/firmware/target/hosted/xduoo/power-xduoo.c @@ -35,28 +35,6 @@ const char * const sysfs_bat_voltage = const char * const sysfs_bat_capacity = "/sys/class/power_supply/battery/capacity"; -const char * const sysfs_bat_status = - "/sys/class/power_supply/battery/status"; - -const char * const sysfs_pow_supply = - "/sys/class/power_supply/usb/present"; - -unsigned int xduoo_power_input_status(void) -{ - int present = 0; - sysfs_get_int(sysfs_pow_supply, &present); - - return present ? POWER_INPUT_USB_CHARGER : POWER_INPUT_NONE; -} - -bool xduoo_power_charging_status(void) -{ - char buf[12] = {0}; - sysfs_get_string(sysfs_bat_status, buf, sizeof(buf)); - - return (strncmp(buf, "Charging", 8) == 0); -} - unsigned int xduoo_power_get_battery_voltage(void) { int battery_voltage; diff --git a/firmware/target/hosted/xduoo/power-xduoo.h b/firmware/target/hosted/xduoo/power-xduoo.h index d573865ed2..ee016bd60b 100644 --- a/firmware/target/hosted/xduoo/power-xduoo.h +++ b/firmware/target/hosted/xduoo/power-xduoo.h @@ -23,9 +23,6 @@ #include #include "config.h" -unsigned int xduoo_power_input_status(void); -bool xduoo_power_charging_status(void); unsigned int xduoo_power_get_battery_voltage(void); unsigned int xduoo_power_get_battery_capacity(void); #endif /* _POWER_XDUOO_H_ */ - diff --git a/firmware/target/hosted/xduoo/powermgmt-xduoo.c b/firmware/target/hosted/xduoo/powermgmt-xduoo.c index 35d9e45ffc..855072031e 100644 --- a/firmware/target/hosted/xduoo/powermgmt-xduoo.c +++ b/firmware/target/hosted/xduoo/powermgmt-xduoo.c @@ -44,12 +44,6 @@ const unsigned short const percent_to_volt_charge[11] = 3485, 3780, 3836, 3857, 3890, 3930, 3986, 4062, 4158, 4185, 4196 }; -unsigned int power_input_status(void) -{ - /* POWER_INPUT_USB_CHARGER, POWER_INPUT_NONE */ - return xduoo_power_input_status(); -} - #if defined(XDUOO_X3II) int _battery_voltage(void) { @@ -63,8 +57,3 @@ int _battery_level(void) return xduoo_power_get_battery_capacity(); } #endif - -bool charging_state(void) -{ - return xduoo_power_charging_status(); -}