Skip to contentSkip to conversation

Add driver for Qualcomm QGauge (qcom_qg) fuel gauge

PATCH In review Val Packett 7 participants 16 messages Nov 12 – Nov 15

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover Add driver for Qualcomm QGauge (qcom_qg) fuel gauge

Hi o/

This series adds a driver for the "qgauge" battery fuel gauge present in Qualcomm PMICs like PMI632, PM6150, PM7250B, PM2250. The PMI632 is used in SDM632, SM6115/SM4250, SM6125 based phones.

This series was tested on sm6115-motorola-{cebu,guamp}, whose basic dts has been submitted as [1].

There have been two attempts at making this driver, the first one "WIP! power: supply: Add rude driver for QGauge" as found in various development forks [2] was rather infamous for wild capacity% swings due to load variation :D

The second one was submitted to msm8953-mainline at [3] by Marc Lainez, and it behaved much better thanks to the use of power_supply_batinfo_ocv2cap.

This is a continuation of that work, carrying his From/S-o-b on modified patches with explicit permission. I have added the SDAM read via NVMEM API, fixed the batt_info leak and did some general cleanup.

With the SMB5 work also going on right now, we'll finally have full battery support on these awkward pre-battmgr generations!

[1]: https://lore.kernel.org/all/[email protected]/
[2]: https://github.com/sm7150-mainline/linux/commit/76879bf7a71f4f8f66b9bcd7175069ca7bbf4274
[3]: https://github.com/msm8953-mainline/linux/pull/242

Thanks,
~val

---

Marc Lainez (2):
dt-bindings: power: supply: add Qualcomm QGauge fuel gauge
power: supply: add Qualcomm QGauge (qcom_qg) fuel gauge driver

Val Packett (3):
iio: adc: qcom-spmi-adc5: Add ADC5_BAT_THERM_100K_PU
arm64: dts: qcom: pmi632: Add missing bat_therm ADC channel
arm64: dts: qcom: pmi632: Add battery fuel gauge node

 .../bindings/power/supply/qcom,qg.yaml        | 105 +++
 MAINTAINERS                                   |   7 +
 arch/arm64/boot/dts/qcom/pmi632.dtsi          |  41 +
 drivers/iio/adc/qcom-spmi-adc5.c              |   2 +
 drivers/power/supply/Kconfig                  |  13 +
 drivers/power/supply/Makefile                 |   1 +
 drivers/power/supply/qcom_qg.c                | 807 ++++++++++++++++++
 7 files changed, 976 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/power/supply/qcom,qg.yaml
 create mode 100644 drivers/power/supply/qcom_qg.c
Signature · 1 line
2.56.0
#1 lore raw

PATCH 1/5 dt-bindings: power: supply: add Qualcomm QGauge fuel gauge

From: Marc Lainez <[email protected]>

Document the QGauge fuel-gauge block handled by the qcom_qg driver, with compatibles for the PMI632 and PM6150 PMICs.

---
 .../bindings/power/supply/qcom,qg.yaml        | 105 ++++++++++++++++++
 MAINTAINERS                                   |   6 +
 2 files changed, 111 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/power/supply/qcom,qg.yaml
Documentation/devicetree/bindings/power/supply/qcom,qg.yaml new file +105 -0
new file mode 100644
index 000000000000..79dd18d87879
@@ -0,0 +1,105 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/power/supply/qcom,qg.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Qualcomm QGauge Fuel Gauge
+
+maintainers:
+  - Val Packett <[email protected]>
+
+description:
+  The QGauge (QG) block found in some Qualcomm PMICs (PM6150, PMI632)
+  provides battery fuel gauging through hardware voltage and current
+  FIFO sampling, open circuit voltage (OCV) measurement, and coulomb
+  counting. The hardware has a multi-state FSM (S1-S7) that automatically
+  transitions between active sampling and low-power sleep states.
+
+allOf:
+  - $ref: /schemas/power/supply/power-supply.yaml#
+  - $ref: /schemas/nvmem/nvmem-consumer.yaml#
+
+properties:
+  compatible:
+    enum:
+      - qcom,pm6150-qg
+      - qcom,pmi632-qg
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    items:
+      - description: Battery missing interrupt
+      - description: Battery voltage low interrupt
+      - description: Battery voltage empty interrupt
+      - description: FIFO data update done interrupt
+      - description: Good OCV measured interrupt
+      - description: FSM state change interrupt
+      - description: QG event interrupt
+
+  interrupt-names:
+    items:
+      - const: batt-missing
+      - const: vbat-low
+      - const: vbat-empty
+      - const: fifo-done
+      - const: good-ocv
+      - const: fsm-state-chg
+      - const: event
+
+  io-channels:
+    items:
+      - description: ADC channel for battery thermistor temperature
+
+  io-channel-names:
+    items:
+      - const: batt-therm
+
+  monitored-battery: true
+
+  nvmem: true
+
+  power-supplies: true
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - interrupt-names
+  - monitored-battery
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    pmic {
+      #address-cells = <1>;
+      #size-cells = <0>;
+
+      fuel-gauge@4800 {
+        compatible = "qcom,pmi632-qg";
+        reg = <0x4800>;
+
+        interrupts = <0x2 0x48 0x0 IRQ_TYPE_EDGE_BOTH>,
+                     <0x2 0x48 0x1 IRQ_TYPE_EDGE_BOTH>,
+                     <0x2 0x48 0x2 IRQ_TYPE_EDGE_RISING>,
+                     <0x2 0x48 0x3 IRQ_TYPE_EDGE_RISING>,
+                     <0x2 0x48 0x4 IRQ_TYPE_EDGE_RISING>,
+                     <0x2 0x48 0x5 IRQ_TYPE_EDGE_RISING>,
+                     <0x2 0x48 0x6 IRQ_TYPE_EDGE_RISING>;
+        interrupt-names = "batt-missing",
+                          "vbat-low",
+                          "vbat-empty",
+                          "fifo-done",
+                          "good-ocv",
+                          "fsm-state-chg",
+                          "event";
+
+        nvmem = <&pmi632_sdam_2>;
+
+        monitored-battery = <&battery>;
+      };
+    };
MAINTAINERS +6 -0
index b253b10dd436..7b76b07ed66f 100644
@@ -23328,6 +23328,12 @@ F:	drivers/firmware/qcom/qcom_pas.h
 F:	drivers/firmware/qcom/qcom_pas_tee.c
 F:	include/linux/firmware/qcom/qcom_pas.h
 
+QUALCOMM QGAUGE FUEL GAUGE DRIVER
+M:	Val Packett <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/power/supply/qcom,qg.yaml
+
 QUALCOMM SMB CHARGER DRIVER
 M:	Casey Connolly <[email protected]>
 L:	[email protected]
Signature · 1 line
2.56.0
#2 lore raw

Re: [PATCH 1/5] dt-bindings: power: supply: add Qualcomm QGauge fuel gauge

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] dt-bindings: power: supply: qcom,qg: unconstrained nvmem and schema refs --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] dt-bindings: power: supply: qcom,qg: unconstrained nvmem and schema refs
--

commit 3e127648f3c0c46268041b3bde4ad7d6e88f27b3
Author: Marc Lainez <[email protected]>

dt-bindings: power: supply: add Qualcomm QGauge fuel gauge

This commit adds the Device Tree binding schema for the Qualcomm QGauge fuel gauge block on PMI632 and PM6150 PMICs and adds a corresponding MAINTAINERS entry.

Quoted 6 lines
Documentation/devicetree/bindings/power/supply/qcom,qg.yaml new file +0 -0
new file mode 100644
index 0000000000000..79dd18d878793
@@ -0,0 +1,105 @@

[ ... ]

Quoted 3 lines
Documentation/devicetree/bindings/power/supply/qcom,qg.yaml new file +3 -0
+allOf:
+  - $ref: /schemas/power/supply/power-supply.yaml#
+  - $ref: /schemas/nvmem/nvmem-consumer.yaml#

[ ... ]

Quoted 4 lines
Documentation/devicetree/bindings/power/supply/qcom,qg.yaml new file +4 -0
+  monitored-battery: true
+
+  nvmem: true
+

Documentation/devicetree/bindings/power/supply/qcom,qg.yaml line 73

Quoted 10 lines
Documentation/devicetree/bindings/power/supply/qcom,qg.yaml new file +10 -0
+  power-supplies: true
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - interrupt-names
+  - monitored-battery
+
+additionalProperties: false

[Severity: Low]
Should nvmem specify maxItems: 1 instead of true, and should this binding use unevaluatedProperties: false without referencing nvmem-consumer.yaml# in allOf?

nvmem-consumer.yaml is a base consumer schema with select: true that dtschema applies automatically, and it only constrains each nvmem entry to a single phandle while leaving the outer array length to the device binding. Dropping nvmem-consumer.yaml# from allOf and switching
additionalProperties: false to unevaluatedProperties: false also avoids having to redeclare monitored-battery and power-supplies from power-supply.yaml#.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH 1/5] dt-bindings: power: supply: add Qualcomm QGauge fuel gauge

In reply to Val Packett

Documentation/devicetree/bindings/power/supply/qcom,qg.yaml

Quoted 18 lines · Val Packett

From: Marc Lainez <[email protected]>

Document the QGauge fuel-gauge block handled by the qcom_qg driver, with compatibles for the PMI632 and PM6150 PMICs.

---
 .../bindings/power/supply/qcom,qg.yaml        | 105 ++++++++++++++++++
 MAINTAINERS                                   |   6 +
 2 files changed, 111 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/power/supply/qcom,qg.yaml
Documentation/devicetree/bindings/power/supply/qcom,qg.yaml new file +0 -0
new file mode 100644
index 000000000000..79dd18d87879

Filename should match one of the compatibles.

Quoted 51 lines
Documentation/devicetree/bindings/power/supply/qcom,qg.yaml new file +50 -0
@@ -0,0 +1,105 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/power/supply/qcom,qg.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Qualcomm QGauge Fuel Gauge
+
+maintainers:
+  - Val Packett <[email protected]>
+
+description:
+  The QGauge (QG) block found in some Qualcomm PMICs (PM6150, PMI632)
+  provides battery fuel gauging through hardware voltage and current
+  FIFO sampling, open circuit voltage (OCV) measurement, and coulomb
+  counting. The hardware has a multi-state FSM (S1-S7) that automatically
+  transitions between active sampling and low-power sleep states.
+
+allOf:
+  - $ref: /schemas/power/supply/power-supply.yaml#
+  - $ref: /schemas/nvmem/nvmem-consumer.yaml#
+
+properties:
+  compatible:
+    enum:
+      - qcom,pm6150-qg
+      - qcom,pmi632-qg
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    items:
+      - description: Battery missing interrupt
+      - description: Battery voltage low interrupt
+      - description: Battery voltage empty interrupt
+      - description: FIFO data update done interrupt
+      - description: Good OCV measured interrupt
+      - description: FSM state change interrupt
+      - description: QG event interrupt
+
+  interrupt-names:
+    items:
+      - const: batt-missing
+      - const: vbat-low
+      - const: vbat-empty
+      - const: fifo-done
+      - const: good-ocv
+      - const: fsm-state-chg
+      - const: event

Documentation/devicetree/bindings/power/supply/qcom,qg.yaml line 60

