Skip to contentSkip to conversation

power: supply: add support for MAX1720x standalone fuel

PATCH Superseded Dimitri Fedrau 3 participants 9 messages Jun 17, 2024 – Jun 19, 2024

Subsystems (2)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover power: supply: add support for MAX1720x standalone fuel

Changes to max1721x_battery.c:
- reading manufacturer, model name and serial number is only possible when SBS functions of the IC are enabled.(nNVCfg0.enSBS) Factory default is off. Manufacturer is "Maxim Integrated" and the model name can be derived by register MAX172XX_DEV_NAME. Serial number is not available anymore.
- According to the datasheet MAX172XX_BAT_PRESENT is at BIT(3) not BIT(4). Furthermore the naming is misleading, when BIT(3) is set the battery is not present.
- Removed DeviceName, ManufacturerName and SerialNumber from struct

    max17211_device_info

Changes in V2:
- Changed E-Mail in Patch (2/2) Signed-Off

Changes in V3:
- Changed E-Mail in Patch (2/2) Author

Changes in V4:
- add compatibles "maxim,max17201, "maxim,max17205" in bindings
- use generic node name fuel-gauge@36 instead of max17201@36 in bindings
- remove status in bindings
- fix spelling mistakes in commit message
- fix indentation in Kconfig
- fix typos in max1720x_battery.c
- Drop bat and bat_desc from info struct.
- MAX172XX_DEV_NAME and MAX172XX_DESIGN_CAP aren't volatile, adjust regmap
- constify max1720x_manufacturer, max17201_model, max17205_model
- constify max1720x_battery_props
- Remove braces around reg in max172xx_current_to_voltage
- Skip initialization of reg_val in max1720x_battery_get_property
- Remove braces around FIELD_GET() in max1720x_battery_get_property
- In case POWER_SUPPLY_PROP_PRESENT there is an early return if ret < 0. Return 0 if regmap_read fails, device is not responding in case battery is not inserted
- Implement multi-byte readings instead of i2c_smbus_read_word_data
- Drop ancillary from info
- Drop both calls to i2c_set_clientdata in max1720x_probe
- Get rid of max1720x_remove
- Remove comma after sentinel in max1720x_of_match
- Fix alignment of max1720x_i2c_driver
- Fix return value of dev_err_probe after max1720x_probe_sense_resistor

    to use ret instead of PTR_ERR(info->bat)

Dimitri Fedrau (2):
dt-bindings: power: supply: add support for MAX17201/MAX17205 fuel
gauge
power: supply: add support for MAX1720x standalone fuel gauge

 .../bindings/power/supply/maxim,max1720x.yaml |  55 +++
 drivers/power/supply/Kconfig                  |  12 +
 drivers/power/supply/Makefile                 |   1 +
 drivers/power/supply/max1720x_battery.c       | 363 ++++++++++++++++++
 4 files changed, 431 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/power/supply/maxim,max1720x.yaml
 create mode 100644 drivers/power/supply/max1720x_battery.c
Signature · 1 line
2.39.2
#1 lore raw

PATCH v4 1/2 dt-bindings: power: supply: add support for MAX17201/MAX17205 fuel gauge

Adding documentation for MAXIMs MAX17201/MAX17205 fuel gauge.

---
 .../bindings/power/supply/maxim,max1720x.yaml | 55 +++++++++++++++++++
 1 file changed, 55 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/power/supply/maxim,max1720x.yaml
Documentation/devicetree/bindings/power/supply/maxim,max1720x.yaml new file +55 -0
new file mode 100644
index 000000000000..52467af5388a
@@ -0,0 +1,55 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/power/supply/maxim,max1720x.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Maxim MAX1720x fuel gauge
+
+maintainers:
+  - Dimitri Fedrau <[email protected]>
+
+allOf:
+  - $ref: power-supply.yaml#
+
+properties:
+  compatible:
+    enum:
+      - maxim,max17201
+      - maxim,max17205
+
+  reg:
+    items:
+      - description: ModelGauge m5 registers
+      - description: Nonvolatile registers
+
+  reg-names:
+    items:
+      - const: m5
+      - const: nvmem
+
+  interrupts:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - reg-names
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    i2c {
+      #address-cells = <1>;
+      #size-cells = <0>;
+
+      fuel-gauge@36 {
+        compatible = "maxim,max17201";
+        reg = <0x36>, <0xb>;
+        reg-names = "m5", "nvmem";
+        interrupt-parent = <&gpio0>;
+        interrupts = <31 IRQ_TYPE_LEVEL_LOW>;
+      };
+    };
Signature · 1 line
2.39.2
#2 lore raw

Re: [PATCH v4 1/2] dt-bindings: power: supply: add support for MAX17201/MAX17205 fuel gauge

Quoted 23 lines · Dimitri Fedrau

Adding documentation for MAXIMs MAX17201/MAX17205 fuel gauge.

---
 .../bindings/power/supply/maxim,max1720x.yaml | 55 +++++++++++++++++++
 1 file changed, 55 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/power/supply/maxim,max1720x.yaml
Documentation/devicetree/bindings/power/supply/maxim,max1720x.yaml new file +9 -0
new file mode 100644
index 000000000000..52467af5388a
@@ -0,0 +1,55 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/power/supply/maxim,max1720x.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Maxim MAX1720x fuel gauge
+
+maintainers:

Documentation/devicetree/bindings/power/supply/maxim,max1720x.yaml line 19

Quoted 10 lines
Documentation/devicetree/bindings/power/supply/maxim,max1720x.yaml new file +10 -0
+  - Dimitri Fedrau <[email protected]>
+
+allOf:
+  - $ref: power-supply.yaml#
+
+properties:
+  compatible:
+    enum:
+      - maxim,max17201
+      - maxim,max17205

Your driver suggests these are compatible, so express it in the bindings (oneOf with list and fallback).

https://elixir.bootlin.com/linux/v6.3-rc6/source/Documentation/devicetree/bindings/sound/nvidia,tegra210-ope.yaml#L31

Best regards,
Krzysztof

#3 lore raw

Re: [PATCH v4 1/2] dt-bindings: power: supply: add support for MAX17201/MAX17205 fuel gauge

In reply to Dimitri Fedrau

Hi,

Quoted 27 lines · Dimitri Fedrau

Adding documentation for MAXIMs MAX17201/MAX17205 fuel gauge.

---
 .../bindings/power/supply/maxim,max1720x.yaml | 55 +++++++++++++++++++
 1 file changed, 55 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/power/supply/maxim,max1720x.yaml
Documentation/devicetree/bindings/power/supply/maxim,max1720x.yaml new file +13 -0
new file mode 100644
index 000000000000..52467af5388a
@@ -0,0 +1,55 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/power/supply/maxim,max1720x.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Maxim MAX1720x fuel gauge
+
+maintainers:
+  - Dimitri Fedrau <[email protected]>
+
+allOf:
+  - $ref: power-supply.yaml#

[...]

Quoted 1 line

+additionalProperties: false

this needs to be "unevaluatedProperties: false", otherwise the properties from power-supply.yaml won't be allowed.

Greetings,

-- Sebastian

#4 lore raw

PATCH v4 2/2 power: supply: add support for MAX1720x standalone fuel gauge

In reply to Dimitri Fedrau

The MAX17201 monitors a single cell pack. The MAX17205 monitors and balances a 2S or 3S pack or monitors a multiple-series cell pack. Both devices use a I2C interface.

---
 drivers/power/supply/Kconfig            |  12 +
 drivers/power/supply/Makefile           |   1 +
 drivers/power/supply/max1720x_battery.c | 363 ++++++++++++++++++++++++
 3 files changed, 376 insertions(+)
 create mode 100644 drivers/power/supply/max1720x_battery.c
drivers/power/supply/Kconfig +12 -0
index 3e31375491d5..9294520b64a3 100644
@@ -402,6 +402,18 @@ config BATTERY_MAX17042
 
 	  Driver can be build as a module (max17042_battery).
 
+config BATTERY_MAX1720X
+	tristate "Maxim MAX17201/MAX17205 Fuel Gauge"
+	depends on I2C
+	select REGMAP_I2C
+	help
+	  MAX1720x is a family of fuel-gauge systems for lithium-ion (Li+)
+	  batteries in handheld and portable equipment. MAX17201 are
+	  configured to operate with a single lithium cell, the MAX17205
+	  can operate with multiple cells.
+
+	  Say Y to include support for the MAX17201/MAX17205 Fuel Gauges.
+
 config BATTERY_MAX1721X
 	tristate "MAX17211/MAX17215 standalone gas-gauge"
 	depends on W1
drivers/power/supply/Makefile +1 -0
index 58b567278034..6f02d0248826 100644
@@ -52,6 +52,7 @@ obj-$(CONFIG_CHARGER_DA9150)	+= da9150-charger.o
 obj-$(CONFIG_BATTERY_DA9150)	+= da9150-fg.o
 obj-$(CONFIG_BATTERY_MAX17040)	+= max17040_battery.o
 obj-$(CONFIG_BATTERY_MAX17042)	+= max17042_battery.o
