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
This commit is contained in:
Solomon Peachy 2021-03-10 18:10:00 -05:00
parent b3b8310e4e
commit 555299af9f
19 changed files with 80 additions and 187 deletions

View File

@ -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

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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;

View File

@ -23,8 +23,5 @@
#include <stdbool.h>
#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_ */

View File

@ -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();
}

View File

@ -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;

View File

@ -23,9 +23,6 @@
#include <stdbool.h>
#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_ */

View File

@ -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();
}

View File

@ -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;

View File

@ -23,9 +23,6 @@
#include <stdbool.h>
#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_ */

View File

@ -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();
}

View File

@ -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);;
}

View File

@ -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 <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#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;
}

View File

@ -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;

View File

@ -23,9 +23,6 @@
#include <stdbool.h>
#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_ */

View File

@ -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();
}