Quoted 10 lines
Documentation/devicetree/bindings/power/supply/qcom,qg.yaml new file +10 -0
+
+  io-channels:
+    items:
+      - description: ADC channel for battery thermistor temperature
+
+  io-channel-names:
+    items:
+      - const: batt-therm
+
+  monitored-battery: true

drop

Documentation/devicetree/bindings/power/supply/qcom,qg.yaml line 62

Quoted 2 lines
Documentation/devicetree/bindings/power/supply/qcom,qg.yaml new file +2 -0
+
+  nvmem: true

Missing constraints

Documentation/devicetree/bindings/power/supply/qcom,qg.yaml line 64

Quoted 2 lines
Documentation/devicetree/bindings/power/supply/qcom,qg.yaml new file +2 -0
+
+  power-supplies: true

Drop

Documentation/devicetree/bindings/power/supply/qcom,qg.yaml line 73

Quoted 9 lines
Documentation/devicetree/bindings/power/supply/qcom,qg.yaml new file +9 -0
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - interrupt-names
+  - monitored-battery
+
+additionalProperties: false

unevaluatedProperties instead

Documentation/devicetree/bindings/power/supply/qcom,qg.yaml line 75

Quoted 2 lines
Documentation/devicetree/bindings/power/supply/qcom,qg.yaml new file +2 -0
+
+examples:

Best regards,
Krzysztof

#4 lore raw

PATCH 2/5 power: supply: add Qualcomm QGauge (qcom_qg) fuel gauge driver

In reply to Val Packett

From: Marc Lainez <[email protected]>

Add a driver for the QGauge (QG) fuel-gauge block found in Qualcomm SPMI PMICs such as PMI632 and PM6150. The QG hardware provides FIFO-based voltage/current sampling, sleep-time open-circuit-voltage measurement and coulomb counting; state-of-charge is computed in-kernel from the monitored-battery OCV-to-capacity tables. Coulomb counting is not implemented yet.

The driver exposes present/status/health/technology, instantaneous and running-average voltage and current, OCV, capacity, temperature, and battery design limits via the power_supply class.

[val: cleanup, implement SDAM full charge, fix batt_info leak]

---

Assisted-by LLM

(as declared by Marc in the GitHub thread)

---
 MAINTAINERS                    |   1 +
 drivers/power/supply/Kconfig   |  13 +
 drivers/power/supply/Makefile  |   1 +
 drivers/power/supply/qcom_qg.c | 807 +++++++++++++++++++++++++++++++++
 4 files changed, 822 insertions(+)
 create mode 100644 drivers/power/supply/qcom_qg.c
MAINTAINERS +1 -0
index 7b76b07ed66f..111e56340be8 100644
@@ -23333,6 +23333,7 @@ M:	Val Packett <[email protected]>
 L:	[email protected]
 S:	Maintained
 F:	Documentation/devicetree/bindings/power/supply/qcom,qg.yaml
+F:	drivers/power/supply/qcom_qg.c
 
 QUALCOMM SMB CHARGER DRIVER
 M:	Casey Connolly <[email protected]>
drivers/power/supply/Kconfig +13 -0
index 16d0a38b41b7..ad235a9057d1 100644
@@ -228,6 +228,19 @@ config BATTERY_QCOM_BATTMGR
 	  which is used on modern Qualcomm platforms to provide battery and
 	  power supply information.
 
+config BATTERY_QCOM_QG
+	tristate "Qualcomm QGauge fuel gauge support"
+	depends on MFD_SPMI_PMIC || COMPILE_TEST
+	depends on IIO
+	help
+	  Say Y to add support for the QGauge (QG) fuel gauge block found
+	  in Qualcomm PMICs such as PMI632 and PM6150. This hardware provides
+	  battery voltage and current FIFO sampling, open circuit voltage
+	  (OCV) measurement, and coulomb counting for battery state estimation.
+
+	  To compile this driver as module, choose M here: the
+	  module will be called qcom_qg.
+
 config BATTERY_OLPC
 	tristate "One Laptop Per Child battery"
 	depends on OLPC_EC
drivers/power/supply/Makefile +1 -0
index cd3640f4b748..07296f3f7914 100644
@@ -134,6 +134,7 @@ obj-$(CONFIG_CHARGER_SURFACE)	+= surface_charger.o
 obj-$(CONFIG_BATTERY_CHARGER_SURFACE_RT) += surface-rt-ec.o
 obj-$(CONFIG_BATTERY_UG3105)	+= ug3105_battery.o
 obj-$(CONFIG_CHARGER_QCOM_SMB2)	+= qcom_smbx.o
+obj-$(CONFIG_BATTERY_QCOM_QG)	+= qcom_qg.o
 obj-$(CONFIG_FUEL_GAUGE_MM8013)	+= mm8013.o
 obj-$(CONFIG_MACSMC_POWER)	+= macsmc-power.o
 obj-$(CONFIG_CHARGER_MAX77759)	+= max77759_charger.o
drivers/power/supply/qcom_qg.c new file +807 -0

807 lines not on this page · raw message

Signature · 1 line
2.56.0
#5 lore raw

Re: [PATCH 2/5] power: supply: add Qualcomm QGauge (qcom_qg) fuel gauge driver

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] power: supply: qcom_qg: UAF of batt_info in qcom_qg_probe() teardown - [Medium] power: supply…

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] power: supply: qcom_qg: UAF of batt_info in qcom_qg_probe() teardown
- [Medium] power: supply: qcom_qg: temperature unit mismatch in qg_get_capacity()
- [Medium] power: supply: qcom_qg: broken temperature threshold checks in qg_get_health()
- [Medium] power: supply: qcom_qg: unhandled -EINVAL design voltage limits in health/props
--

commit 23cbeedd29303e2809a8e496524a3641be8ea9bc
Author: Marc Lainez <[email protected]>

power: supply: add Qualcomm QGauge (qcom_qg) fuel gauge driver

This commit adds a power_supply driver for the Qualcomm QGauge (QG) fuel gauge hardware block found in SPMI PMICs such as PMI632 and PM6150.

Quoted 5 lines
drivers/power/supply/qcom_qg.c new file +0 -0
new file mode 100644
index 0000000000000..071e29d4c2991

[ ... ]

Quoted 11 lines
drivers/power/supply/qcom_qg.c new file +11 -0
+static int qg_get_capacity(struct qcom_qg *qg, int *capacity)
+{
+	int temp_decidegc;
+	int cap, ret;
+
+	if (!qg->batt_info)
+		return -ENODATA;
+
+	/*
+	 * Without OCV, it's impossible to calculate state-of-charge.
+	 * Well, you could try, but you'd get *massive* load swings.

drivers/power/supply/qcom_qg.c line 331

Quoted 10 lines
drivers/power/supply/qcom_qg.c new file +10 -0
+	 */
+	if (!qg->ocv_uv)
+		return -ENODATA;
+
+	ret = qg_read_temperature(qg, &temp_decidegc);
+	if (ret)
+		temp_decidegc = 250; /* default 25.0 degC */
+
+	cap = power_supply_batinfo_ocv2cap(qg->batt_info, qg->ocv_uv,
+					   temp_decidegc);

[Severity: Medium]
Does passing temp_decidegc to power_supply_batinfo_ocv2cap() use the wrong temperature unit?

qg_read_temperature() and the fallback value return tenths of a degree Celsius (e.g. 250 for 25.0 degC), whereas power_supply_get_battery_info() populates info->ocv_temp[] from ocv-capacity-celsius in whole degrees Celsius. When multiple temperature OCV tables are defined, power_supply_find_ocv2cap_table() compares decidegrees against degrees and selects the wrong table.

[ ... ]

Quoted 32 lines
drivers/power/supply/qcom_qg.c new file +32 -0
+static int qg_get_health(struct qcom_qg *qg, int *health)
+{
+	struct power_supply_battery_info *info = qg->batt_info;
+	int temp_decidegc;
+	int ret;
+
+	if (!qg->batt_present) {
+		*health = POWER_SUPPLY_HEALTH_UNKNOWN;
+		return 0;
+	}
+
+	if (info) {
+		ret = qg_read_temperature(qg, &temp_decidegc);
+
+		/* No thermistor is not a health fault */
+		if (ret && ret != -ENODEV)
+			return ret;
+
+		if (!ret) {
+			/* Unset thresholds stay 0; 0 degC is a valid reading. */
+			if (info->temp_alert_max > 0 &&
+			    temp_decidegc >= info->temp_alert_max) {
+				*health = POWER_SUPPLY_HEALTH_HOT;
+				return 0;
+			}
+			if (info->temp_alert_min > 0 &&
+			    temp_decidegc <= info->temp_alert_min) {
+				*health = POWER_SUPPLY_HEALTH_COLD;
+				return 0;
+			}
+			if (info->temp_max > 0 &&
+			    temp_decidegc >= info->temp_max) {

drivers/power/supply/qcom_qg.c line 412

Quoted 10 lines
drivers/power/supply/qcom_qg.c new file +10 -0
+				*health = POWER_SUPPLY_HEALTH_WARM;
+				return 0;
+			}
+			if (info->temp_min > 0 &&
+			    temp_decidegc <= info->temp_min) {
+				*health = POWER_SUPPLY_HEALTH_COOL;
+				return 0;
+			}
+		}
+	}

[Severity: Medium]
Are the temperature threshold checks in qg_get_health() comparing decidegrees against whole degrees and using the wrong sentinel values?

power_supply_get_battery_info() initializes unset info->temp_alert_min and info->temp_min to INT_MIN and unset info->temp_alert_max and info->temp_max to INT_MAX, and populates them from alert-celsius and operating-range-celsius in whole degrees Celsius.

Because temp_decidegc is in tenths of a degree Celsius and unset max limits are INT_MAX (which satisfies > 0), any room-temperature reading (e.g. 250 for 25.0 degC) is compared directly against a whole-degree limit (e.g. 45) and falsely reports POWER_SUPPLY_HEALTH_HOT. Checking > 0 on temp_alert_min and temp_min also ignores any cold threshold at or below 0 degC. In addition, temp_min and temp_max represent the outer operating range (COLD/HOT) while temp_alert_min and temp_alert_max are the inner alert thresholds (COOL/WARM), so their health states are swapped here.

Quoted 12 lines
drivers/power/supply/qcom_qg.c new file +12 -0
+
+	*health = POWER_SUPPLY_HEALTH_GOOD;
+
+	if (info && qg->vbat_uv) {
+		if (qg->vbat_uv < info->voltage_min_design_uv)
+			*health = POWER_SUPPLY_HEALTH_DEAD;
+		else if (qg->vbat_uv > info->voltage_max_design_uv)
+			*health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
+	}
+
+	return 0;
+}

[ ... ]

drivers/power/supply/qcom_qg.c line 550

Quoted 11 lines
drivers/power/supply/qcom_qg.c new file +11 -0
+	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
+		if (!qg->batt_info)
+			return -ENODATA;
+		val->intval = qg->batt_info->voltage_max_design_uv;
+		return 0;
+
+	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
+		if (!qg->batt_info)
+			return -ENODATA;
+		val->intval = qg->batt_info->voltage_min_design_uv;
+		return 0;

[Severity: Medium]
What happens in qg_get_health() and qg_get_property() when voltage-min-design-microvolt or voltage-max-design-microvolt is omitted from the battery node?

power_supply_get_battery_info() initializes info->voltage_min_design_uv and info->voltage_max_design_uv to -EINVAL. In qg_get_health(), comparing unsigned int qg->vbat_uv against signed int info->voltage_min_design_uv promotes -EINVAL to UINT_MAX - 21, so qg->vbat_uv < info->voltage_min_design_uv is always true and reports POWER_SUPPLY_HEALTH_DEAD. In qg_get_property(), POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN and POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN return 0 with val->intval = -22 instead of returning -ENODATA when those fields are negative (unlike POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN just below).

[ ... ]

Quoted 9 lines
drivers/power/supply/qcom_qg.c new file +9 -0
+static void qg_batt_info_cleanup(void *data)
+{
+	struct qcom_qg *qg = data;
+
+	power_supply_put_battery_info(qg->psy, qg->batt_info);
+}
+
+static int qcom_qg_probe(struct platform_device *pdev)
+{

[ ... ]

drivers/power/supply/qcom_qg.c line 724

Quoted 12 lines
drivers/power/supply/qcom_qg.c new file +12 -0
+	qg->psy = devm_power_supply_register(dev, &qg_psy_desc, &psy_cfg);
+	if (IS_ERR(qg->psy))
+		return dev_err_probe(dev, PTR_ERR(qg->psy),
+				     "Failed to register power supply");
+
+	ret = power_supply_get_battery_info(qg->psy, &qg->batt_info);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to get battery info");
+
+	ret = devm_add_action_or_reset(dev, qg_batt_info_cleanup, qg);
+	if (ret)
+		return ret;

[Severity: High]
Can qg_get_property() dereference qg->batt_info after it has been freed during driver unbind or probe unwind?

devm_add_action_or_reset(dev, qg_batt_info_cleanup, qg) is registered on dev after devm_power_supply_register(). Because devres releases resources in reverse order, on driver unbind or if any of the subsequent qg_get_optional_irq() calls fails, qg_batt_info_cleanup() frees qg->batt_info and its ocv_table[] arrays while qg->psy is still registered and qg->batt_info remains non-NULL. Any sysfs read or pending uevent work flushed during power_supply_unregister() then calls qg_get_property() and reads freed memory. Calling power_supply_get_battery_info() after devm_power_supply_register() also leaves qg->batt_info NULL while qg->psy is first exposed to userspace and thermal/hwmon consumers.

Note that because qg_psy_desc.type is POWER_SUPPLY_TYPE_BATTERY, __power_supply_register() already populates psy->battery_info before device_add() and frees it when psy->dev is unregistered, or if qg->batt_info is kept, its cleanup action only runs safely if unregistered after qg->psy (or if qg->batt_info is allocated on &qg->psy->dev without a separate dev action that runs before power_supply_unregister()).

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#6 lore raw

Re: [PATCH 2/5] power: supply: add Qualcomm QGauge (qcom_qg) fuel gauge driver

In reply to Val Packett

drivers/power/supply/qcom_qg.c line 27

Quoted 3 lines · Val Packett
drivers/power/supply/qcom_qg.c new file +2 -0
+#include <linux/mod_devicetable.h>
[...]
+#include <linux/platform_device.h>

Please don't include <linux/mod_devicetable.h>. That introduces a bunch of unneeded build dependencies and of_device_id is already provided (without these dependencies) by <linux/platform_device.h>.

drivers/power/supply/qcom_qg.c line 790

Quoted 5 lines

[...]

drivers/power/supply/qcom_qg.c new file +4 -0
+static const struct of_device_id qcom_qg_of_match[] = {
+	{ .compatible = "qcom,pm6150-qg" },
+	{ .compatible = "qcom,pmi632-qg" },
+	{}

Please make this { } (i.e add a space) to match the most common style.

drivers/power/supply/qcom_qg.c line 792

Quoted 2 lines
drivers/power/supply/qcom_qg.c new file +2 -0
+};
+MODULE_DEVICE_TABLE(of, qcom_qg_of_match);

Best regards
Uwe

#7 lore raw

PATCH 3/5 iio: adc: qcom-spmi-adc5: Add ADC5_BAT_THERM_100K_PU

In reply to Val Packett

Even though it existed in bindings for the longest time, ADC5_BAT_THERM_100K_PU was never assigned in the driver. Do so.

---
 drivers/iio/adc/qcom-spmi-adc5.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iio/adc/qcom-spmi-adc5.c +2 -0
index d57ba8526e0c..c27ff89429f5 100644
@@ -542,6 +542,8 @@ static const struct adc5_channels adc5_chans_pmic[ADC5_MAX_CHANNEL] = {
 					SCALE_HW_CALIB_DEFAULT)
 	[ADC5_XO_THERM_100K_PU]	= ADC5_CHAN_TEMP("xo_therm", 0,
 					SCALE_HW_CALIB_XOTHERM)
+	[ADC5_BAT_THERM_100K_PU]	= ADC5_CHAN_TEMP("bat_therm", 0,
+					SCALE_HW_CALIB_THERM_100K_PULLUP)
 	[ADC5_BAT_ID_100K_PU]	= ADC5_CHAN_TEMP("bat_id", 0,
 					SCALE_HW_CALIB_DEFAULT)
 	[ADC5_AMUX_THM1_100K_PU] = ADC5_CHAN_TEMP("amux_thm1_100k_pu", 0,
Signature · 1 line
2.56.0
maintainer
#8 lore raw

Re: [PATCH 3/5] iio: adc: qcom-spmi-adc5: Add ADC5_BAT_THERM_100K_PU

Quoted 13 lines · On Thu, 12 Nov 2026 14:07:29 -0300 Val Packett <[email protected]> wrote:

Even though it existed in bindings for the longest time, ADC5_BAT_THERM_100K_PU was never assigned in the driver. Do so.

---
 drivers/iio/adc/qcom-spmi-adc5.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iio/adc/qcom-spmi-adc5.c +0 -0
index d57ba8526e0c..c27ff89429f5 100644

drivers/iio/adc/qcom-spmi-adc5.c static const struct adc5_channels adc5_chans_pmic[ADC5_MAX_CHANNEL] = { · line 546

Quoted 5 lines
drivers/iio/adc/qcom-spmi-adc5.c +2 -0
@@ -542,6 +542,8 @@ static const struct adc5_channels adc5_chans_pmic[ADC5_MAX_CHANNEL] = {
 					SCALE_HW_CALIB_DEFAULT)
 	[ADC5_XO_THERM_100K_PU]	= ADC5_CHAN_TEMP("xo_therm", 0,
 					SCALE_HW_CALIB_XOTHERM)
+	[ADC5_BAT_THERM_100K_PU]	= ADC5_CHAN_TEMP("bat_therm", 0,
+					SCALE_HW_CALIB_THERM_100K_PULLUP)

Whilst alignment isn't exactly consistent, this should probably match the one two down that has same number of chars in the _PU] bit. So use a space rather than a tab.

Otherwise seems fine to me.

Quoted 3 lines
drivers/iio/adc/qcom-spmi-adc5.c +0 -0
 	[ADC5_BAT_ID_100K_PU]	= ADC5_CHAN_TEMP("bat_id", 0,
 					SCALE_HW_CALIB_DEFAULT)
 	[ADC5_AMUX_THM1_100K_PU] = ADC5_CHAN_TEMP("amux_thm1_100k_pu", 0,
#9 lore raw

Re: [PATCH 3/5] iio: adc: qcom-spmi-adc5: Add ADC5_BAT_THERM_100K_PU

In reply to Val Packett

Hi Val,

Quoted 13 lines · Val Packett

Even though it existed in bindings for the longest time, ADC5_BAT_THERM_100K_PU was never assigned in the driver. Do so.

---
 drivers/iio/adc/qcom-spmi-adc5.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iio/adc/qcom-spmi-adc5.c +0 -0
index d57ba8526e0c..c27ff89429f5 100644

drivers/iio/adc/qcom-spmi-adc5.c static const struct adc5_channels adc5_chans_pmic[ADC5_MAX_CHANNEL] = { · line 546

Quoted 5 lines
drivers/iio/adc/qcom-spmi-adc5.c +2 -0
@@ -542,6 +542,8 @@ static const struct adc5_channels adc5_chans_pmic[ADC5_MAX_CHANNEL] = {
 					SCALE_HW_CALIB_DEFAULT)
 	[ADC5_XO_THERM_100K_PU]	= ADC5_CHAN_TEMP("xo_therm", 0,
 					SCALE_HW_CALIB_XOTHERM)
+	[ADC5_BAT_THERM_100K_PU]	= ADC5_CHAN_TEMP("bat_therm", 0,
+					SCALE_HW_CALIB_THERM_100K_PULLUP)

While this will work fine for some devices, unfortunately this won't work for quite some other devices that don't use a 30K, 100K or 400K NTC in the battery - or when the NTC beta (B) is differing to the hardcoded lookup table.

For example on Fairphone 4 (pm7250b fwiw), the battery has a 10kΩ NTC, so using the ADC5_BAT_THERM_30K_PU with the hard-coded lookup tables would result in very wrong temperature values. Even ignoring the whole NTC beta topic.

You can see my proposed patch series at
https://lore.kernel.org/linux-arm-msm/[email protected]/

Unfortunately it's from February and I haven't spent more time on this topic since then, mostly because fuel gauge anyways was downstream, so pushing just this upstream wouldn't make much difference.

Now it does, so I should get back to it... Let me know your thoughts!

(Fairphone 3 with SDM632/PMI632 would actually be fine-ish as-is because the battery has a 100kΩ NTC and the beta probably is close enough to what's in adcmap_100k_104ef_104fb_1875_vref)

Regards
Luca

Quoted 3 lines
drivers/iio/adc/qcom-spmi-adc5.c +0 -0
 	[ADC5_BAT_ID_100K_PU]	= ADC5_CHAN_TEMP("bat_id", 0,
 					SCALE_HW_CALIB_DEFAULT)
 	[ADC5_AMUX_THM1_100K_PU] = ADC5_CHAN_TEMP("amux_thm1_100k_pu", 0,
#10 lore raw

Re: [PATCH 3/5] iio: adc: qcom-spmi-adc5: Add ADC5_BAT_THERM_100K_PU

Quoted 31 lines · Luca Weiss

Hi Val,

Quoted 13 lines · Val Packett

Even though it existed in bindings for the longest time, ADC5_BAT_THERM_100K_PU was never assigned in the driver. Do so.

---
  drivers/iio/adc/qcom-spmi-adc5.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/iio/adc/qcom-spmi-adc5.c +0 -0
index d57ba8526e0c..c27ff89429f5 100644

drivers/iio/adc/qcom-spmi-adc5.c static const struct adc5_channels adc5_chans_pmic[ADC5_MAX_CHANNEL] = { · line 546

Quoted 5 lines
drivers/iio/adc/qcom-spmi-adc5.c +2 -0
@@ -542,6 +542,8 @@ static const struct adc5_channels adc5_chans_pmic[ADC5_MAX_CHANNEL] = {
  					SCALE_HW_CALIB_DEFAULT)
  	[ADC5_XO_THERM_100K_PU]	= ADC5_CHAN_TEMP("xo_therm", 0,
  					SCALE_HW_CALIB_XOTHERM)
+	[ADC5_BAT_THERM_100K_PU]	= ADC5_CHAN_TEMP("bat_therm", 0,
+					SCALE_HW_CALIB_THERM_100K_PULLUP)

While this will work fine for some devices, unfortunately this won't work for quite some other devices that don't use a 30K, 100K or 400K NTC in the battery - or when the NTC beta (B) is differing to the hardcoded lookup table.

For example on Fairphone 4 (pm7250b fwiw), the battery has a 10kΩ NTC, so using the ADC5_BAT_THERM_30K_PU with the hard-coded lookup tables would result in very wrong temperature values. Even ignoring the whole NTC beta topic.

Quoted 8 lines

You can see my proposed patch series at
https://lore.kernel.org/linux-arm-msm/[email protected]/

Unfortunately it's from February and I haven't spent more time on this topic since then, mostly because fuel gauge anyways was downstream, so pushing just this upstream wouldn't make much difference.

Now it does, so I should get back to it... Let me know your thoughts!

Looks like your series has had very positive feedback so far, from a quick look I haven't even noticed any pending changes really..

It's orthogonal to the qgauge driver itself as the driver just consumes an IIO channel which can be whatever's appropriate for the device, but that's informative in that I probably should not set a default channel and rather leave an explanation in the example in the bindings that it's very battery-specific.

Quoted 10 lines

(Fairphone 3 with SDM632/PMI632 would actually be fine-ish as-is because the battery has a 100kΩ NTC and the beta probably is close enough to what's in adcmap_100k_104ef_104fb_1875_vref)

Regards
Luca

Quoted 3 lines
drivers/iio/adc/qcom-spmi-adc5.c +0 -0
  	[ADC5_BAT_ID_100K_PU]	= ADC5_CHAN_TEMP("bat_id", 0,
  					SCALE_HW_CALIB_DEFAULT)
  	[ADC5_AMUX_THM1_100K_PU] = ADC5_CHAN_TEMP("amux_thm1_100k_pu", 0,

Thanks,
~val

#11 lore raw

PATCH 4/5 arm64: dts: qcom: pmi632: Add missing bat_therm ADC channel

In reply to Val Packett

Add the temperature channel that is used with the battery fuel gauge.

---
 arch/arm64/boot/dts/qcom/pmi632.dtsi | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/arm64/boot/dts/qcom/pmi632.dtsi +8 -0
index ab850050eb22..82b5d7b7bb5b 100644
@@ -124,6 +124,14 @@ channel@9 {
 				label = "chg_temp";
 			};
 
+			channel@4a {
+				reg = <ADC5_BAT_THERM_100K_PU>;
+				qcom,hw-settle-time = <200>;
+				qcom,pre-scaling = <1 1>;
+				qcom,ratiometric;
+				label = "bat_therm";
+			};
+
 			channel@4b {
 				reg = <ADC5_BAT_ID_100K_PU>;
 				qcom,hw-settle-time = <200>;
Signature · 1 line
2.56.0
#12 lore raw

PATCH 5/5 arm64: dts: qcom: pmi632: Add battery fuel gauge node

In reply to Val Packett

Add a node for the "qgauge" battery fuel gauge at 0x4800, and for the SDAM region at 0xb100 which contains the full charge capacity.

---
 arch/arm64/boot/dts/qcom/pmi632.dtsi | 33 ++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
arch/arm64/boot/dts/qcom/pmi632.dtsi +33 -0
index 82b5d7b7bb5b..5e9e82563884 100644
@@ -163,11 +163,44 @@ pmi632_adc_tm: adc-tm@3500 {
 			status = "disabled";
 		};
 
+		pmi632_qgauge: fuel-gauge@4800 {
+			compatible = "qcom,pmi632-qg";
+
+			reg = <0x4800>;
+
+			interrupts = <0x2 0x48 0x0 IRQ_TYPE_EDGE_BOTH>,
+				     <0x2 0x48 0x1 IRQ_TYPE_EDGE_BOTH>,
+				     <0x2 0x48 0x2 IRQ_TYPE_EDGE_RISING>,
+				     <0x2 0x48 0x3 IRQ_TYPE_EDGE_RISING>,
+				     <0x2 0x48 0x4 IRQ_TYPE_EDGE_RISING>,
+				     <0x2 0x48 0x5 IRQ_TYPE_EDGE_RISING>,
+				     <0x2 0x48 0x6 IRQ_TYPE_EDGE_RISING>;
+			interrupt-names = "batt-missing",
+					  "vbat-low",
+					  "vbat-empty",
+					  "fifo-done",
+					  "good-ocv",
+					  "fsm-state-chg",
+					  "event";
+
+			nvmem = <&pmi632_sdam_2>;
+
+			status = "disabled";
+		};
+
 		pmi632_pbs_client3: pbs@7400 {
 			compatible = "qcom,pmi632-pbs", "qcom,pbs";
 			reg = <0x7400>;
 		};
 
+		pmi632_sdam_2: nvram@b100 {
+			compatible = "qcom,spmi-sdam";
+			reg = <0xb100>;
+			#address-cells = <1>;
+			#size-cells = <1>;
+			ranges = <0 0xb100 0x100>;
+		};
+
 		pmi632_sdam_7: nvram@b600 {
 			compatible = "qcom,spmi-sdam";
 			reg = <0xb600>;
Signature · 1 line
2.56.0
#13 lore raw

Re: [PATCH 5/5] arm64: dts: qcom: pmi632: Add battery fuel gauge node

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Low] arm64: dts: qcom: pmi632: fuel-gauge@4800 violates qcom,spmi-pmic.yaml schema - [Low] arm64: dts…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Low] arm64: dts: qcom: pmi632: fuel-gauge@4800 violates qcom,spmi-pmic.yaml schema
- [Low] arm64: dts: qcom: pmi632: missing io-channels in pmi632_qgauge node
--

commit 15ccef39df06d430c96a1c58092319c5e86db8a5
Author: Val Packett <[email protected]>

arm64: dts: qcom: pmi632: Add battery fuel gauge node

This commit adds devicetree nodes for the PMI632 qgauge battery fuel gauge at 0x4800 and the SDAM region at 0xb100.

arch/arm64/boot/dts/qcom/pmi632.dtsi pmi632_adc_tm: adc-tm@3500 { · line 167

Quoted 10 lines
arch/arm64/boot/dts/qcom/pmi632.dtsi +2 -0
index 85cef103e00dc..f04d65fb7aabb 100644
@@ -163,11 +163,44 @@ pmi632_adc_tm: adc-tm@3500 {
 			status = "disabled";
 		};
 
+		pmi632_qgauge: fuel-gauge@4800 {
+			compatible = "qcom,pmi632-qg";

[Severity: Low]
Does adding fuel-gauge@4800 under pmic@2 break dtbs_check against Documentation/devicetree/bindings/mfd/qcom,spmi-pmic.yaml?

That schema matches "qcom,pmi632" and sets additionalProperties: false, but its patternProperties do not list "^fuel-gauge@[0-9a-f]+$" referencing /schemas/power/supply/qcom,qg.yaml#.

Quoted 12 lines
arch/arm64/boot/dts/qcom/pmi632.dtsi +12 -0
+
+			reg = <0x4800>;
+
+			interrupts = <0x2 0x48 0x0 IRQ_TYPE_EDGE_BOTH>,
+				     <0x2 0x48 0x1 IRQ_TYPE_EDGE_BOTH>,
+				     <0x2 0x48 0x2 IRQ_TYPE_EDGE_RISING>,
+				     <0x2 0x48 0x3 IRQ_TYPE_EDGE_RISING>,
+				     <0x2 0x48 0x4 IRQ_TYPE_EDGE_RISING>,
+				     <0x2 0x48 0x5 IRQ_TYPE_EDGE_RISING>,
+				     <0x2 0x48 0x6 IRQ_TYPE_EDGE_RISING>;
+			interrupt-names = "batt-missing",
+					  "vbat-low",

arch/arm64/boot/dts/qcom/pmi632.dtsi pmi632_adc_tm: adc-tm@3500 { · line 189

Quoted 10 lines
arch/arm64/boot/dts/qcom/pmi632.dtsi +10 -0
+					  "vbat-empty",
+					  "fifo-done",
+					  "good-ocv",
+					  "fsm-state-chg",
+					  "event";
+
+			nvmem = <&pmi632_sdam_2>;
+
+			status = "disabled";
+		};

[Severity: Low]
Should pmi632_qgauge also wire up io-channels = <&pmi632_adc ADC5_BAT_THERM_100K_PU> and io-channel-names = "batt-therm" here?

Earlier in this series, commit "arm64: dts: qcom: pmi632: Add missing bat_therm ADC channel" added channel@4a to pmi632_adc for the fuel gauge. Without io-channels on pmi632_qgauge (or in every board DTS), qcom_qg_probe() leaves qg->batt_therm NULL, causing temperature reads to fail with -ENODEV, qg_get_capacity() to fall back to a hardcoded 25 C, and qg_get_health() to skip temperature fault checks.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#14 lore raw

Re: [PATCH 5/5] arm64: dts: qcom: pmi632: Add battery fuel gauge node

In reply to Val Packett
Quoted 8 lines · Val Packett

Add a node for the "qgauge" battery fuel gauge at 0x4800, and for the SDAM region at 0xb100 which contains the full charge capacity.

---
 arch/arm64/boot/dts/qcom/pmi632.dtsi | 33 ++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

It does not look like you tested the DTS against bindings. Please run 'make dtbs_check W=1' (see Documentation/devicetree/bindings/writing-schema.rst or
https://www.linaro.org/blog/tips-and-tricks-for-validating-devicetree-sources-with-the-devicetree-schema/
for instructions).
Maybe you need to update your dtschema and yamllint. Don't rely on distro packages for dtschema and be sure you are using the latest released dtschema.

Best regards,
Krzysztof

Tested-by on the v1 series
#15 lore raw
In reply to Val Packett

Hi Val,

Quoted 3 lines · Val Packett

This series adds a driver for the "qgauge" battery fuel gauge present in Qualcomm PMICs like PMI632, PM6150, PM7250B, PM2250. The PMI632 is used in SDM632, SM6115/SM4250, SM6125 based phones.

I tried v1 on an SM6125 phone, a Xiaomi Mi A3 (sm6125-xiaomi-laurel-sprout, PMI632), on top of next-20261113. Board side: the node enabled with monitored-battery (simple-battery with the OCV table from the stock profile), io-channels = <&pmi632_adc ADC5_BAT_THERM_100K_PU> as "batt-therm" and power-supplies = <&pmi632_typec>.

It probes without any kernel log output and reports sane values:

  capacity            66
  voltage_now         3979548   (vbat_sns from the ADC: 3.98 V)
  voltage_avg         3979937
  current_now         -72631
  temp                297       (SoC TSENS zones: 29-30 C)
  charge_full         4048000   (design 4030000, so the SDAM read works)
  health              Good
  status              Charging

Two things I noticed:

- voltage_ocv returns -ENODATA ("No data available") while capacity is reported at the same time. Is that intended, e.g. the PON OCV being used for capacity but not exposed?

- status is derived only from power_supply_am_i_supplied(), so it says Charging whenever USB is connected. On my phone the charger float voltage is limited to 3.96 V with a local patch and the battery sits at 3.98 V, so current_now stays at about -70 mA (discharging) while status reads Charging. I would expect the same once a battery is full. Would it make sense to report Not charging / Full based on the current?

Happy to test v2 as well.

Thanks,
Majid