+obj-$(CONFIG_BATTERY_MAX1720X)	+= max1720x_battery.o
 obj-$(CONFIG_BATTERY_MAX1721X)	+= max1721x_battery.o
 obj-$(CONFIG_BATTERY_RT5033)	+= rt5033_battery.o
 obj-$(CONFIG_CHARGER_RT5033)	+= rt5033_charger.o
drivers/power/supply/max1720x_battery.c new file +363 -0

363 lines not on this page · raw message

Signature · 1 line
2.39.2
#5 lore raw

Re: [PATCH v4 2/2] power: supply: add support for MAX1720x standalone fuel gauge

Hi,

Quoted 6 lines · Dimitri Fedrau

The MAX17201 monitors a single cell pack. The MAX17205 monitors and balances a 2S or 3S pack or monitors a multiple-series cell pack. Both devices use a I2C interface.

---

[...]

Quoted 15 lines
drivers/power/supply/max1720x_battery.c new file +15 -0
+static int max1720x_read_word_data_nvmem(struct i2c_client *ancillary, u8 addr)
+{
+	u8 rx[2];
+	struct i2c_msg msgs[] = {
+		{
+			.addr = ancillary->addr,
+			.flags = 0,
+			.len = 1,
+			.buf = &addr,
+		},
+		{
+			.addr = ancillary->addr,
+			.flags = I2C_M_RD,
+			.len = 2,
+			.buf = &rx[0],

drivers/power/supply/max1720x_battery.c line 275

Quoted 10 lines
drivers/power/supply/max1720x_battery.c new file +10 -0
+		},
+	};
+	int ret;
+
+	ret = i2c_transfer(ancillary->adapter, msgs, ARRAY_SIZE(msgs));
+	if (ret != ARRAY_SIZE(msgs))
+		return ret < 0 ? ret : -EIO;
+
+	return get_unaligned_le16(&rx[0]);
+}

Have you tried using i2c_smbus_read_word_data(ancillary, addr) instead of the above?

[...]

drivers/power/supply/max1720x_battery.c line 325

Quoted 13 lines
drivers/power/supply/max1720x_battery.c new file +13 -0
+static int max1720x_probe(struct i2c_client *client)
+{
+	struct power_supply_config psy_cfg = {};
+	struct device *dev = &client->dev;
+	struct max1720x_device_info *info;
+	struct power_supply *bat;
+	int ret;
+
+	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
+	if (!info)
+		return -ENOMEM;
+
+	psy_cfg.drv_data = info;

psy_cfg.fwnode = dev_fwnode(dev);

Otherwise LGTM.

Thanks for your patch,

-- Sebastian

#6 lore raw

Re: [PATCH v4 2/2] power: supply: add support for MAX1720x standalone fuel gauge

Am Wed, Jun 19, 2024 at 01:19:32AM +0200 schrieb Sebastian Reichel:

Quoted 32 lines

Hi,

Quoted 6 lines · Dimitri Fedrau

The MAX17201 monitors a single cell pack. The MAX17205 monitors and balances a 2S or 3S pack or monitors a multiple-series cell pack. Both devices use a I2C interface.

---

[...]

Quoted 20 lines
drivers/power/supply/max1720x_battery.c new file +20 -0
+static int max1720x_read_word_data_nvmem(struct i2c_client *ancillary, u8 addr)
+{
+	u8 rx[2];
+	struct i2c_msg msgs[] = {
+		{
+			.addr = ancillary->addr,
+			.flags = 0,
+			.len = 1,
+			.buf = &addr,
+		},
+		{
+			.addr = ancillary->addr,
+			.flags = I2C_M_RD,
+			.len = 2,
+			.buf = &rx[0],
+		},
+	};
+	int ret;
+
+	ret = i2c_transfer(ancillary->adapter, msgs, ARRAY_SIZE(msgs));
Quoted 9 lines

drivers/power/supply/max1720x_battery.c line 275

Quoted 5 lines
drivers/power/supply/max1720x_battery.c new file +5 -0
+	if (ret != ARRAY_SIZE(msgs))
+		return ret < 0 ? ret : -EIO;
+
+	return get_unaligned_le16(&rx[0]);
+}

Have you tried using i2c_smbus_read_word_data(ancillary, addr) instead of the above?

Yes, I did and it worked. Used it in V3 and changed it due to review comments by Thomas which make sense to me. I don't have any preference on this. Should I change it back ?

Quoted 2 lines

[...]

[...]

Dimitri

#7 lore raw

Re: [PATCH v4 2/2] power: supply: add support for MAX1720x standalone fuel gauge

Hi,

Quoted 40 lines · Dimitri Fedrau

Am Wed, Jun 19, 2024 at 01:19:32AM +0200 schrieb Sebastian Reichel:

Quoted 30 lines · Dimitri Fedrau
Quoted 6 lines

The MAX17201 monitors a single cell pack. The MAX17205 monitors and balances a 2S or 3S pack or monitors a multiple-series cell pack. Both devices use a I2C interface.

---

[...]

Quoted 20 lines
drivers/power/supply/max1720x_battery.c new file +20 -0
+static int max1720x_read_word_data_nvmem(struct i2c_client *ancillary, u8 addr)
+{
+	u8 rx[2];
+	struct i2c_msg msgs[] = {
+		{
+			.addr = ancillary->addr,
+			.flags = 0,
+			.len = 1,
+			.buf = &addr,
+		},
+		{
+			.addr = ancillary->addr,
+			.flags = I2C_M_RD,
+			.len = 2,
+			.buf = &rx[0],
+		},
+	};
+	int ret;
+
+	ret = i2c_transfer(ancillary->adapter, msgs, ARRAY_SIZE(msgs));
Quoted 9 lines

drivers/power/supply/max1720x_battery.c line 275

Quoted 5 lines
drivers/power/supply/max1720x_battery.c new file +5 -0
+	if (ret != ARRAY_SIZE(msgs))
+		return ret < 0 ? ret : -EIO;
+
+	return get_unaligned_le16(&rx[0]);
+}

Have you tried using i2c_smbus_read_word_data(ancillary, addr) instead of the above?

Quoted 3 lines

Yes, I did and it worked. Used it in V3 and changed it due to review comments by Thomas which make sense to me. I don't have any preference on this. Should I change it back ?

Using the smbus function makes the driver work with a pure SMBus adapter and keeps it working with I2C adapters through the SMBus "emulation". By using the open coded version we have more code and the driver does not work with pure SMBus adapters.

So I see no reason why the open coded version should be used. Note, that regmap also uses i2c_smbus_read_word_data() internally for your configuration of the non-ancillary device.

Greetings,

-- Sebastian

#8 lore raw

Re: [PATCH v4 2/2] power: supply: add support for MAX1720x standalone fuel gauge

Hi Sebastian,

Am Wed, Jun 19, 2024 at 05:30:27PM +0200 schrieb Sebastian Reichel:

Quoted 47 lines

Hi,

Quoted 40 lines · Dimitri Fedrau

Am Wed, Jun 19, 2024 at 01:19:32AM +0200 schrieb Sebastian Reichel:

Quoted 30 lines · Dimitri Fedrau
Quoted 6 lines

The MAX17201 monitors a single cell pack. The MAX17205 monitors and balances a 2S or 3S pack or monitors a multiple-series cell pack. Both devices use a I2C interface.

---

[...]

Quoted 20 lines
drivers/power/supply/max1720x_battery.c new file +20 -0
+static int max1720x_read_word_data_nvmem(struct i2c_client *ancillary, u8 addr)
+{
+	u8 rx[2];
+	struct i2c_msg msgs[] = {
+		{
+			.addr = ancillary->addr,
+			.flags = 0,
+			.len = 1,
+			.buf = &addr,
+		},
+		{
+			.addr = ancillary->addr,
+			.flags = I2C_M_RD,
+			.len = 2,
+			.buf = &rx[0],
+		},
+	};
+	int ret;
+
+	ret = i2c_transfer(ancillary->adapter, msgs, ARRAY_SIZE(msgs));
Quoted 9 lines

drivers/power/supply/max1720x_battery.c line 275

Quoted 5 lines
drivers/power/supply/max1720x_battery.c new file +5 -0
+	if (ret != ARRAY_SIZE(msgs))
+		return ret < 0 ? ret : -EIO;
+
+	return get_unaligned_le16(&rx[0]);
+}

Have you tried using i2c_smbus_read_word_data(ancillary, addr) instead of the above?

Quoted 3 lines

Yes, I did and it worked. Used it in V3 and changed it due to review comments by Thomas which make sense to me. I don't have any preference on this. Should I change it back ?

Quoted 9 lines

Using the smbus function makes the driver work with a pure SMBus adapter and keeps it working with I2C adapters through the SMBus "emulation". By using the open coded version we have more code and the driver does not work with pure SMBus adapters.

So I see no reason why the open coded version should be used. Note, that regmap also uses i2c_smbus_read_word_data() internally for your configuration of the non-ancillary device.

Thanks for the explanation, will switch to i2c_smbus_read_word_data in the next version.

Dimitri