hosted: consolidate sysfs-based battery measurement code

Several hosted targets read their battery state from a fixed
sysfs path. Get rid of the duplicated code by handling this
common case in power-linux.c.

Some targets use non-standard units in sysfs instead of the
typical microvolts / microamps, so allow the scale factors
to be overridden by the target.

Change-Id: I31dc4ffc7a2d2c066d00a74070f9f9849c1663d0
This commit is contained in:
Aidan MacDonald 2025-12-01 13:32:26 +00:00
parent 7ffdf50ba5
commit 806b71b2ed
20 changed files with 74 additions and 454 deletions

View file

@ -1,41 +0,0 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* Copyright (C) 2017 by Marcin Bukat
*
* 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-agptek.h"
#include "power.h"
#include "panic.h"
#include "sysfs.h"
const char * const sysfs_bat_voltage =
"/sys/class/power_supply/battery/voltage_now";
unsigned int agptek_power_get_battery_voltage(void)
{
int battery_voltage;
sysfs_get_int(sysfs_bat_voltage, &battery_voltage);
return battery_voltage/1000;
}

View file

@ -1,27 +0,0 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* Copyright (C) 2017 by Marcin Bukat
*
* 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.
*
****************************************************************************/
#ifndef _POWER_AGPTEK_H_
#define _POWER_AGPTEK_H_
#include <stdbool.h>
#include "config.h"
unsigned int agptek_power_get_battery_voltage(void);
#endif /* _POWER_AGPTEK_H_ */

View file

@ -19,7 +19,6 @@
****************************************************************************/
#include "powermgmt.h"
#include "power.h"
#include "power-agptek.h"
unsigned short battery_level_disksafe = 3470;
@ -37,8 +36,3 @@ unsigned short percent_to_volt_charge[11] =
{
3485, 3780, 3836, 3857, 3890, 3930, 3986, 4062, 4158, 4185, 4196
};
int _battery_voltage(void)
{
return agptek_power_get_battery_voltage();
}

View file

@ -1,64 +0,0 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* Copyright (C) 2025 Hairo R. Carela
*
* 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-rgnano.h"
#include "power.h"
#include "panic.h"
#include "sysfs.h"
const char * const sysfs_bat_voltage =
"/sys/class/power_supply/axp20x-battery/voltage_now";
const char * const sysfs_bat_current =
"/sys/class/power_supply/axp20x-battery/current_now";
const char * const sysfs_bat_level =
"/sys/class/power_supply/axp20x-battery/capacity";
unsigned int rgnano_power_get_battery_voltage(void)
{
int battery_voltage;
sysfs_get_int(sysfs_bat_voltage, &battery_voltage);
return battery_voltage;
}
unsigned int rgnano_power_get_battery_current(void)
{
int battery_current;
sysfs_get_int(sysfs_bat_current, &battery_current);
/* Current is in microamps */
return (battery_current / 1000);
}
unsigned int rgnano_power_get_battery_capacity(void)
{
int battery_level;
sysfs_get_int(sysfs_bat_level, &battery_level);
return battery_level;
}

View file

@ -1,29 +0,0 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* Copyright (C) 2025 Hairo R. Carela
*
* 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.
*
****************************************************************************/
#ifndef _POWER_RGNANO_H_
#define _POWER_RGNANO_H_
#include <stdbool.h>
#include "config.h"
unsigned int rgnano_power_get_battery_voltage(void);
unsigned int rgnano_power_get_battery_current(void);
unsigned int rgnano_power_get_battery_capacity(void);
#endif /* _POWER_RGNANO_H_ */

View file

@ -20,7 +20,6 @@
****************************************************************************/
#include "powermgmt.h"
#include "power.h"
#include "power-rgnano.h"
/* System handles powering off at 2% */
unsigned short battery_level_disksafe = 0;
@ -39,18 +38,3 @@ unsigned short percent_to_volt_charge[11] =
{
3512, 3729, 3795, 3831, 3865, 3906, 3953, 4010, 4072, 4150, 4186
};
int _battery_voltage(void)
{
return rgnano_power_get_battery_voltage();
}
int _battery_current(void)
{
return rgnano_power_get_battery_current();
}
int _battery_level(void)
{
return rgnano_power_get_battery_capacity();
}

View file

@ -1,54 +0,0 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* Copyright (C) 2017 by Marcin Bukat
*
* 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 "usb.h"
#include "power-fiio.h"
const char * const sysfs_bat_voltage =
"/sys/class/power_supply/battery/voltage_now";
const char * const sysfs_bat_capacity =
"/sys/class/power_supply/battery/capacity";
unsigned int fiio_power_get_battery_voltage(void)
{
int battery_voltage;
sysfs_get_int(sysfs_bat_voltage, &battery_voltage);
return battery_voltage;
}
unsigned int fiio_power_get_battery_capacity(void)
{
int battery_capacity;
sysfs_get_int(sysfs_bat_capacity, &battery_capacity);
return battery_capacity * 20;
}

View file

@ -1,28 +0,0 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* Copyright (C) 2017 by Marcin Bukat
*
* 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.
*
****************************************************************************/
#ifndef _POWER_FIIO_H_
#define _POWER_FIIO_H_
#include <stdbool.h>
#include "config.h"
unsigned int fiio_power_get_battery_voltage(void);
unsigned int fiio_power_get_battery_capacity(void);
#endif /* _POWER_FIIO_H_ */

View file

@ -19,7 +19,6 @@
****************************************************************************/
#include "powermgmt.h"
#include "power.h"
#include "power-fiio.h"
unsigned short battery_level_disksafe = 3470;
@ -37,15 +36,3 @@ unsigned short percent_to_volt_charge[11] =
{
3485, 3780, 3836, 3857, 3890, 3930, 3986, 4062, 4158, 4185, 4196
};
int _battery_voltage(void)
{
return fiio_power_get_battery_voltage();
}
#if 0
int _battery_level(void)
{
return fiio_power_get_battery_capacity();
}
#endif

View file

@ -26,7 +26,6 @@
#include "usb.h"
#include "sysfs.h"
#include "power.h"
#include "power-fiio.h"
#ifdef HAVE_MULTIDRIVE
void cleanup_rbhome(void);

View file

@ -34,7 +34,38 @@
#endif
#ifdef BATTERY_DEV_NAME
#define BATTERY_STATUS_PATH "/sys/class/power_supply/" BATTERY_DEV_NAME "/status"
# define BATTERY_SYSFS_PATH "/sys/class/power_supply/" BATTERY_DEV_NAME
# define BATTERY_STATUS_PATH BATTERY_SYSFS_PATH "/status"
# define BATTERY_VOLTAGE_PATH BATTERY_SYSFS_PATH "/voltage_now"
# define BATTERY_CURRENT_PATH BATTERY_SYSFS_PATH "/current_now"
# define BATTERY_LEVEL_PATH BATTERY_SYSFS_PATH "/capacity"
#endif
/* Voltage is normally in microvolts */
#ifndef BATTERY_VOLTAGE_SCALE_MUL
# define BATTERY_VOLTAGE_SCALE_MUL 1
#endif
#ifndef BATTERY_VOLTAGE_SCALE_DIV
# define BATTERY_VOLTAGE_SCALE_DIV 1000
#endif
/* Current is normally in microamps */
#ifndef BATTERY_CURRENT_SCALE_MUL
# define BATTERY_CURRENT_SCALE_MUL 1
#endif
#ifndef BATTERY_CURRENT_SCALE_DIV
# define BATTERY_CURRENT_SCALE_DIV 1000
#endif
/* Level is normally in whole percentage points (0-100) */
#ifndef BATTERY_LEVEL_SCALE_MUL
# define BATTERY_LEVEL_SCALE_MUL 1
#endif
#ifndef BATTERY_LEVEL_SCALE_DIV
# define BATTERY_LEVEL_SCALE_DIV 1
#endif
#define POWER_STATUS_PATH "/sys/class/power_supply/" POWER_DEV_NAME "/online"
@ -55,6 +86,36 @@ bool charging_state(void)
}
return last_power;
}
#if (CONFIG_BATTERY_MEASURE & VOLTAGE_MEASURE)
int _battery_voltage(void)
{
int voltage = 0;
sysfs_get_int(BATTERY_VOLTAGE_PATH, &voltage);
return (voltage * BATTERY_VOLTAGE_SCALE_MUL) / BATTERY_VOLTAGE_SCALE_DIV;
}
#endif
#if (CONFIG_BATTERY_MEASURE & CURRENT_MEASURE)
int _battery_current(void)
{
int current = 0;
sysfs_get_int(BATTERY_CURRENT_PATH, &current);
return (current * BATTERY_CURRENT_SCALE_MUL) / BATTERY_CURRENT_SCALE_DIV;
}
#endif
#if (CONFIG_BATTERY_MEASURE & PERCENTAGE_MEASURE)
int _battery_level(void)
{
int level = 0;
sysfs_get_int(BATTERY_LEVEL_PATH, &level);
return (level * BATTERY_LEVEL_SCALE_MUL) / BATTERY_LEVEL_SCALE_DIV;
}
#endif
#endif
unsigned int power_input_status(void)

