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

Benson Leung bleung at chromium.org
Thu Mar 24 02:49:21 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>

--
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-23 14:39:54.079130000 -0700
+++ b/src/linux/up-device-supply.c	2011-03-23 18:18:39.032724000 -0700
@@ -557,16 +557,32 @@ up_device_supply_refresh_battery (UpDevi
 	/* 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);
-
-		/* convert charge to energy */
-		if (energy == 0) {
-			energy = sysfs_get_double (native_path, "charge_now") / 1000000.0;
-			if (energy == 0)
-				energy = sysfs_get_double (native_path, "charge_avg") / 1000000.0;
-			energy *= voltage_design;
-			energy_rate *= voltage_design;
+		gdouble current;
+		gdouble charge;
+		/* Depending on type of system, current_now may report as power (uW).
+		 * Determine below. */
+		current = fabs (sysfs_get_double (native_path, "current_now") / 1000000.0);
+
+		charge = sysfs_get_double (native_path, "charge_now") / 1000000.0;
+		if (charge == 0)
+			charge = sysfs_get_double (native_path, "charge_avg") / 1000000.0;
+
+		if (energy == 0 && charge != 0) {
+			/* convert charge to energy */
+			energy = charge * voltage_design;
+			energy_rate = current * voltage_design;
+		} else if (energy != 0 && charge == 0) {
+			/* energy values only. "current_now" is actually power in uW */
+			energy_rate = current;
+		} else if (energy != 0 && charge != 0) {
+			/* both exist. Use reported energy for energy,
+			 * and calculate energy_rate from current */
+			energy_rate = current * voltage_design;
+		} else {
+			/* neither energy nor charge found. */
+			g_warning ("neither energy nor charge found.");
+			ret = FALSE;
+			goto out;
 		}
 	}
 


More information about the devkit-devel mailing list