Skip to contentSkip to conversation

Add TI BQ27Z558 fuel gauge support

PATCH New Prachoday Sutar 1 participant 3 messages Dec 6

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

v3 cover Add TI BQ27Z558 fuel gauge support

The TI BQ27Z558 is a single-cell fuel gauge documented in the same Technical Reference Manual as the BQ27Z561-R2 (SLUUC54) and is register compatible with the BQ27Z561.

Patch 1 makes the bq27xxx I2C driver take the chip type from the OF match data via i2c_get_match_data() and use client->name for the battery name. Until now, probe always looked up the client name in the I2C ID table, so a device bound through a fallback compatible got a NULL ID and crashed.

Patch 2 adds "ti,bq27z558" to the binding with "ti,bq27z561" as the fallback. With patch 1 applied no BQ27Z558-specific driver change is needed, and the battery is still named after the exact chip ("bq27z558-N").

Testing:
- Build-tested on x86_64 with W=1, with and without CONFIG_OF.
- dt_binding_check for bq27xxx.yaml and checkpatch --strict are clean.
- Hardware: Kneron KL630-based board with a BQ27Z558, running a vendor Linux 4.9 kernel with BQ27Z561 support backported and "bq27z558" added to the driver's I2C ID and OF match tables, with only "ti,bq27z558" in the DT. Voltage, current, capacity, status and temperature read correctly. This shows that the BQ27Z558 works with the BQ27Z561 register map; it did not exercise this series or the fallback compatible.

Changes in v3:
- Keep "ti,bq27z561" as the fallback compatible (Sebastian).
- New patch 1: add the chip type as .data to all OF match entries and use i2c_get_match_data() and client->name instead of i2c_client_get_device_id() (suggested by Andrew).
- Drop the v2 patch adding a "bq27z558" I2C device ID; it is no longer needed after patch 1.
- Drop the separate "Add BQ27Z558 support" driver patch planned in the v2 discussion, for the same reason.
- Put the driver change first, so the fallback binding never exists without a driver that can handle it.
- Correct the testing description. The v1 and v2 cover letters wrongly said the fallback compatible was tested.

Changes in v2:
- Add a patch with a "bq27z558" I2C device ID. Binding via the fallback compatible alone left i2c_client_get_device_id() returning NULL in probe (reported by the Sashiko AI review).
- Binding patch: drop the claim that no driver changes are needed.

Link to v2: https://lore.kernel.org/r/[email protected]
Link to v1: https://lore.kernel.org/r/[email protected]

Prachoday Sutar (2):
power: supply: bq27xxx: Use i2c_get_match_data()
dt-bindings: power: supply: bq27xxx: Add BQ27Z558

 .../bindings/power/supply/bq27xxx.yaml        | 72 +++++++++---------
 drivers/power/supply/bq27xxx_battery_i2c.c    | 76 ++++++++++---------
 2 files changed, 78 insertions(+), 70 deletions(-)

base-commit: 7778cfebeb93187a47251df6ad3fbbb5709e6786

Signature · 1 line
2.25.1
#1 lore raw

PATCH v3 1/2 power: supply: bq27xxx: Use i2c_get_match_data()

bq27xxx_battery_i2c_probe() gets the chip type and the battery name from i2c_client_get_device_id(), which looks up the client name in the I2C ID table. The OF match table has no .data, so even when the device is matched through a DT compatible, the chip type comes from the I2C ID table.

This only works as long as the two tables are kept in sync and the client name, which is taken from the first DT compatible, has an entry in the I2C ID table. A device described with a fallback compatible, where only the fallback is known to the driver, gets a NULL ID and probe dereferences it.

Add the chip type as .data to every OF match entry and get it with i2c_get_match_data(), which uses the OF match data and falls back to the I2C ID table. Use client->name for the battery name. For every compatible in the OF match table, client->name is the same as the matching I2C ID name, so the names seen by userspace do not change.

---
 drivers/power/supply/bq27xxx_battery_i2c.c | 76 ++++++++++++----------
 1 file changed, 40 insertions(+), 36 deletions(-)
drivers/power/supply/bq27xxx_battery_i2c.c +40 -36
index 63bfc868a6d..8e54dbfee2d 100644
@@ -157,12 +157,16 @@ static void bq27xxx_battery_i2c_devm_ida_free(void *data)
 
 static int bq27xxx_battery_i2c_probe(struct i2c_client *client)
 {
-	const struct i2c_device_id *id = i2c_client_get_device_id(client);
 	struct bq27xxx_device_info *di;
+	enum bq27xxx_chip chip;
 	int ret;
 	char *name;
 	long num;
 
+	chip = (uintptr_t)i2c_get_match_data(client);
+	if (!chip)
+		return -ENODEV;
+
 	/* Get new ID for the new battery device */
 	num = ida_alloc(&battery_id, GFP_KERNEL);
 	if (num < 0)
@@ -173,7 +177,7 @@ static int bq27xxx_battery_i2c_probe(struct i2c_client *client)
 	if (ret)
 		return ret;
 
-	name = devm_kasprintf(&client->dev, GFP_KERNEL, "%s-%ld", id->name, num);
+	name = devm_kasprintf(&client->dev, GFP_KERNEL, "%s-%ld", client->name, num);
 	if (!name)
 		return -ENOMEM;
 
@@ -182,7 +186,7 @@ static int bq27xxx_battery_i2c_probe(struct i2c_client *client)
 		return -ENOMEM;
 
 	di->dev = &client->dev;
-	di->chip = id->driver_data;
+	di->chip = chip;
 	di->name = name;
 
 	di->bus.read = bq27xxx_battery_i2c_read;
@@ -266,39 +270,39 @@ 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,bq27200" },
-	{ .compatible = "ti,bq27210" },
-	{ .compatible = "ti,bq27500" },
-	{ .compatible = "ti,bq27510" },
-	{ .compatible = "ti,bq27520" },
-	{ .compatible = "ti,bq27500-1" },
-	{ .compatible = "ti,bq27510g1" },
-	{ .compatible = "ti,bq27510g2" },
-	{ .compatible = "ti,bq27510g3" },
-	{ .compatible = "ti,bq27520g1" },
-	{ .compatible = "ti,bq27520g2" },
-	{ .compatible = "ti,bq27520g3" },
-	{ .compatible = "ti,bq27520g4" },
-	{ .compatible = "ti,bq27521" },
-	{ .compatible = "ti,bq27530" },
-	{ .compatible = "ti,bq27531" },
-	{ .compatible = "ti,bq27541" },
-	{ .compatible = "ti,bq27542" },
-	{ .compatible = "ti,bq27546" },
-	{ .compatible = "ti,bq27742" },
-	{ .compatible = "ti,bq27545" },
-	{ .compatible = "ti,bq27411" },
-	{ .compatible = "ti,bq27421" },
-	{ .compatible = "ti,bq27425" },
-	{ .compatible = "ti,bq27426" },
-	{ .compatible = "ti,bq27441" },
-	{ .compatible = "ti,bq27621" },
-	{ .compatible = "ti,bq27z561" },
-	{ .compatible = "ti,bq27z746" },
-	{ .compatible = "ti,bq28z610" },
-	{ .compatible = "ti,bq28z620" },
-	{ .compatible = "ti,bq34z100" },
-	{ .compatible = "ti,bq78z100" },
+	{ .compatible = "ti,bq27200", .data = (void *)BQ27000 },
+	{ .compatible = "ti,bq27210", .data = (void *)BQ27010 },
+	{ .compatible = "ti,bq27500", .data = (void *)BQ2750X },
+	{ .compatible = "ti,bq27510", .data = (void *)BQ2751X },
+	{ .compatible = "ti,bq27520", .data = (void *)BQ2752X },
+	{ .compatible = "ti,bq27500-1", .data = (void *)BQ27500 },
+	{ .compatible = "ti,bq27510g1", .data = (void *)BQ27510G1 },
+	{ .compatible = "ti,bq27510g2", .data = (void *)BQ27510G2 },
+	{ .compatible = "ti,bq27510g3", .data = (void *)BQ27510G3 },
+	{ .compatible = "ti,bq27520g1", .data = (void *)BQ27520G1 },
+	{ .compatible = "ti,bq27520g2", .data = (void *)BQ27520G2 },
+	{ .compatible = "ti,bq27520g3", .data = (void *)BQ27520G3 },
+	{ .compatible = "ti,bq27520g4", .data = (void *)BQ27520G4 },
+	{ .compatible = "ti,bq27521", .data = (void *)BQ27521 },
+	{ .compatible = "ti,bq27530", .data = (void *)BQ27530 },
+	{ .compatible = "ti,bq27531", .data = (void *)BQ27531 },
+	{ .compatible = "ti,bq27541", .data = (void *)BQ27541 },
+	{ .compatible = "ti,bq27542", .data = (void *)BQ27542 },
+	{ .compatible = "ti,bq27546", .data = (void *)BQ27546 },
+	{ .compatible = "ti,bq27742", .data = (void *)BQ27742 },
+	{ .compatible = "ti,bq27545", .data = (void *)BQ27545 },
+	{ .compatible = "ti,bq27411", .data = (void *)BQ27411 },
+	{ .compatible = "ti,bq27421", .data = (void *)BQ27421 },
+	{ .compatible = "ti,bq27425", .data = (void *)BQ27425 },
+	{ .compatible = "ti,bq27426", .data = (void *)BQ27426 },
+	{ .compatible = "ti,bq27441", .data = (void *)BQ27441 },
+	{ .compatible = "ti,bq27621", .data = (void *)BQ27621 },
+	{ .compatible = "ti,bq27z561", .data = (void *)BQ27Z561 },
+	{ .compatible = "ti,bq27z746", .data = (void *)BQ27Z746 },
+	{ .compatible = "ti,bq28z610", .data = (void *)BQ28Z610 },
+	{ .compatible = "ti,bq28z620", .data = (void *)BQ28Z620 },
+	{ .compatible = "ti,bq34z100", .data = (void *)BQ34Z100 },
+	{ .compatible = "ti,bq78z100", .data = (void *)BQ78Z100 },
 	{},
 };
 MODULE_DEVICE_TABLE(of, bq27xxx_battery_i2c_of_match_table);
