How can I obtain battery level inside a Linux kernel module?

梦想与她 提交于 2019-11-30 11:50:15

I have found a solution that works for me. First of all make sure to #include < linux/power_supply.h >

Assuming you know the name of the battery, this code gives an example of how to get current battery capacity.

char name[]= "BAT0";
int result = 0;
struct power_supply *psy = power_supply_get_by_name(name);
union power_supply_propval chargenow, chargefull;
result = psy->get_property(psy,POWER_SUPPLY_PROP_CHARGE_NOW,&chargenow);
if(!result) {
    printk(KERN_INFO "The charge level is %d\n",chargenow.intval);
}
result = psy->get_property(psy,POWER_SUPPLY_PROP_CHARGE_FULL,&chargefull);
if(!result) {
    printk(KERN_INFO "The charge level is %d\n",chargefull.intval);
}

Looking at the battery.c, sbs.c, I think you can call the interface API (acpi_battery_read, acpi_battery_get_state) in your LKM directly.
Did you try that so far?

I have the same dilemma! :-\ If this is a hardware specific thing you'r doing, you could see if on your particular laptop you can detect the smart battery on the SMBus link or not. If you can, then you can just do i2c/SMBus calls from within your LKM. Most new systems (except some Fujitsu laptops) talk to an embedded controller instead, which ends up configuring the battery (through SMBus i suppose).... Try installing "lm-sensors" and see if it detects your smart battery. If so you should be able to talk directly to the battery (usually at i2c address 0xb).

If this isn't a hardware specific thing you're doing, then ignore what i said :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!