Re: [PATCH v3 2/3] power: supply: bq27xxx: autodetect bq27411 and bq27541

From: David Heidelberg

Date: Wed Sep 09 2026 - 20:12:13 EST


On 10/09/2026 01:47, Rinat Muhamedgaliev wrote:
Some replaceable battery packs used by OnePlus 6 and OnePlus 6T
phones contain a bq27411, while others contain a bq27541. These
devices require different register maps.

Add support for the generic ti,bq27xxx compatible. At probe time,
issue the DeviceType control command and select the bq27411 or bq27541
register map from the returned value.

Signed-off-by: Rinat Muhamedgaliev <rinat.muhamedgaliev@xxxxxxxxx>
---
drivers/power/supply/bq27xxx_battery_i2c.c | 39 ++++++++++++++++++++++
1 file changed, 39 insertions(+)

diff --git a/drivers/power/supply/bq27xxx_battery_i2c.c b/drivers/power/supply/bq27xxx_battery_i2c.c
index 868e95f..718f8e8 100644
--- a/drivers/power/supply/bq27xxx_battery_i2c.c
+++ b/drivers/power/supply/bq27xxx_battery_i2c.c
@@ -16,6 +16,11 @@
static DEFINE_IDA(battery_id);
+#define BQ27XXX_REG_CTRL 0x00
+#define BQ27XXX_DEVICE_TYPE 0x0001
+#define BQ27411_DEVICE_TYPE 0x0421
+#define BQ27541_DEVICE_TYPE 0x0541
+
static irqreturn_t bq27xxx_battery_irq_handler_thread(int irq, void *data)
{
struct bq27xxx_device_info *di = data;
@@ -153,6 +158,32 @@ static void bq27xxx_battery_i2c_devm_ida_free(void *data)
ida_free(&battery_id, num);
}
+static int bq27xxx_battery_i2c_detect_chip(struct bq27xxx_device_info *di)
+{
+ int ret;
+
+ ret = di->bus.write(di, BQ27XXX_REG_CTRL, BQ27XXX_DEVICE_TYPE,
+ false);
+ if (ret < 0)
+ return ret;
+
+ ret = di->bus.read(di, BQ27XXX_REG_CTRL, false);
+ if (ret < 0)
+ return ret;
+
+ switch (ret) {
+ case BQ27411_DEVICE_TYPE:
+ di->chip = BQ27411;
+ return 0;
+ case BQ27541_DEVICE_TYPE:
+ di->chip = BQ27541;
+ return 0;
+ default:
+ dev_err(di->dev, "unsupported device type 0x%04x\n", ret);
+ return -ENODEV;
+ }
+}
+
static int bq27xxx_battery_i2c_probe(struct i2c_client *client)
{
const struct i2c_device_id *id = i2c_client_get_device_id(client);
@@ -188,6 +219,12 @@ static int bq27xxx_battery_i2c_probe(struct i2c_client *client)
di->bus.read_bulk = bq27xxx_battery_i2c_bulk_read;
di->bus.write_bulk = bq27xxx_battery_i2c_bulk_write;
+ if (!di->chip) {
+ ret = bq27xxx_battery_i2c_detect_chip(di);
+ if (ret)
+ return ret;
+ }
+
ret = bq27xxx_battery_setup(di);
if (ret)
return ret;
@@ -225,6 +262,7 @@ static void bq27xxx_battery_i2c_remove(struct i2c_client *client)
}
static const struct i2c_device_id bq27xxx_i2c_id_table[] = {
+ { "bq27xxx", 0 },
{ "bq27200", BQ27000 },
{ "bq27210", BQ27010 },
{ "bq27500", BQ2750X },
@@ -262,6 +300,7 @@ MODULE_DEVICE_TABLE(i2c, bq27xxx_i2c_id_table);
#ifdef CONFIG_OF
static const struct of_device_id bq27xxx_battery_i2c_of_match_table[] = {
+ { .compatible = "ti,bq27xxx" },

Maybe some comment suggesting to use this compatible when autodetection is needed?

David

{ .compatible = "ti,bq27200" },
{ .compatible = "ti,bq27210" },
{ .compatible = "ti,bq27500" },