* [PATCH 1/2] dt-bindings: power: supply: add samsung,s2mu106-fuelgauge
2026-11-17 10:43 [PATCH 0/2] power: supply: add standalone S2MU106 fuel gauge (Samsung Galaxy XCover5) Adil Hüsrev Tepe
@ 2026-11-17 10:43 ` Adil Hüsrev Tepe
2026-11-17 10:43 ` [PATCH 2/2] power: supply: s2mu106-fuelgauge: add standalone driver Adil Hüsrev Tepe
1 sibling, 0 replies; 3+ messages in thread
From: Adil Hüsrev Tepe @ 2026-11-17 10:43 UTC (permalink / raw)
To: sre, robh, krzk+dt, conor+dt
Cc: linux-pm, devicetree, linux-kernel, Adil Hüsrev Tepe
Add a binding for the Samsung S2MU106 PMIC's fuel gauge, as a
standalone I2C device (own address, 0x3b) rather than a fallback
compatible on samsung,s2mu005-fuel-gauge.
A previous version of this series
(https://lore.kernel.org/all/20261116074000.355176-1-tadilhusrev@gmail.com/)
extended the S2MU005 fuel-gauge binding and driver instead, with
samsung,s2mu106-fuel-gauge falling back to samsung,s2mu005-fuel-gauge.
Sashiko AI's review of that series pointed out that the fallback let a
board bind through the plain samsung,s2mu005-fuel-gauge compatible
with no guard at all, which runs s2mu005_fg_get_voltage_avg()/
get_current_avg() against MONOUT registers the S2MU106 doesn't have,
and can misreport charging status from the resulting garbage reads.
Rather than add a flag to close that specific hole, this binding
avoids the fallback-compatible construction entirely: the S2MU106
fuel gauge only shares the S2MU005's STATUS/RVBAT/RCURCC/RSOC register
layout and scaling, not its device-tree node shape, so it gets its own
compatible and its own driver
("power: supply: s2mu106-fuelgauge: add standalone driver").
samsung,s2mu005-fuel-gauge is left untouched.
Signed-off-by: Adil Hüsrev Tepe <tadilhusrev@gmail.com>
---
.../supply/samsung,s2mu106-fuelgauge.yaml | 51 +++++++++++++++++++
1 file changed, 51 insertions(+)
create mode 100644 Documentation/devicetree/bindings/power/supply/samsung,s2mu106-fuelgauge.yaml
diff --git a/Documentation/devicetree/bindings/power/supply/samsung,s2mu106-fuelgauge.yaml b/Documentation/devicetree/bindings/power/supply/samsung,s2mu106-fuelgauge.yaml
new file mode 100644
index 000000000..3dd91412e
--- /dev/null
+++ b/Documentation/devicetree/bindings/power/supply/samsung,s2mu106-fuelgauge.yaml
@@ -0,0 +1,51 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/power/supply/samsung,s2mu106-fuelgauge.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Samsung S2MU106 Fuel Gauge
+
+maintainers:
+ - Adil Hüsrev Tepe <tadilhusrev@gmail.com>
+
+description:
+ Fuel-gauge sub-function of the Samsung S2MU106 PMIC, on its own I2C address
+ (separate from the 0x3d charger/MUIC/FLED block). As on the Galaxy XCover5.
+
+allOf:
+ - $ref: power-supply.yaml#
+
+properties:
+ compatible:
+ const: samsung,s2mu106-fuelgauge
+
+ reg:
+ maxItems: 1
+
+ interrupts:
+ maxItems: 1
+
+ monitored-battery: true
+
+required:
+ - compatible
+ - reg
+
+additionalProperties: false
+
+examples:
+ - |
+ #include <dt-bindings/interrupt-controller/irq.h>
+ i2c {
+ #address-cells = <1>;
+ #size-cells = <0>;
+ fuel-gauge@3b {
+ compatible = "samsung,s2mu106-fuelgauge";
+ reg = <0x3b>;
+ interrupt-parent = <&gpa1>;
+ interrupts = <3 IRQ_TYPE_LEVEL_LOW>;
+ monitored-battery = <&battery>;
+ };
+ };
+...
--
2.53.0
^ permalink raw reply [flat|nested] 3+ messages in thread* [PATCH 2/2] power: supply: s2mu106-fuelgauge: add standalone driver
2026-11-17 10:43 [PATCH 0/2] power: supply: add standalone S2MU106 fuel gauge (Samsung Galaxy XCover5) Adil Hüsrev Tepe
2026-11-17 10:43 ` [PATCH 1/2] dt-bindings: power: supply: add samsung,s2mu106-fuelgauge Adil Hüsrev Tepe
@ 2026-11-17 10:43 ` Adil Hüsrev Tepe
1 sibling, 0 replies; 3+ messages in thread
From: Adil Hüsrev Tepe @ 2026-11-17 10:43 UTC (permalink / raw)
To: sre, robh, krzk+dt, conor+dt
Cc: linux-pm, devicetree, linux-kernel, Adil Hüsrev Tepe
Add a standalone driver for the fuel gauge in the Samsung S2MU106
PMIC, as fitted on the Samsung Galaxy XCover5 (own I2C address, 0x3b).
It shares the S2MU005 fuel gauge's STATUS/RVBAT/RCURCC/RSOC register
layout and scaling, which this driver reads directly with the stock
fixed-point formulas; it has no MONOUT average-monitoring registers,
so unlike s2mu005-battery.c it never reads VOLTAGE_AVG/CURRENT_AVG or
uses current_avg to decide POWER_SUPPLY_STATUS.
This replaces a previous version of this series
(https://lore.kernel.org/all/20261116074000.355176-1-tadilhusrev@gmail.com/)
that instead extended s2mu005-battery.c with a samsung,s2mu106-fuel-gauge
fallback compatible; see "dt-bindings: power: supply: add
samsung,s2mu106-fuelgauge" in this series for why that shape was
dropped.
The driver was drafted with an AI coding assistant (Claude Code,
Anthropic) from this register map; I reviewed every line, built it
(arm64, 0 warnings), ran checkpatch --strict (clean), and tested it on
a Samsung Galaxy XCover5 (re-confirmed 2026-11-17, kernel
7.4.0-rc1-00023-gdcb8b91fb779+ #30): it probes, and
/sys/class/power_supply/s2mu106-battery/ reports plausible voltage,
capacity and charging-status values that track the device's actual
state (confirmed live, including across a flash/reboot cycle).
Signed-off-by: Adil Hüsrev Tepe <tadilhusrev@gmail.com>
---
drivers/power/supply/Kconfig | 13 ++
drivers/power/supply/Makefile | 1 +
drivers/power/supply/s2mu106-fuelgauge.c | 240 +++++++++++++++++++++++
3 files changed, 254 insertions(+)
create mode 100644 drivers/power/supply/s2mu106-fuelgauge.c
diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig
index 1eeb176b2..cc8fed03b 100644
--- a/drivers/power/supply/Kconfig
+++ b/drivers/power/supply/Kconfig
@@ -240,6 +240,19 @@ config BATTERY_S2MU005
This driver, if built as a module, will be called s2mu005-fuel-gauge.
+config BATTERY_S2MU106
+ tristate "Samsung S2MU106 PMIC fuel gauge driver"
+ depends on I2C
+ select REGMAP_I2C
+ help
+ Say Y here to build a driver for the fuel gauge block of the
+ Samsung S2MU106 PMIC, as fitted on the Samsung Galaxy XCover5.
+ It is a standalone I2C device at address 0x3b, reporting
+ voltage, current, capacity and charging status.
+
+ To compile this driver as a module, choose M here: the module
+ will be called s2mu106-fuelgauge.
+
config CHARGER_S2MU106
tristate "Samsung S2MU106 charger driver (read-only)"
depends on I2C
diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile
index cd16c6e48..531a0b024 100644
--- a/drivers/power/supply/Makefile
+++ b/drivers/power/supply/Makefile
@@ -135,4 +135,5 @@ obj-$(CONFIG_CHARGER_QCOM_SMB2) += qcom_smbx.o
obj-$(CONFIG_FUEL_GAUGE_MM8013) += mm8013.o
obj-$(CONFIG_MACSMC_POWER) += macsmc-power.o
obj-$(CONFIG_CHARGER_MAX77759) += max77759_charger.o
+obj-$(CONFIG_BATTERY_S2MU106) += s2mu106-fuelgauge.o
obj-$(CONFIG_CHARGER_S2MU106) += s2mu106-charger.o
diff --git a/drivers/power/supply/s2mu106-fuelgauge.c b/drivers/power/supply/s2mu106-fuelgauge.c
new file mode 100644
index 000000000..2fdd72c80
--- /dev/null
+++ b/drivers/power/supply/s2mu106-fuelgauge.c
@@ -0,0 +1,240 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Samsung S2MU106 Fuel Gauge driver (XCover5)
+ *
+ * Register map confirmed against stock source AND live device dmesg:
+ * kernel_src/extracted/.../s2mu106_fuelgauge/s2mu106_fuelgauge.{c,h}
+ * hw_exhaustive/11_usb.txt + 20_kernel_base.txt (stock driver live on 0-003b @ i2c-0 0x3B)
+ *
+ * Confirmed registers (stock s2mu106_fuelgauge.h; defines are "addr>>1, R/W bit excluded"):
+ * 0x00 FG_STATUS — power-on / por bits
+ * 0x04 RVBAT — raw VBAT ADC (16-bit LE); mV = (raw*1000)>>13 [live: vbat 3899]
+ * 0x06 RCUR_CC — coulomb-counter current, 2's-comp 16-bit;
+ * bit15 set = charging [live: -386 mA]
+ * 0x08 RSOC — raw SOC, 2's-comp 16-bit;
+ * %*100 = (raw&0x7FFF)*10000/(1<<14) [live: SOC 67]
+ * 0x2A RSOC_R — alternate SOC representation (NOT the main path; stock uses RSOC 0x08)
+ * 0xC3 FG_PROP — calibration mode (0x09 → normal)
+ *
+ * NOTE: earlier revs read RVBAT/RCUR from 0x26/0x28 citing a "live 3899 mV". That voltage was
+ * real but came from the STOCK driver reading 0x04 (dmesg s2mu106_get_vbat) — 0x26/0x28 are not
+ * fuel-gauge registers at all. Corrected to the stock 0x04/0x06/0x08 map.
+ *
+ * Exposed as a power_supply "s2mu106-battery" with:
+ * POWER_SUPPLY_PROP_VOLTAGE_NOW (µV)
+ * POWER_SUPPLY_PROP_CURRENT_NOW (µA, signed)
+ * POWER_SUPPLY_PROP_CAPACITY (% int 0-100)
+ * POWER_SUPPLY_PROP_STATUS (derived from current sign + SOC)
+ * POWER_SUPPLY_PROP_PRESENT
+ * POWER_SUPPLY_PROP_TECHNOLOGY (LION)
+ */
+
+#include <linux/i2c.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/of.h>
+#include <linux/power_supply.h>
+#include <linux/regmap.h>
+
+/* Register definitions */
+#define S2MU106_FG_STATUS 0x00
+#define FG_STATUS_ACTIVE BIT(7)
+
+#define S2MU106_RVBAT_L 0x04 /* 16-bit LE: low byte first */
+#define S2MU106_RVBAT_H (S2MU106_RVBAT_L + 1)
+
+#define S2MU106_RCUR_CC_L 0x06 /* 2's-complement 16-bit LE coulomb counter */
+#define S2MU106_RCUR_CC_H (S2MU106_RCUR_CC_L + 1)
+
+#define S2MU106_RSOC_L 0x08 /* 2's-complement 16-bit LE */
+#define S2MU106_RSOC_H (S2MU106_RSOC_L + 1)
+
+/* Scaling is done with the stock fixed-point formulas inline (see get_property); the
+ * old per-LSB constants were for the wrong registers and are gone.
+ */
+
+struct s2mu106_fg {
+ struct device *dev;
+ struct regmap *regmap;
+ struct power_supply *psy;
+ struct mutex lock; /* serialises the two-register reads in get_property() */
+};
+
+static int fg_read16(struct s2mu106_fg *fg, u8 lo_reg, s32 *val, bool is_signed)
+{
+ unsigned int lo, hi;
+ int ret;
+
+ ret = regmap_read(fg->regmap, lo_reg, &lo);
+ if (ret)
+ return ret;
+ ret = regmap_read(fg->regmap, lo_reg + 1, &hi);
+ if (ret)
+ return ret;
+
+ if (is_signed)
+ *val = (s16)((hi << 8) | lo);
+ else
+ *val = (u16)((hi << 8) | lo);
+ return 0;
+}
+
+static int s2mu106_fg_get_property(struct power_supply *psy,
+ enum power_supply_property psp,
+ union power_supply_propval *val)
+{
+ struct s2mu106_fg *fg = power_supply_get_drvdata(psy);
+ s32 raw;
+ int ret = 0;
+
+ mutex_lock(&fg->lock);
+ switch (psp) {
+ case POWER_SUPPLY_PROP_VOLTAGE_NOW:
+ ret = fg_read16(fg, S2MU106_RVBAT_L, &raw, false);
+ if (!ret)
+ /* stock s2mu106_get_vbat: mV = (raw*1000)>>13; expose as µV */
+ val->intval = (((u32)raw * 1000) >> 13) * 1000;
+ break;
+ case POWER_SUPPLY_PROP_CURRENT_NOW: {
+ u32 cc, mag;
+ s32 ma;
+
+ ret = fg_read16(fg, S2MU106_RCUR_CC_L, &raw, false);
+ if (ret)
+ break;
+ cc = (u16)raw; /* RCUR_CC: 2's-complement coulomb-counter word */
+ /*
+ * stock s2mu106_get_current: bit15 set => charging (positive),
+ * clear => discharging (negative). Scale mA = (mag*1000)>>12, then
+ * mA->µA in a second step so mag*1000000 can't overflow s32.
+ */
+ if (cc & 0x8000) { /* charging */
+ mag = ((~cc) & 0xFFFF) + 1;
+ ma = (s32)((mag * 1000) >> 12);
+ } else { /* discharging */
+ mag = cc & 0x7FFF;
+ ma = -(s32)((mag * 1000) >> 12);
+ }
+ val->intval = ma * 1000; /* µA */
+ break;
+ }
+ case POWER_SUPPLY_PROP_CAPACITY: {
+ u32 soc;
+
+ ret = fg_read16(fg, S2MU106_RSOC_L, &raw, false);
+ if (ret)
+ break;
+ soc = (u16)raw;
+ /* stock s2mu106_get_rawsoc: %*100 = (raw&0x7FFF)*10000/(1<<14) */
+ if (soc & 0x8000)
+ val->intval = 0; /* negative SOC -> treat as empty */
+ else
+ val->intval = clamp_val(((soc & 0x7FFF) * 10000 / (1 << 14)) / 100,
+ 0, 100);
+ break;
+ }
+ case POWER_SUPPLY_PROP_STATUS: {
+ u32 cc;
+
+ ret = fg_read16(fg, S2MU106_RCUR_CC_L, &raw, false);
+ if (ret)
+ break;
+ cc = (u16)raw;
+ /* bit15 set = charging (stock convention); independent of the µA scaling */
+ if (cc & 0x8000)
+ val->intval = POWER_SUPPLY_STATUS_CHARGING;
+ else if ((cc & 0x7FFF) > 20) /* small deadband (~5 mA) */
+ val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
+ else
+ val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
+ break;
+ }
+ case POWER_SUPPLY_PROP_PRESENT:
+ val->intval = 1;
+ break;
+ case POWER_SUPPLY_PROP_TECHNOLOGY:
+ val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
+ break;
+ default:
+ ret = -EINVAL;
+ }
+ mutex_unlock(&fg->lock);
+ return ret;
+}
+
+static enum power_supply_property s2mu106_fg_props[] = {
+ POWER_SUPPLY_PROP_VOLTAGE_NOW,
+ POWER_SUPPLY_PROP_CURRENT_NOW,
+ POWER_SUPPLY_PROP_CAPACITY,
+ POWER_SUPPLY_PROP_STATUS,
+ POWER_SUPPLY_PROP_PRESENT,
+ POWER_SUPPLY_PROP_TECHNOLOGY,
+};
+
+static const struct power_supply_desc s2mu106_fg_desc = {
+ .name = "s2mu106-battery",
+ .type = POWER_SUPPLY_TYPE_BATTERY,
+ .properties = s2mu106_fg_props,
+ .num_properties = ARRAY_SIZE(s2mu106_fg_props),
+ .get_property = s2mu106_fg_get_property,
+};
+
+static const struct regmap_config s2mu106_fg_regmap_config = {
+ .reg_bits = 8,
+ .val_bits = 8,
+ .max_register = 0xFF,
+};
+
+static int s2mu106_fg_probe(struct i2c_client *client)
+{
+ struct s2mu106_fg *fg;
+ struct power_supply_config cfg = {};
+
+ fg = devm_kzalloc(&client->dev, sizeof(*fg), GFP_KERNEL);
+ if (!fg)
+ return -ENOMEM;
+
+ fg->dev = &client->dev;
+ mutex_init(&fg->lock);
+
+ fg->regmap = devm_regmap_init_i2c(client, &s2mu106_fg_regmap_config);
+ if (IS_ERR(fg->regmap))
+ return PTR_ERR(fg->regmap);
+
+ i2c_set_clientdata(client, fg);
+
+ cfg.drv_data = fg;
+ fg->psy = devm_power_supply_register(&client->dev, &s2mu106_fg_desc, &cfg);
+ if (IS_ERR(fg->psy))
+ return PTR_ERR(fg->psy);
+
+ dev_info(&client->dev, "S2MU106 fuel gauge registered (addr 0x%02x)\n",
+ client->addr);
+ return 0;
+}
+
+static const struct i2c_device_id s2mu106_fg_id[] = {
+ { "s2mu106-fuelgauge", 0 },
+ {}
+};
+MODULE_DEVICE_TABLE(i2c, s2mu106_fg_id);
+
+static const struct of_device_id s2mu106_fg_of_match[] = {
+ { .compatible = "samsung,s2mu106-fuelgauge" },
+ {}
+};
+MODULE_DEVICE_TABLE(of, s2mu106_fg_of_match);
+
+static struct i2c_driver s2mu106_fg_driver = {
+ .driver = {
+ .name = "s2mu106-fuelgauge",
+ .of_match_table = s2mu106_fg_of_match,
+ },
+ .probe = s2mu106_fg_probe,
+ .id_table = s2mu106_fg_id,
+};
+module_i2c_driver(s2mu106_fg_driver);
+
+MODULE_DESCRIPTION("Samsung S2MU106 Fuel Gauge driver");
+MODULE_AUTHOR("Adil Hüsrev Tepe");
+MODULE_LICENSE("GPL");
--
2.53.0
^ permalink raw reply [flat|nested] 3+ messages in thread