View file

@ -1,41 +0,0 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* Copyright (C) 2017 by Marcin Bukat
*
* 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-f28.h"
#include "power.h"
#include "panic.h"
#include "sysfs.h"
const char * const sysfs_bat_voltage =
"/sys/class/power_supply/battery/voltage_now";
unsigned int f28_power_get_battery_voltage(void)
{
int battery_voltage;
sysfs_get_int(sysfs_bat_voltage, &battery_voltage);
return battery_voltage/1000;
}

View file

@ -1,27 +0,0 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* Copyright (C) 2017 by Marcin Bukat
*
* 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.
*
****************************************************************************/
#ifndef _POWER_F28_H_
#define _POWER_F28_H_
#include <stdbool.h>
#include "config.h"
unsigned int f28_power_get_battery_voltage(void);
#endif /* _POWER_F28_H_ */

View file

@ -19,7 +19,6 @@
****************************************************************************/
#include "powermgmt.h"
#include "power.h"
#include "power-f28.h"
// XXX all of this... blabla
// max voltage is 4360 apparently
@ -40,8 +39,3 @@ unsigned short percent_to_volt_charge[11] =
{
3485, 3780, 3836, 3857, 3890, 3930, 3986, 4062, 4158, 4185, 4196
};
int _battery_voltage(void)
{
return f28_power_get_battery_voltage();
}

View file

@ -1,52 +0,0 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* Copyright (C) 2017 by Marcin Bukat
*
* 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-xduoo.h"
#include "power.h"
#include "panic.h"
#include "sysfs.h"
const char * const sysfs_bat_voltage =
"/sys/class/power_supply/battery/voltage_now";
const char * const sysfs_bat_capacity =
"/sys/class/power_supply/battery/capacity";
unsigned int xduoo_power_get_battery_voltage(void)
{
int battery_voltage;
sysfs_get_int(sysfs_bat_voltage, &battery_voltage);
return battery_voltage/1000;
}
unsigned int xduoo_power_get_battery_capacity(void)
{
int battery_capacity;
sysfs_get_int(sysfs_bat_capacity, &battery_capacity);
return battery_capacity;
}

View file

@ -1,28 +0,0 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* Copyright (C) 2017 by Marcin Bukat
*
* 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.
*
****************************************************************************/
#ifndef _POWER_XDUOO_H_
#define _POWER_XDUOO_H_
#include <stdbool.h>
#include "config.h"
unsigned int xduoo_power_get_battery_voltage(void);
unsigned int xduoo_power_get_battery_capacity(void);
#endif /* _POWER_XDUOO_H_ */

View file

@ -19,7 +19,6 @@
****************************************************************************/
#include "powermgmt.h"
#include "power.h"
#include "power-xduoo.h"
unsigned short battery_level_disksafe = 3443; /* 5% */
@ -37,17 +36,3 @@ unsigned short percent_to_volt_charge[11] =
{
3485, 3780, 3836, 3857, 3890, 3930, 3986, 4062, 4158, 4185, 4196
};
#if defined(XDUOO_X3II)
int _battery_voltage(void)
{
return xduoo_power_get_battery_voltage();
}
#endif
#if defined(XDUOO_X20)
int _battery_level(void)
{
return xduoo_power_get_battery_capacity();
}
#endif