Signature · 1 line
2.25.1
#2 lore raw

PATCH v3 2/2 dt-bindings: power: supply: bq27xxx: Add BQ27Z558

In reply to Prachoday Sutar

The TI BQ27Z558 is a single-cell fuel gauge documented in the same Technical Reference Manual as the BQ27Z561-R2 (SLUUC54). It uses the same standard command set and register layout as the BQ27Z561.

Add a compatible for the BQ27Z558 with "ti,bq27z561" as the fallback.

---
 .../bindings/power/supply/bq27xxx.yaml        | 72 ++++++++++---------
 1 file changed, 38 insertions(+), 34 deletions(-)
Documentation/devicetree/bindings/power/supply/bq27xxx.yaml +38 -34
index be0f1f7292c..28b04732e08 100644
@@ -18,40 +18,44 @@ description: |
 
 properties:
   compatible:
-    enum:
-      - ti,bq27200
-      - ti,bq27210
-      - ti,bq27500 # deprecated, use revision specific property below
-      - ti,bq27510 # deprecated, use revision specific property below
-      - ti,bq27520 # deprecated, use revision specific property below
-      - ti,bq27500-1
-      - ti,bq27510g1
-      - ti,bq27510g2
-      - ti,bq27510g3
-      - ti,bq27520g1
-      - ti,bq27520g2
-      - ti,bq27520g3
-      - ti,bq27520g4
-      - ti,bq27521
-      - ti,bq27530
-      - ti,bq27531
-      - ti,bq27541
-      - ti,bq27542
-      - ti,bq27546
-      - ti,bq27742
-      - ti,bq27545
-      - ti,bq27411
-      - ti,bq27421
-      - ti,bq27425
-      - ti,bq27426
-      - ti,bq27441
-      - ti,bq27621
-      - ti,bq27z561
-      - ti,bq27z746
-      - ti,bq28z610
-      - ti,bq28z620
-      - ti,bq34z100
-      - ti,bq78z100
+    oneOf:
+      - enum:
+          - ti,bq27200
+          - ti,bq27210
+          - ti,bq27500 # deprecated, use revision specific property below
+          - ti,bq27510 # deprecated, use revision specific property below
+          - ti,bq27520 # deprecated, use revision specific property below
+          - ti,bq27500-1
+          - ti,bq27510g1
+          - ti,bq27510g2
+          - ti,bq27510g3
+          - ti,bq27520g1
+          - ti,bq27520g2
+          - ti,bq27520g3
+          - ti,bq27520g4
+          - ti,bq27521
+          - ti,bq27530
+          - ti,bq27531
+          - ti,bq27541
+          - ti,bq27542
+          - ti,bq27546
+          - ti,bq27742
+          - ti,bq27545
+          - ti,bq27411
+          - ti,bq27421
+          - ti,bq27425
+          - ti,bq27426
+          - ti,bq27441
+          - ti,bq27621
+          - ti,bq27z561
+          - ti,bq27z746
+          - ti,bq28z610
+          - ti,bq28z620
+          - ti,bq34z100
+          - ti,bq78z100
+      - items:
+          - const: ti,bq27z558
+          - const: ti,bq27z561
 
   reg:
     maxItems: 1
Signature · 1 line
2.25.1