Skip to contentSkip to conversation

Add TI BQ27Z558 fuel gauge support

PATCH Superseded Prachoday Sutar 3 participants 7 messages Nov 26 – Dec 6

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 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 adds the compatible to the binding with "ti,bq27z561" as the fallback. Patch 2 adds a matching I2C device ID so the driver probes correctly when the client is named after the first compatible.

Tested on a Kneron KL630-based board with a BQ27Z558, using a vendor Linux 4.9 kernel with the equivalent bq27xxx changes and the fallback compatible in DT. Voltage, current, capacity, status and temperature read correctly through /sys/class/power_supply.

Changes in v2:
- Add patch 2. Binding via the fallback compatible alone left i2c_client_get_device_id() returning NULL in probe, causing a NULL pointer dereference (reported by the Sashiko AI review).
- Patch 1: drop the claim from the commit message that no driver changes are needed.

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

Prachoday Sutar (2):
dt-bindings: power: supply: bq27xxx: Add BQ27Z558
power: supply: bq27xxx: Add BQ27Z558 I2C device ID

 .../bindings/power/supply/bq27xxx.yaml        | 72 ++++++++++---------
 drivers/power/supply/bq27xxx_battery_i2c.c    |  1 +
 2 files changed, 39 insertions(+), 34 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

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

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 be0f1f729..28b04732e 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.53.0
#2 lore raw

PATCH v2 2/2 power: supply: bq27xxx: Add BQ27Z558 I2C device ID

In reply to Prachoday Sutar

The BQ27Z558 is register compatible with the BQ27Z561 and is described in DT with "ti,bq27z561" as a fallback compatible.

When an I2C client is instantiated from DT, its name is taken from the first compatible string, so the client is named "bq27z558". bq27xxx_battery_i2c_probe() gets the chip type from i2c_client_get_device_id(), which matches on the client name. Without a matching entry in the I2C ID table it returns NULL, and probe then dereferences it.

Add a "bq27z558" entry to the I2C ID table that maps to BQ27Z561.

---
 drivers/power/supply/bq27xxx_battery_i2c.c | 1 +
 1 file changed, 1 insertion(+)
drivers/power/supply/bq27xxx_battery_i2c.c +1 -0
index bd1d5d046..6c0cea193 100644
@@ -252,6 +252,7 @@ static const struct i2c_device_id bq27xxx_i2c_id_table[] = {
 	{ .name = "bq27426", .driver_data = BQ27426 },
 	{ .name = "bq27441", .driver_data = BQ27441 },
 	{ .name = "bq27621", .driver_data = BQ27621 },
+	{ .name = "bq27z558", .driver_data = BQ27Z561 },
 	{ .name = "bq27z561", .driver_data = BQ27Z561 },
 	{ .name = "bq27z746", .driver_data = BQ27Z746 },
 	{ .name = "bq28z610", .driver_data = BQ28Z610 },
Signature · 1 line
2.53.0
#3 lore raw
In reply to Prachoday Sutar
Quoted 13 lines · Prachoday Sutar

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 adds the compatible to the binding with "ti,bq27z561" as the fallback. Patch 2 adds a matching I2C device ID so the driver probes correctly when the client is named after the first compatible.

Tested on a Kneron KL630-based board with a BQ27Z558, using a vendor Linux 4.9 kernel with the equivalent bq27xxx changes and the fallback compatible in DT. Voltage, current, capacity, status and temperature read correctly through /sys/class/power_supply.

Quoted 4 lines

Changes in v2:
- Add patch 2. Binding via the fallback compatible alone left i2c_client_get_device_id() returning NULL in probe, causing a NULL pointer dereference (reported by the Sashiko AI review).

I thought you said you tested this with the fallback? And Sashiko says fallback will cause a NULL dereference, one of these must not be true then..

I can see what Sashiko might be talking about here. I think a couple things need fixed.

First, the bq27xxx_battery_i2c_of_match_table and the bq27xxx_i2c_id_table are out of sync, so we match with OF compatible, but use the I2C ID table to get the mach data. The OF match data is currently empty, that needs updated.

Second, i2c_client_get_device_id() is only used as we need to fetch the name, but we only use the name in request_threaded_irq(), and we could use client->name instead. Then for getting the match data use i2c_get_match_data().

Third, I'd just skip the fallback stuff. If you have to modify the driver anyway, just add a normal compatible and use it in your DT.

Andrew

Quoted 13 lines

- Patch 1: drop the claim from the commit message that no driver changes are needed.

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

Prachoday Sutar (2):
dt-bindings: power: supply: bq27xxx: Add BQ27Z558
power: supply: bq27xxx: Add BQ27Z558 I2C device ID

  .../bindings/power/supply/bq27xxx.yaml        | 72 ++++++++++---------
  drivers/power/supply/bq27xxx_battery_i2c.c    |  1 +
  2 files changed, 39 insertions(+), 34 deletions(-)
#4 lore raw

Hi Andrew,

Thanks for the review.

Quoted 3 lines · Andrew Davis

I thought you said you tested this with the fallback? And Sashiko says fallback will cause a NULL dereference, one of these must not be true then..

You're right, the test note was wrong, sorry about that. The testing was done on a vendor 4.9 kernel with a local backport of BQ27Z561 support, and the board DT used only "ti,bq27z558", not the fallback. In that tree "bq27z558" was in both the I2C ID table and the OF match table, so the I2C ID lookup succeeded and the fallback-only path that Sashiko flagged was never exercised. So that test only shows that the BQ27Z558 works with the BQ27Z561 register map and status bits; it did not run the mainline driver. I'll describe the testing accurately in v3.

Quoted 4 lines

First, the bq27xxx_battery_i2c_of_match_table and the bq27xxx_i2c_id_table are out of sync, so we match with OF compatible, but use the I2C ID table to get the mach data. The OF match data is currently empty, that needs updated.

Agreed. In v3 I'll add the chip type as .data to every entry in the OF match table, so the two tables no longer need to be kept in sync by hand.

Quoted 4 lines

Second, i2c_client_get_device_id() is only used as we need to fetch the name, but we only use the name in request_threaded_irq(), and we could use client->name instead. Then for getting the match data use i2c_get_match_data().

Will do. For all existing compatibles client->name is the same as the matched I2C ID name, so the battery names seen by userspace don't change.

Quoted 2 lines

Third, I'd just skip the fallback stuff. If you have to modify the driver anyway, just add a normal compatible and use it in your DT.

OK, I'll drop the fallback and add "ti,bq27z558" as a plain compatible, which also matches how it was set up in the tree I tested. One question first: once the OF match data is in place, the fallback would work without any BQ27Z558-specific driver change. If the DT binding maintainers would prefer to keep the fallback for that reason, I'm happy to do that instead. Otherwise I'll go with the plain compatible.

Planned v3:
1/3 dt-bindings: power: supply: bq27xxx: Add BQ27Z558 2/3 power: supply: bq27xxx: Use i2c_get_match_data() 3/3 power: supply: bq27xxx: Add BQ27Z558 support

Thanks,
Prachoday

#5 lore raw

Hi,

Quoted 33 lines · Prachoday Sutar
Quoted 3 lines · Andrew Davis

I thought you said you tested this with the fallback? And Sashiko says fallback will cause a NULL dereference, one of these must not be true then..

You're right, the test note was wrong, sorry about that. The testing was done on a vendor 4.9 kernel with a local backport of BQ27Z561 support, and the board DT used only "ti,bq27z558", not the fallback. In that tree "bq27z558" was in both the I2C ID table and the OF match table, so the I2C ID lookup succeeded and the fallback-only path that Sashiko flagged was never exercised. So that test only shows that the BQ27Z558 works with the BQ27Z561 register map and status bits; it did not run the mainline driver. I'll describe the testing accurately in v3.

Quoted 4 lines

First, the bq27xxx_battery_i2c_of_match_table and the bq27xxx_i2c_id_table are out of sync, so we match with OF compatible, but use the I2C ID table to get the mach data. The OF match data is currently empty, that needs updated.

Agreed. In v3 I'll add the chip type as .data to every entry in the OF match table, so the two tables no longer need to be kept in sync by hand.

Quoted 4 lines

Second, i2c_client_get_device_id() is only used as we need to fetch the name, but we only use the name in request_threaded_irq(), and we could use client->name instead. Then for getting the match data use i2c_get_match_data().

Will do. For all existing compatibles client->name is the same as the matched I2C ID name, so the battery names seen by userspace don't change.

Quoted 5 lines
Quoted 2 lines

Third, I'd just skip the fallback stuff. If you have to modify the driver anyway, just add a normal compatible and use it in your DT.

OK, I'll drop the fallback and add "ti,bq27z558" as a plain compatible, which also matches how it was set up in the tree I tested.

The fallback is the correct thing to do. The binding is not just for the Linux driver and the only reason the Linux driver needs the specific entry is the exact chip name.

Quoted 7 lines

One question first: once the OF match data is in place, the fallback would work without any BQ27Z558-specific driver change. If the DT binding maintainers would prefer to keep the fallback for that reason, I'm happy to do that instead. Otherwise I'll go with the plain compatible.

Quoted 7 lines

Planned v3:
1/3 dt-bindings: power: supply: bq27xxx: Add BQ27Z558 2/3 power: supply: bq27xxx: Use i2c_get_match_data() 3/3 power: supply: bq27xxx: Add BQ27Z558 support

Thanks,
Prachoday

Greetings,

-- Sebastian

#6 lore raw

Thanks Sebastian, Andrew. v3 keeps the fallback and moves the chip type into the OF match data:
https://lore.kernel.org/r/[email protected]

Quoted 65 lines · Sebastian Reichel

Hi,

Quoted 33 lines · Prachoday Sutar
Quoted 3 lines · Andrew Davis

I thought you said you tested this with the fallback? And Sashiko says fallback will cause a NULL dereference, one of these must not be true then..

You're right, the test note was wrong, sorry about that. The testing was done on a vendor 4.9 kernel with a local backport of BQ27Z561 support, and the board DT used only "ti,bq27z558", not the fallback. In that tree "bq27z558" was in both the I2C ID table and the OF match table, so the I2C ID lookup succeeded and the fallback-only path that Sashiko flagged was never exercised. So that test only shows that the BQ27Z558 works with the BQ27Z561 register map and status bits; it did not run the mainline driver. I'll describe the testing accurately in v3.

Quoted 4 lines

First, the bq27xxx_battery_i2c_of_match_table and the bq27xxx_i2c_id_table are out of sync, so we match with OF compatible, but use the I2C ID table to get the mach data. The OF match data is currently empty, that needs updated.

Agreed. In v3 I'll add the chip type as .data to every entry in the OF match table, so the two tables no longer need to be kept in sync by hand.

Quoted 4 lines

Second, i2c_client_get_device_id() is only used as we need to fetch the name, but we only use the name in request_threaded_irq(), and we could use client->name instead. Then for getting the match data use i2c_get_match_data().

Will do. For all existing compatibles client->name is the same as the matched I2C ID name, so the battery names seen by userspace don't change.

Quoted 5 lines
Quoted 2 lines

Third, I'd just skip the fallback stuff. If you have to modify the driver anyway, just add a normal compatible and use it in your DT.

OK, I'll drop the fallback and add "ti,bq27z558" as a plain compatible, which also matches how it was set up in the tree I tested.

The fallback is the correct thing to do. The binding is not just for the Linux driver and the only reason the Linux driver needs the specific entry is the exact chip name.

Quoted 7 lines

One question first: once the OF match data is in place, the fallback would work without any BQ27Z558-specific driver change. If the DT binding maintainers would prefer to keep the fallback for that reason, I'm happy to do that instead. Otherwise I'll go with the plain compatible.

Quoted 7 lines

Planned v3:
1/3 dt-bindings: power: supply: bq27xxx: Add BQ27Z558 2/3 power: supply: bq27xxx: Use i2c_get_match_data() 3/3 power: supply: bq27xxx: Add BQ27Z558 support

Thanks,
Prachoday

Greetings,

-- Sebastian