Linux IIO development
 help / color / mirror / Atom feed
From: "Molnár Máté via B4 Relay" <devnull+matemolnar88.gmail.com@kernel.org>
To: "Jonathan Cameron" <jic23@kernel.org>,
	"David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	"Rob Herring" <robh@kernel.org>,
	"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
	"Conor Dooley" <conor+dt@kernel.org>,
	"Chen-Yu Tsai" <wens@kernel.org>,
	"Sebastian Reichel" <sre@kernel.org>,
	"Liam Girdwood" <lgirdwood@gmail.com>,
	"Mark Brown" <broonie@kernel.org>,
	"Dmitry Torokhov" <dmitry.torokhov@gmail.com>,
	"Lee Jones" <lee@kernel.org>
Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org,
	linux-input@vger.kernel.org, mfd@lists.linux.dev,
	"Aidan MacDonald" <aidanmacdonald.0x0@gmail.com>,
	"Alex Bee" <knaerzche@gmail.com>,
	"Molnár Máté" <matemolnar88@gmail.com>
Subject: [PATCH v7 7/9] power: axp20x_battery: Estimate AXP192 capacity from the OCV table
Date: Sun, 06 Dec 2026 04:08:59 +0100	[thread overview]
Message-ID: <20261206-axp192-v7-7-0200fc77da53@gmail.com> (raw)
In-Reply-To: <20261206-axp192-v7-0-0200fc77da53@gmail.com>

From: Molnár Máté <matemolnar88@gmail.com>

The AXP192 has no fuel gauge, so the driver does not report a capacity
at all. Userspace then has no idea how full the battery is: UPower, for
example, reads 0% and powers the device off as soon as the battery
reports discharging.

Use the OCV table of the monitored battery, when one is given, to
estimate the capacity from the battery voltage. The voltage is corrected
for the drop across the battery's internal resistance when the battery
info has it. Keep the battery info around for this on PMICs without a
fuel gauge instead of releasing it after probe.

Signed-off-by: Molnár Máté <matemolnar88@gmail.com>
---
 drivers/power/supply/axp20x_battery.c | 58 ++++++++++++++++++++++++++++++++++-
 1 file changed, 57 insertions(+), 1 deletion(-)

diff --git a/drivers/power/supply/axp20x_battery.c b/drivers/power/supply/axp20x_battery.c
index b55089a81..3c3cf349f 100644
--- a/drivers/power/supply/axp20x_battery.c
+++ b/drivers/power/supply/axp20x_battery.c
@@ -21,6 +21,7 @@
 #include <linux/err.h>
 #include <linux/interrupt.h>
 #include <linux/irq.h>
+#include <linux/math64.h>
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/platform_device.h>
@@ -128,6 +129,8 @@ struct axp20x_batt_ps {
 	/* Maximum constant charge current */
 	unsigned int max_ccc;
 	const struct axp_data	*data;
+	/* Battery info kept for the OCV capacity estimate on PMICs without FG */
+	struct power_supply_battery_info *bat_info;
 	bool ts_disable;
 };
 
@@ -285,6 +288,50 @@ static int axp717_get_constant_charge_current(struct axp20x_batt_ps *axp,
 	return 0;
 }
 
+static int axp20x_battery_get_prop(struct power_supply *psy,
+				   enum power_supply_property psp,
+				   union power_supply_propval *val);
+
+/*
+ * Estimate the capacity from the open circuit voltage for PMICs without a
+ * fuel gauge, using the OCV table of the monitored battery. The OCV is
+ * approximated from the battery voltage and current with the battery's
+ * internal resistance, when it is known.
+ */
+static int axp20x_battery_ocv_capacity(struct axp20x_batt_ps *axp20x_batt,
+				       int *val)
+{
+	struct power_supply_battery_info *info = axp20x_batt->bat_info;
+	union power_supply_propval v, i;
+	int ret;
+
+	if (!info || !info->ocv_table[0])
+		return -ENODATA;
+
+	ret = axp20x_battery_get_prop(axp20x_batt->batt,
+				      POWER_SUPPLY_PROP_VOLTAGE_NOW, &v);
+	if (ret)
+		return ret;
+
+	ret = axp20x_battery_get_prop(axp20x_batt->batt,
+				      POWER_SUPPLY_PROP_CURRENT_NOW, &i);
+	if (ret)
+		return ret;
+
+	if (info->factory_internal_resistance_uohm > 0)
+		v.intval -= div_s64((s64)i.intval *
+				    info->factory_internal_resistance_uohm,
+				    1000000);
+
+	/* ponytail: no temperature sensor wired up, assume 20 C */
+	ret = power_supply_batinfo_ocv2cap(info, v.intval, 20);
+	if (ret < 0)
+		return ret;
+
+	*val = ret;
+	return 0;
+}
+
 static int axp20x_battery_get_prop(struct power_supply *psy,
 				   enum power_supply_property psp,
 				   union power_supply_propval *val)
@@ -423,6 +470,10 @@ static int axp20x_battery_get_prop(struct power_supply *psy,
 			return 0;
 		}
 
+		if (!axp20x_batt->data->has_fg)
+			return axp20x_battery_ocv_capacity(axp20x_batt,
+							   &val->intval);
+
 		ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, &reg);
 		if (ret)
 			return ret;
@@ -955,6 +1006,7 @@ static enum power_supply_property axp192_battery_props[] = {
 	POWER_SUPPLY_PROP_HEALTH,
 	POWER_SUPPLY_PROP_VOLTAGE_MAX,
 	POWER_SUPPLY_PROP_VOLTAGE_MIN,
+	POWER_SUPPLY_PROP_CAPACITY,
 };
 
 static const struct power_supply_desc axp192_batt_ps_desc = {
@@ -1237,7 +1289,11 @@ static int axp20x_power_probe(struct platform_device *pdev)
 
 	if (!power_supply_get_battery_info(axp20x_batt->batt, &info)) {
 		axp20x_batt->data->set_bat_info(pdev, axp20x_batt, info);
-		power_supply_put_battery_info(axp20x_batt->batt, info);
+		/* Without a fuel gauge the OCV table is used for the capacity */
+		if (axp20x_batt->data->has_fg)
+			power_supply_put_battery_info(axp20x_batt->batt, info);
+		else
+			axp20x_batt->bat_info = info;
 	}
 
 	/*

-- 
2.43.0



  parent reply	other threads:[~2026-12-06  3:08 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-06  3:08 [PATCH v7 0/9] Add the remaining AXP192 PMIC support Molnár Máté via B4 Relay
2026-12-06  3:08 ` [PATCH v7 1/9] dt-bindings: iio: adc: axp209: Add AXP192 compatible Molnár Máté via B4 Relay
2026-12-06  3:08 ` [PATCH v7 2/9] dt-bindings: power: axp20x-battery: " Molnár Máté via B4 Relay
2026-12-06  3:08 ` [PATCH v7 3/9] regulator: axp20x: Add support for AXP192 Molnár Máté via B4 Relay
2026-12-06  3:08 ` [PATCH v7 4/9] power: axp20x_battery: Add constant charge current table Molnár Máté via B4 Relay
2026-12-06  3:08 ` [PATCH v7 5/9] power: axp20x_battery: Support battery status without fuel gauge Molnár Máté via B4 Relay
2026-12-06  3:08 ` [PATCH v7 6/9] power: axp20x_battery: Add support for AXP192 Molnár Máté via B4 Relay
2026-12-06  3:08 ` Molnár Máté via B4 Relay [this message]
2026-12-06  3:09 ` [PATCH v7 8/9] Input: axp20x-pek: Add AXP192 support Molnár Máté via B4 Relay
2026-12-06  4:44   ` Dmitry Torokhov
2026-12-06  3:09 ` [PATCH v7 9/9] mfd: axp20x: Add the AXP192 power key cell Molnár Máté via B4 Relay

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261206-axp192-v7-7-0200fc77da53@gmail.com \
    --to=devnull+matemolnar88.gmail.com@kernel.org \
    --cc=aidanmacdonald.0x0@gmail.com \
    --cc=andy@kernel.org \
    --cc=broonie@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=dmitry.torokhov@gmail.com \
    --cc=jic23@kernel.org \
    --cc=knaerzche@gmail.com \
    --cc=krzk+dt@kernel.org \
    --cc=lee@kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=matemolnar88@gmail.com \
    --cc=mfd@lists.linux.dev \
    --cc=nuno.sa@analog.com \
    --cc=robh@kernel.org \
    --cc=sre@kernel.org \
    --cc=wens@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox