Skip to content

Conversation

@yezhonghui2024
Copy link
Contributor

@yezhonghui2024 yezhonghui2024 commented Dec 12, 2025

Note: Please adhere to Contributing Guidelines.

Summary

Unify the data types of attributes in the driver framework layer and
add a new driver for dummy power levels to support power management.

Impact

Simplify driver usage and reduce data type conversions, fakeguage can
used when device is debug

Testing

1.open defconfig
CONFIG_BATTERY_FAKEGAUGE=y

  1. example code
int main(int argc, char** argv)
{
int ret;
int fd;
int temp, cap, current, vol;

fd = open("/dev/charge/fakegauge", O_RDWR);
if (fd < 0) {
	return -EINVAL;
}

ret = ioctl(fd, BATIOC_TEMPERATURE, (unsigned long)((uintptr_t)(&temp)));
if (ret < 0) {
    printf("can not get battery temperature\n");
}

ret = ioctl(fd, BATIOC_CAPACITY, (unsigned long)((uintptr_t)(&cap)));
if (ret < 0) {
    printf("can not get battery capacity\n");
}

ret = ioctl(fd, BATIOC_CURRENT, (unsigned long)((uintptr_t)(&current)));
if (ret < 0) {
    printf("can not get battery current\n");
}

ret = ioctl(fd, BATIOC_VOLTAGE, (unsigned long)((uintptr_t)(&vol)));
if (ret < 0) {
    printf("can not get battery voltage\n");
}

printf("capacity: %d, voltage: %d, current: %d, temp: %d\n", cap, vol, current, temp);
return ret;
}
  1. Test result
qemu-armv8a-ap> battery
capacity: 60, voltage: 4000, current: 100, temp: 220
qemu-armv8a-ap> battery
capacity: 60, voltage: 4042, current: 410, temp: 250
qemu-armv8a-ap> battery
capacity: 67, voltage: 4182, current: 294, temp: 250

@github-actions github-actions bot added Area: Drivers Drivers issues Size: L The size of the change in this PR is large labels Dec 12, 2025
upper and lower drivers use int data type

Signed-off-by: yezhonghui <yezhonghui@xiaomi.com>
battery drivers used unification unit

Signed-off-by: yezhonghui <yezhonghui@xiaomi.com>
Copy link
Contributor

@acassis acassis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yezhonghui2024 please add Documentation/ about this new feature

Copy link
Contributor

@linguini1 linguini1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide proper test description and results.

jerpelea
jerpelea previously approved these changes Dec 15, 2025
Copy link
Contributor

@jerpelea jerpelea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add documentation

@yezhonghui2024
Copy link
Contributor Author

@acassis Add Document in Documentation/components/drivers/special/power/battery/fakegauge.rst

@yezhonghui2024
Copy link
Contributor Author

usage:
1.open defconfig
CONFIG_BATTERY_FAKEGAUGE=y

2.user call ioctl can get value
[12/15 06:33:02] [56] [ap] [CHARGERD]charger_print_battery_info: temperature:280; capacity:99; voltage:4130;
[12/15 06:33:12] [56] [ap] [CHARGERD]charger_print_battery_info: temperature:280; capacity:99; voltage:4130;
@linguini1 usgae and used chargerd can get the value

@linguini1
Copy link
Contributor

usage:
1.open defconfig
CONFIG_BATTERY_FAKEGAUGE=y

2.user call ioctl can get value
[12/15 06:33:02] [56] [ap] [CHARGERD]charger_print_battery_info: temperature:280; capacity:99; voltage:4130;
[12/15 06:33:12] [56] [ap] [CHARGERD]charger_print_battery_info: temperature:280; capacity:99; voltage:4130;

This is great, but what ioctl call results in these values/logs? Can you put these logs and that information in your test section please? If you have some sample code that generated this output that would be good.

@jerpelea jerpelea changed the title Power/battery: battery gauge driver support power/battery: battery gauge driver support Dec 17, 2025
@yezhonghui2024
Copy link
Contributor Author

usage:
1.open defconfig
CONFIG_BATTERY_FAKEGAUGE=y
2.user call ioctl can get value
[12/15 06:33:02] [56] [ap] [CHARGERD]charger_print_battery_info: temperature:280; capacity:99; voltage:4130;
[12/15 06:33:12] [56] [ap] [CHARGERD]charger_print_battery_info: temperature:280; capacity:99; voltage:4130;

This is great, but what ioctl call results in these values/logs? Can you put these logs and that information in your test section please? If you have some sample code that generated this output that would be good.

I used framework charged, the code cannot be simply converted into a sample

@linguini1
Copy link
Contributor

I used framework charged, the code cannot be simply converted into a sample

Okay, I understand. However, more information than what's provided is needed for the testing section.

@yezhonghui2024
Copy link
Contributor Author

I used framework charged, the code cannot be simply converted into a sample

Okay, I understand. However, more information than what's provided is needed for the testing section.

we can test like this:
int main(int argc, char** argv)
{
int ret;
int fd;
int temp, cap, current, vol;

int fd = open("/dev/charge/fakegauge", O_RDRW);
if (fd < 0) {
	return -EINVAL;
}

ret = ioctl(fd, BATIOC_TEMPERATURE, (unsigned long)((uintptr_t)(&temp)));
if (ret < 0) {
    printf("can not get battery temperature\n");
}

ret = ioctl(fd, BATIOC_CAPACITY, (unsigned long)((uintptr_t)(&cap)));
if (ret < 0) {
    printf("can not get battery capacity\n");
}

ret = ioctl(fd, BATIOC_CURRENT, (unsigned long)((uintptr_t)(&current)));
if (ret < 0) {
    printf("can not get battery current\n");
}

ret = ioctl(fd, BATIOC_VOLTAGE, (unsigned long)((uintptr_t)(&vol)));
if (ret < 0) {
    printf("can not get battery voltage\n");
}

return ret;

}

@acassis acassis dismissed their stale review December 18, 2025 13:28

Doc added

realize a fake gauge to support powermanger

Signed-off-by: yezhonghui <yezhonghui@xiaomi.com>
new add a macro for charge full

Signed-off-by: yezhonghui <yezhonghui@xiaomi.com>
set time interval to control report frequency

Signed-off-by: yezhonghui <yezhonghui@xiaomi.com>
add ioctl cmd

Signed-off-by: yezhonghui <yezhonghui@xiaomi.com>
@yezhonghui2024
Copy link
Contributor Author

I used framework charged, the code cannot be simply converted into a sample

Okay, I understand. However, more information than what's provided is needed for the testing section.

we can test like this: int main(int argc, char** argv) { int ret; int fd; int temp, cap, current, vol;

int fd = open("/dev/charge/fakegauge", O_RDRW);
if (fd < 0) {
	return -EINVAL;
}

ret = ioctl(fd, BATIOC_TEMPERATURE, (unsigned long)((uintptr_t)(&temp)));
if (ret < 0) {
    printf("can not get battery temperature\n");
}

ret = ioctl(fd, BATIOC_CAPACITY, (unsigned long)((uintptr_t)(&cap)));
if (ret < 0) {
    printf("can not get battery capacity\n");
}

ret = ioctl(fd, BATIOC_CURRENT, (unsigned long)((uintptr_t)(&current)));
if (ret < 0) {
    printf("can not get battery current\n");
}

ret = ioctl(fd, BATIOC_VOLTAGE, (unsigned long)((uintptr_t)(&vol)));
if (ret < 0) {
    printf("can not get battery voltage\n");
}

return ret;

}

@linguini1 See if it's OK?

@linguini1
Copy link
Contributor

linguini1 commented Dec 19, 2025

@linguini1 See if it's OK?

Thank you for the code sample! Can you please put this and the other testing information from your comments under the "testing" section in your PR description? It looks good to me.

@xiaoxiang781216
Copy link
Contributor

@linguini1 See if it's OK?

Thank you for the code sample! Can you please put this and the other testing information from your comments under the "testing" section in your PR description? It looks good to me.

@linguini1 here is tool let you interactive with battery driver:
https://github.com/apache/nuttx-apps/tree/master/system/batterydump

@linguini1 linguini1 dismissed their stale review December 20, 2025 03:43

Test information provided

@yezhonghui2024
Copy link
Contributor Author

@linguini1 Now I add test Result in "test" Section under qemu

@linguini1
Copy link
Contributor

@linguini1 Now I add test Result in "test" Section under qemu

Looks good! I just modified your description to put the code inside code blocks (with markdown it's three backtick ` characters around the code)

@xiaoxiang781216
Copy link
Contributor

@linguini1 Now I add test Result in "test" Section under qemu

Looks good! I just modified your description to put the code inside code blocks (with markdown it's three backtick ` characters around the code)

@linguini1 could you merge the pr?

@acassis acassis merged commit 8a91155 into apache:master Dec 22, 2025
82 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Documentation Improvements or additions to documentation Area: Drivers Drivers issues Size: L The size of the change in this PR is large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants