need help regarding battery kernel driver

Moderators: mdrjr, odroid

need help regarding battery kernel driver

Unread postby Danie » Fri May 04, 2018 11:10 pm

Hello

i am trying to write a battery driver which reports battery related information to the android os.

I started by studing the odroid-battery.c driver from:
https://github.com/hardkernel/linux/blob/odroidc2-3.14.y-android-m/drivers/hardkernel/odroid-battery.c
First i manipulate the hardcoded values for soc and so on and check with some battery info apps, that values are reported correctly to the android OS.

The output of the grep battery device:
Code: Select all
adb shell grep '' /sys/class/power_supply/battery/*
/sys/class/power_supply/battery/capacity:77
grep: /sys/class/power_supply/battery/device: Is a directory
/sys/class/power_supply/battery/health:Overheat
grep: /sys/class/power_supply/battery/power: Is a directory
/sys/class/power_supply/battery/present:0
/sys/class/power_supply/battery/status:Discharging
grep: /sys/class/power_supply/battery/subsystem: Is a directory
/sys/class/power_supply/battery/technology:Li-poly
/sys/class/power_supply/battery/temp:100
/sys/class/power_supply/battery/type:Battery
/sys/class/power_supply/battery/uevent:POWER_SUPPLY_NAME=battery
/sys/class/power_supply/battery/uevent:POWER_SUPPLY_STATUS=Discharging
/sys/class/power_supply/battery/uevent:POWER_SUPPLY_HEALTH=Overheat
/sys/class/power_supply/battery/uevent:POWER_SUPPLY_PRESENT=0
/sys/class/power_supply/battery/uevent:POWER_SUPPLY_TECHNOLOGY=Li-poly
/sys/class/power_supply/battery/uevent:POWER_SUPPLY_VOLTAGE_NOW=5000
/sys/class/power_supply/battery/uevent:POWER_SUPPLY_TEMP=100
/sys/class/power_supply/battery/uevent:POWER_SUPPLY_CAPACITY=77
/sys/class/power_supply/battery/voltage_now:5000


This seems to work fine. (Except that i found out, that voltage is reported in µV - so if 5V should be reported, VOLTAGE_NOW have to return 5000000 instead of 5, which only means 5µV)


After that i looked at some I2C drivers like the max17040:
https://github.com/hardkernel/linux/blob/odroidc2-3.14.y-android-m/drivers/power/max17040_battery.c
I just modiefied it to provide hardcoded values like the fake battery driver does and comment all I2C related communication stuff out.
See: https://github.com/DanieGieb/Odroid-Battery-Gauge/blob/master/max17040_test.c
So at the moment there is no I2C data transfer (except for the rtc)

The entry in my meson64_odroidc2.dts looks as follows:
Code: Select all
&i2c_a {
   status = "okay";

   /* Hardkernel I2C RTC */
   pcf8563: pcf8563@51   {
      compatible = "nxp,pcf8563";
      status = "okay";
      reg = <0x51>;
      #clock-cells = <0>;
   };
   
   /* I2C Battery Gauge */
   max17040: max17040@30   {
      compatible = "max17040";
      status = "okay";
      reg = <0x30>;
   };
};



When i load my driver the greb output of the battery device looks fine (i think):
Code: Select all
adb shell grep '' /sys/class/power_supply/battery/*
/sys/class/power_supply/battery/capacity:77
grep: /sys/class/power_supply/battery/device: Is a directory
/sys/class/power_supply/battery/online:0
grep: /sys/class/power_supply/battery/power: Is a directory
/sys/class/power_supply/battery/status:Discharging
grep: /sys/class/power_supply/battery/subsystem: Is a directory
/sys/class/power_supply/battery/type:Battery
/sys/class/power_supply/battery/uevent:POWER_SUPPLY_NAME=battery
/sys/class/power_supply/battery/uevent:POWER_SUPPLY_STATUS=Discharging
/sys/class/power_supply/battery/uevent:POWER_SUPPLY_ONLINE=0
/sys/class/power_supply/battery/uevent:POWER_SUPPLY_VOLTAGE_NOW=5000
/sys/class/power_supply/battery/uevent:POWER_SUPPLY_CAPACITY=77
/sys/class/power_supply/battery/voltage_now:5000


With my driver loaded i get wrong values from the battery Info app!
The only correct value are the voltage which is reported as 5µV (same for the fake odroid-battery driver) and the status (in this case Discharging)
soc stuck always at 100%, Temperature at 42.2C degrees...

What is the cause for this behavior?
Is this because i do not have a physical device attached, which is needed by the I2C core driver?

Hope someone can help me!

Kind Regards
Danie
Danie
 
Posts: 58
Joined: Thu Jan 14, 2016 8:19 am
languages_spoken: english, german
ODROIDs: C1+

Re: need help regarding battery kernel driver

Unread postby Danie » Tue May 08, 2018 4:22 am

I did some further testing.
I am now quite sure, that this is a bug within the kernel and/or the android Sourcecode itself!

I modified my driver to simply increment the values within the working thread:
Code: Select all
static void max17040_work(struct work_struct *work)
{   
   struct max17040_chip *chip;
   chip = container_of(work, struct max17040_chip, work.work);

   printk("max17040_work !!!!!!!!!!!!!!!!!!!!!!!!!\n");
   
   //increment battery soc
   if (chip->soc == 100) {
      chip->soc = 0;
   }
   else {
      chip->soc = chip->soc +1;
    }

   //increment battery voltage (offset = 1V)
   if (chip->vcell == 5500000) {
      chip->vcell = 1000000;
   }
   else {
      chip->vcell = chip->vcell +5000;
    }
   
   //increment battery temp
   if (chip->temp == 1000) {
      chip->temp = 0;
   }
   else {
      chip->temp = chip->temp +1;
    }
   
   //toogle battery online
   if(chip->online == 0) {
      chip->online =1;
   }
   else {
      chip->online =0;
   }

   //change battery status
   if (chip->status == 5) {
      chip->status = 0;
   }
   else {
      chip->status = chip->status +1;
   }
   
//   max17040_get_vcell(chip->client);
//   max17040_get_soc(chip->client);
//   max17040_get_online(chip->client);
//   max17040_get_status(chip->client);

   power_supply_changed(&chip->battery);
   schedule_delayed_work(&chip->work, MAX17040_DELAY);
}

I also implemented the call "power_supply_changed();", which should send the values with udev to android userspace...
Battery voltage und status are changing/increment each second, but temperature and soc still stuck on fixed values. (soc stuck on 100 and temperature on 42.2c)

It seems that these values are only reported to android while booting.
Due to the fact, that my driver implement I2C, the driver is loaded at a different time (right after the I2C core driver is ready and called the max17040_probe() function).
But why these values for soc = 100, and for the temperature = 42.2 is still very confusing for me.

I think udev is not sending the values to android and the values which are getting updated, are directly read from the depending sys-file!?

Do you have any plan, where to start searching for this bug?

Kind Regards
Danie
Danie
 
Posts: 58
Joined: Thu Jan 14, 2016 8:19 am
languages_spoken: english, german
ODROIDs: C1+

Re: need help regarding battery kernel driver

Unread postby odroid » Tue May 08, 2018 11:10 am

We have no idea how Android battery framework reads the level with the battery gauge driver.
User avatar
odroid
Site Admin
 
Posts: 28490
Joined: Fri Feb 22, 2013 11:14 pm
languages_spoken: English
ODROIDs: ODROID

Re: need help regarding battery kernel driver

Unread postby Danie » Sun May 13, 2018 11:07 pm

All the magic happens in the "BatteryService.java" located at "frameworks/base/services/core/java/com/android/server"

After studying the code i am quite sure that this issue is related to the update function, which is not called on runtime.
On boot the function is called once, so the values are getting updated one time.
But after that, the function is not called, so the values for soc and temperature are not updated any more.

I downloaded the Android Source code an implement some debugging-stuff, to verify that.

I successfully build the image.
This was the first time i build an android image for an odroid.
At the moment i am not sure, which file i need to push on my sd card for self-install process.

On the c2 wiki is mentioned that this file would be "out/target/product/odroidc2/selfinstall-odroidc2.bin"
But it should be an .img, file, I think?
Danie
 
Posts: 58
Joined: Thu Jan 14, 2016 8:19 am
languages_spoken: english, german
ODROIDs: C1+

Re: need help regarding battery kernel driver

Unread postby Danie » Thu Jun 14, 2018 7:30 am

Hello,

can someone please point me in the right direction or give any tipp?
I think this would be a great feature! i already designed a UPS, which uses a battery charger IC with integrated coulomb counter.
It would be very nice, if android can read the real battery SOC...

Kind Regards
Danie
 
Posts: 58
Joined: Thu Jan 14, 2016 8:19 am
languages_spoken: english, german
ODROIDs: C1+


Return to Android

Who is online

Users browsing this forum: No registered users and 4 guests