[PATCH v3] Support batteries that report both energy and charge.

Benson Leung bleung at chromium.org
Thu Mar 24 12:04:19 PDT 2011


More cleanly support batteries that report both energy and charge,
but do not report power_now. One battery driver in drivers/power
(bq20z75) supports reporting energy and charge natively from the
fuel gauge, but does not report power_now. The previous legacy behavior
would ignore the existence of charge and treat current_now as
being in units of power.

Signed-off-by: Benson Leung <bleung at chromium.org>

Version history : 
v3 - Change the patch to more closely match upower style. Separate
charge to energy conversion and energy_rate decision to make it
more easy to follow.

--
diff -rupN a/src/linux/up-device-supply.c b/src/linux/up-device-supply.c
--- a/src/linux/up-device-supply.c	2011-03-24 11:02:51.322591000 -0700
+++ b/src/linux/up-device-supply.c	2011-03-24 11:17:06.232076000 -0700
@@ -554,11 +554,10 @@ up_device_supply_refresh_battery (UpDevi
 		supply->priv->unknown_retries = 0;
 	}
 
-	/* this is the new value in mWh */
+	/* this is the new value in uW */
 	energy_rate = fabs (sysfs_get_double (native_path, "power_now") / 1000000.0);
 	if (energy_rate == 0) {
-		/* get the old rate, rate; which is either in uVh or uWh */
-		energy_rate = fabs (sysfs_get_double (native_path, "current_now") / 1000000.0);
+		gdouble charge_full;
 
 		/* convert charge to energy */
 		if (energy == 0) {
@@ -566,8 +565,18 @@ up_device_supply_refresh_battery (UpDevi
 			if (energy == 0)
 				energy = sysfs_get_double (native_path, "charge_avg") / 1000000.0;
 			energy *= voltage_design;
-			energy_rate *= voltage_design;
 		}
+
+                charge_full = sysfs_get_double (native_path, "charge_full") / 1000000.0;
+                if (charge_full == 0)
+                        charge_full = sysfs_get_double (native_path, "charge_full_design") / 1000000.0;
+
+                /* If charge_full exists, then current_now is always reported in uA.
+                 * In the legacy case, where energy only units exist, and power_now isn't present
+                 * current_now is power in uW. */
+		energy_rate = fabs (sysfs_get_double (native_path, "current_now") / 1000000.0);
+		if (charge_full != 0)
+			energy_rate *= voltage_design;
 	}
 
 	/* some batteries don't update last_full attribute */


More information about the devkit-devel mailing list