FORUMS
Remove All Ads from XDA

[KERNEL] [PATCH] 'Extended Battery' Charge/Charging indicator fix [EBFix]

5,530 posts
Thanks Meter: 2,296
 
By Droidzone, Inactive Recognized Developer on 5th November 2011, 08:15 PM
Post Reply Email Thread
Aside:
Thanks to everyone who participated in our online campaign to get the kernel source for Desire, within 6 days, we got the HTC Desire Gingerbread kernel source.

Now to business,

This is a kernel source patch/fix for a problem that had been plaguing me for a long, long time. I use an extended battery for the Desire, and the issue was that for almost any kernel (except an old Neophyte 1000 MV bravo kernel), after every reboot/reboot to recovery, the battery charge status would become disabled (showing a grey rectangle, instead of the usual green/orange/red indicator. At the same time, the led would keep flashing alternate green and orange, and it would not get charged at all when connected to host PC/mains. See the attached pic.



Notice the battery icon the top right?

The next pic shows the charge status after the Kernel code is patched. It now shows normal charge status, no warning LEDs, and charges normally.



If you have a similiar issue while using your battery, and you are using a GB Sense ROM, you can flash the attached zip file.

If you're a kernel dev and wish to add this to your Rom, you can download the patch.

I dont think too many people have this issue/use extended batteries. But for those who do, this is very annoying, since you can never reboot your phone, and have to fix the charge status by shutting down, pulling the battery, reinserting it, and booting. And of course the issue would pop up again on the very next boot!

If you're using this in your kernel, try to use the tag [EBFix]

Dev Notes:
File: bravo_2.6.35_gb-mr/drivers/power/ds2784_battery.c

Simply put, it bypasses an IF block in the kernel source which checks whether it is an officially supported battery. If it isnt, then it toggles an indicator called htc_batt_info.rep.batt_id to UNKNOWN. Additional functions then disable charging, and flash warning lights.


Code:
--- ds2784_battery.c.bak    2011-11-05 21:32:15.000000000 +0530
+++ ds2784_battery.c    2011-11-06 01:41:37.052661823 +0530
@@ -484,10 +484,14 @@ ONLY check when battery driver init.
 /*
 Get Rsns, get from offset 69H . Rsnsp=1/Rsns
 Judge if this is supported battery
+Extended Battery Charge Status fix by Droidzone
 */
     mutex_lock(&htc_batt_info.lock);
-    if (di->raw[DS2784_REG_RSNSP] != BATT_RSNSP)
-        htc_batt_info.rep.batt_id = BATT_UNKNOWN;
+    if (di->raw[DS2784_REG_RSNSP] != BATT_RSNSP) {
+          pr_info("batt: Extended unsupported battery detected\n");
+          pr_info("batt: Re-enabling charge and status indicator - Droidzone\n");
+          pr_info("batt: Battery check overridden\n");
+    }
     else
         htc_batt_info.rep.batt_id = BATT_FIRST_SOURCE;
     mutex_unlock(&htc_batt_info.lock);
The attached kernel has the following characteristics:
  • Non UV, Non OC Based on Official source
  • Sibere's Battfix
  • TP Wake (snq-)
  • Wifi threshold fix
  • Two way call record
  • Linux version upstream upgrade to 2.6.35.14
Attached Files
File Type: txt ds2784_battery_c_patch.txt - [Click for QR Code] (733 Bytes, 379 views)
File Type: zip Kernel_ExtendedBattery_fix.zip - [Click for QR Code] (2.11 MB, 928 views)
The Following 6 Users Say Thank You to Droidzone For This Useful Post: [ View ] Gift Droidzone Ad-Free
5th November 2011, 08:24 PM |#2  
Senior Member
Thanks Meter: 18
 
More
some pics not loading
The Following User Says Thank You to leecavturbo For This Useful Post: [ View ] Gift leecavturbo Ad-Free
5th November 2011, 08:26 PM |#3  
Droidzone's Avatar
OP Inactive Recognized Developer
Flag Kochi
Thanks Meter: 2,296
 
Donate to Me
More
Pics are hosted on my site/server. It's being restarted. So pics will come back within 10 mins.
6th November 2011, 04:47 AM |#4  
Droidzone's Avatar
OP Inactive Recognized Developer
Flag Kochi
Thanks Meter: 2,296
 
Donate to Me
More
Full Battery Charge for Extended Battery - Cameron Sino 2400 mAh
With this kernel code, I have enabled full charge characteristics for the Cameron Sino Extended battery 2400 mAh

The stock battery is 1400/1500 Mah, the full battery level polled is a max of 1500 only due to HTC Code limitation. The attached kernel (to upload later at request) makes it test whether the battery is an extended Cameron Sino or not. If it is, then the max MAh level is upped to 2400 mAh.. I have added additional debug lines to output some useful battery stats.

Code:

Code:
--- /home/droidzone/android/kernel/power_ds2784_patches/ds2784_battery.c.bak	2011-11-05 21:32:15.000000000 +0530
+++ /home/droidzone/android/kernel/bravo_2.6.35_gb-mr/drivers/power/ds2784_battery.c	2011-11-06 09:14:01.255605163 +0530
@@ -118,8 +118,11 @@ PS. 0 or other battery ID use the same p
 /* Battery ID = 1: HT-E/Formosa 1400mAh */
 #define BATT_ID_A				1
 #define BATT_FULL_MAH_A			1400
-
 #define BATT_FULL_MAH_DEFAULT	1500
+#define BATT_FULL_MAH_CAMERONSINO	2400
+#define BATT_ID_CAMERONSINO
+#define BATT_TYPE 0
+
 
 /* Battery OVP and Charging stage define */
 #define BATT_GOOD_STATE			0
@@ -145,6 +148,7 @@ PS. 0 or other battery ID use the same p
 //#endif
 static struct ds2784_device_info htc_batt_info;
 static int htc_battery_initial;
+static int Batt_extended;
 static int Full_40;
 static int Full_30;
 static int Full_20;
@@ -213,7 +217,7 @@ static int ds2784_blocking_notify(unsign
 			htc_batt_info.rep.charging_enabled = DISABLE;
 			chg_ctl = DISABLE;
 			v = DISABLE;
-			pr_info("[HTC_BATT] Unknow battery\n");
+			pr_info("[HTC_BATT] Unknown battery\n");
 		}
 
 		if (machine_is_passionc() || machine_is_bravoc()) {
@@ -285,8 +289,10 @@ ssize_t htc_battery_show_attr(struct dev
 		htc_batt_info.rep.full_level,
 		fake_temp
 		);
+	pr_info("[HTC_BATT] Battery ID is %d\n", htc_batt_info.rep.batt_id);
 	}
 	return len;
+	
 }
 
 static int cable_status_handler_func(struct notifier_block *nfb,
@@ -388,8 +394,25 @@ static int Calculate_Full_mAh(struct ds2
 	int Upper_Full = 0;
 	int Lower_Full = 0;
 	int Delta_temp = 0;
-
-	Full_mAh = BATT_FULL_MAH_DEFAULT;
+	
+	if (Batt_extended == 1) {
+	      Full_mAh = BATT_FULL_MAH_CAMERONSINO;
+	      pr_info("[HTC_BATT] Extended Battery detected and Charge Characteristics set\n");
+	      pr_info("[HTC_BATT] Max Battery mAh is now set as %d\n", Full_mAh);
+	} else {	      
+	      Full_mAh = BATT_FULL_MAH_DEFAULT;
+	      pr_info("[HTC_BATT] Standard HTC Battery detected and Charge Characteristics set\n");
+	      pr_info("[HTC_BATT] Max Battery mAh is now set as %d\n", Full_mAh);
+	}
+	
+	
+	
+	/*  BATT_FULL_MAH_CAMERONSINO 
+	 Droidzone's Hack
+	 Full_mAh = BATT_FULL_MAH_CAMERONSINO;
+	 Batt_extended
+	 
+	 */
 
 /* Only calculate at initial */
 	if (htc_battery_initial == 0) {
@@ -484,12 +507,20 @@ ONLY check when battery driver init.
 /*
 Get Rsns, get from offset 69H . Rsnsp=1/Rsns
 Judge if this is supported battery
+Extended Battery Charge Status fix by Droidzone
 */
 	mutex_lock(&htc_batt_info.lock);
-	if (di->raw[DS2784_REG_RSNSP] != BATT_RSNSP)
-		htc_batt_info.rep.batt_id = BATT_UNKNOWN;
-	else
-		htc_batt_info.rep.batt_id = BATT_FIRST_SOURCE;
+	if (di->raw[DS2784_REG_RSNSP] != BATT_RSNSP) {
+	      pr_info("batt: Extended unsupported battery detected\n");
+	      pr_info("batt: Re-enabling charge and status indicator - Droidzone\n");
+	      pr_info("batt: Battery check overridden\n");
+	      pr_info("batt: Dirty hack : Battery set as Cameron Sino\n");
+	      Batt_extended = 1;     	      
+	} else {
+	      Batt_extended = 0;
+	}
+	
+	htc_batt_info.rep.batt_id = BATT_FIRST_SOURCE;
 	mutex_unlock(&htc_batt_info.lock);
 
 /*
@@ -548,6 +579,7 @@ Get ACR and Active Empty
 /*
 Set to local for reply to framework
 */
+	pr_info("[HTC_BATT] Battery Debug 1 - Calculating Battery Stats\n");
 	htc_batt_info.rep.batt_current = di->current_mA;
 	htc_batt_info.rep.batt_current_avg = di->current_avg_mA;
 	htc_batt_info.rep.batt_temp = di->temp_C;
@@ -558,6 +590,8 @@ Set to local for reply to framework
 	htc_batt_info.rep.acr = di->acr;
 	htc_batt_info.rep.active_empty = di->active_empty;
 	htc_batt_info.rep.full_bat = Calculate_Full_mAh(di);
+	pr_info("[HTC_BATT] Battery Debug 2 - Full mAh value is %d\n", htc_batt_info.rep.full_bat);
+	/*pr_info("Battery Debug 1 - Calculating Battery Stats\n"*/
 
 
 	/* After battery driver gets initialized, send rpc request to inquiry
@@ -575,6 +609,7 @@ pr_info("[HTC_BATT]RSNSP=%d,RARC=%d,Vol=
 		,di->voltage_mV
 		,di->current_mA
 		,di->temp_C);
+pr_info("[HTC_BATT] Battery reported Full Value is %d mAh\n", htc_batt_info.rep.full_bat);
 	mutex_unlock(&htc_batt_info.lock);
 	return 0;
 }
@@ -871,6 +906,9 @@ if (machine_is_bravoc()) {
 	* user while we're maintaining a full charge
 	* (slowly draining to 95 and charging back
 	* to 100)
+	* 
+	* Modified by Sibere Battfix
+	* 
 	 */
 	if (htc_batt_info.rep.charging_source != 0) {
 		if (htc_batt_info.rep.level <= 99)
11th December 2011, 09:48 AM |#5  
Senior Member
Thanks Meter: 197
 
More
Quote:
Originally Posted by Droidzone

With this kernel code, I have enabled full charge characteristics for the Cameron Sino Extended battery 2400 mAh

The stock battery is 1400/1500 Mah, the full battery level polled is a max of 1500 only due to HTC Code limitation. The attached kernel (to upload later at request) makes it test whether the battery is an extended Cameron Sino or not. If it is, then the max MAh level is upped to 2400 mAh.. I have added additional debug lines to output some useful battery stats.

Code:

Code:
--- /home/droidzone/android/kernel/power_ds2784_patches/ds2784_battery.c.bak	2011-11-05 21:32:15.000000000 +0530
+++ /home/droidzone/android/kernel/bravo_2.6.35_gb-mr/drivers/power/ds2784_battery.c	2011-11-06 09:14:01.255605163 +0530
@@ -118,8 +118,11 @@ PS. 0 or other battery ID use the same p
 /* Battery ID = 1: HT-E/Formosa 1400mAh */
 #define BATT_ID_A				1
 #define BATT_FULL_MAH_A			1400
-
 #define BATT_FULL_MAH_DEFAULT	1500
+#define BATT_FULL_MAH_CAMERONSINO	2400
+#define BATT_ID_CAMERONSINO
+#define BATT_TYPE 0
+
 
 /* Battery OVP and Charging stage define */
 #define BATT_GOOD_STATE			0
@@ -145,6 +148,7 @@ PS. 0 or other battery ID use the same p
 //#endif
 static struct ds2784_device_info htc_batt_info;
 static int htc_battery_initial;
+static int Batt_extended;
 static int Full_40;
 static int Full_30;
 static int Full_20;
@@ -213,7 +217,7 @@ static int ds2784_blocking_notify(unsign
 			htc_batt_info.rep.charging_enabled = DISABLE;
 			chg_ctl = DISABLE;
 			v = DISABLE;
-			pr_info("[HTC_BATT] Unknow battery\n");
+			pr_info("[HTC_BATT] Unknown battery\n");
 		}
 
 		if (machine_is_passionc() || machine_is_bravoc()) {
@@ -285,8 +289,10 @@ ssize_t htc_battery_show_attr(struct dev
 		htc_batt_info.rep.full_level,
 		fake_temp
 		);
+	pr_info("[HTC_BATT] Battery ID is %d\n", htc_batt_info.rep.batt_id);
 	}
 	return len;
+	
 }
 
 static int cable_status_handler_func(struct notifier_block *nfb,
@@ -388,8 +394,25 @@ static int Calculate_Full_mAh(struct ds2
 	int Upper_Full = 0;
 	int Lower_Full = 0;
 	int Delta_temp = 0;
-
-	Full_mAh = BATT_FULL_MAH_DEFAULT;
+	
+	if (Batt_extended == 1) {
+	      Full_mAh = BATT_FULL_MAH_CAMERONSINO;
+	      pr_info("[HTC_BATT] Extended Battery detected and Charge Characteristics set\n");
+	      pr_info("[HTC_BATT] Max Battery mAh is now set as %d\n", Full_mAh);
+	} else {	      
+	      Full_mAh = BATT_FULL_MAH_DEFAULT;
+	      pr_info("[HTC_BATT] Standard HTC Battery detected and Charge Characteristics set\n");
+	      pr_info("[HTC_BATT] Max Battery mAh is now set as %d\n", Full_mAh);
+	}
+	
+	
+	
+	/*  BATT_FULL_MAH_CAMERONSINO 
+	 Droidzone's Hack
+	 Full_mAh = BATT_FULL_MAH_CAMERONSINO;
+	 Batt_extended
+	 
+	 */
 
 /* Only calculate at initial */
 	if (htc_battery_initial == 0) {
@@ -484,12 +507,20 @@ ONLY check when battery driver init.
 /*
 Get Rsns, get from offset 69H . Rsnsp=1/Rsns
 Judge if this is supported battery
+Extended Battery Charge Status fix by Droidzone
 */
 	mutex_lock(&htc_batt_info.lock);
-	if (di->raw[DS2784_REG_RSNSP] != BATT_RSNSP)
-		htc_batt_info.rep.batt_id = BATT_UNKNOWN;
-	else
-		htc_batt_info.rep.batt_id = BATT_FIRST_SOURCE;
+	if (di->raw[DS2784_REG_RSNSP] != BATT_RSNSP) {
+	      pr_info("batt: Extended unsupported battery detected\n");
+	      pr_info("batt: Re-enabling charge and status indicator - Droidzone\n");
+	      pr_info("batt: Battery check overridden\n");
+	      pr_info("batt: Dirty hack : Battery set as Cameron Sino\n");
+	      Batt_extended = 1;     	      
+	} else {
+	      Batt_extended = 0;
+	}
+	
+	htc_batt_info.rep.batt_id = BATT_FIRST_SOURCE;
 	mutex_unlock(&htc_batt_info.lock);
 
 /*
@@ -548,6 +579,7 @@ Get ACR and Active Empty
 /*
 Set to local for reply to framework
 */
+	pr_info("[HTC_BATT] Battery Debug 1 - Calculating Battery Stats\n");
 	htc_batt_info.rep.batt_current = di->current_mA;
 	htc_batt_info.rep.batt_current_avg = di->current_avg_mA;
 	htc_batt_info.rep.batt_temp = di->temp_C;
@@ -558,6 +590,8 @@ Set to local for reply to framework
 	htc_batt_info.rep.acr = di->acr;
 	htc_batt_info.rep.active_empty = di->active_empty;
 	htc_batt_info.rep.full_bat = Calculate_Full_mAh(di);
+	pr_info("[HTC_BATT] Battery Debug 2 - Full mAh value is %d\n", htc_batt_info.rep.full_bat);
+	/*pr_info("Battery Debug 1 - Calculating Battery Stats\n"*/
 
 
 	/* After battery driver gets initialized, send rpc request to inquiry
@@ -575,6 +609,7 @@ pr_info("[HTC_BATT]RSNSP=%d,RARC=%d,Vol=
 		,di->voltage_mV
 		,di->current_mA
 		,di->temp_C);
+pr_info("[HTC_BATT] Battery reported Full Value is %d mAh\n", htc_batt_info.rep.full_bat);
 	mutex_unlock(&htc_batt_info.lock);
 	return 0;
 }
@@ -871,6 +906,9 @@ if (machine_is_bravoc()) {
 	* user while we're maintaining a full charge
 	* (slowly draining to 95 and charging back
 	* to 100)
+	* 
+	* Modified by Sibere Battfix
+	* 
 	 */
 	if (htc_batt_info.rep.charging_source != 0) {
 		if (htc_batt_info.rep.level <= 99)


Hi i was wondering if this same coding could work on a dell streak.....i have a mugen 4800 mah battery and it never shows proper charge...it shows the charge for the stock 1500 mah battery and then runs down and stays at 1% till the battery finally dies...so out of the 48 hrs tht i get out of it for the initial 12 to 15 hrs show the proper charge level but after tht it stays on 1% Unless i pull the battery out.
11th December 2011, 02:29 PM |#6  
Droidzone's Avatar
OP Inactive Recognized Developer
Flag Kochi
Thanks Meter: 2,296
 
Donate to Me
More
Quote:
Originally Posted by Dude_Virus

Hi i was wondering if this same coding could work on a dell streak.....i have a mugen 4800 mah battery and it never shows proper charge...it shows the charge for the stock 1500 mah battery and then runs down and stays at 1% till the battery finally dies...so out of the 48 hrs tht i get out of it for the initial 12 to 15 hrs show the proper charge level but after tht it stays on 1% Unless i pull the battery out.

You could certainly code something similiar. I dont know what kind of battery source code Dell uses, but you'll have to look into the specific code of the battery driver. Your problem is similiar to mine. The patch cannoy be directly applied on your device though.
11th December 2011, 08:58 PM |#7  
Senior Member
Thanks Meter: 197
 
More
Quote:
Originally Posted by Droidzone

You could certainly code something similiar. I dont know what kind of battery source code Dell uses, but you'll have to look into the specific code of the battery driver. Your problem is similiar to mine. The patch cannoy be directly applied on your device though.

Thanks .. have one question though if we enable all the drivers in the kernel will this issue get solved or we need to modify the codes in one particular driver which is being used for the battery.
11th December 2011, 09:39 PM |#8  
wings400's Avatar
Senior Member
Flag With Alice in Wonderland
Thanks Meter: 8
 
More
droidzone you are awesome!

ps: why do some people need to quote the whole miles long article when replying? Sssssstop it!
12th December 2011, 04:21 AM |#9  
Droidzone's Avatar
OP Inactive Recognized Developer
Flag Kochi
Thanks Meter: 2,296
 
Donate to Me
More
Quote:
Originally Posted by Dude_Virus

Thanks .. have one question though if we enable all the drivers in the kernel will this issue get solved or we need to modify the codes in one particular driver which is being used for the battery.

You need to modify the code.
10th March 2012, 06:26 PM |#10  
Senior Member
Thanks Meter: 254
 
More
Droidzone, I noticed something with your patch:

Due to DS2784 chip uses extensively the Rsnsp number (sense resistor prime: the value of "1/sense resistor" in mOhms), and since everywhere in the code it's hardcoded as "67"; maybe you should add something for that as well, huh? You can find the value of this sensor chip in "DS2784_REG_RSNSP" location (di->raw[DS2784_REG_RSNSP] can obtain this value).

After retrieval of this value (and keep it in some int like Rsnsp), you can change the "67"s where you can see in calculation of mAh, Raac, etc. and others with Rsnsp. This would make the driver estimate the battery's capacity better - rather than trying to emulate its usage as a HTC Stock battery.

EDIT: Posted that idea in my BATT-Fix thread. Thanks for the idea, buddy!
The Following 4 Users Say Thank You to theGanymedes For This Useful Post: [ View ] Gift theGanymedes Ad-Free
10th March 2012, 06:40 PM |#11  
Droidzone's Avatar
OP Inactive Recognized Developer
Flag Kochi
Thanks Meter: 2,296
 
Donate to Me
More
Definitely worth a try. Thanks.
Post Reply Subscribe to Thread

Tags
charge indicator, extended battery fix, kernel, patch

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes