Skip to contentSkip to conversation

dt-bindings: power: Add the BQ27561 fuel gauge bindings

PATCH In review Dan Murphy 2 participants 6 messages Apr 17, 2020 – Apr 30, 2020

Subsystems (2)
lore
Share & tools
lore

Dan Murphy

author
#0 lore raw

PATCH 1/2 dt-bindings: power: Add the BQ27561 fuel gauge bindings

Introduce the Texas Instrument BQ27561 and BQ27750 fuel gauge device tree bindings.

---
 .../bindings/power/supply/bq27561.yaml        | 46 +++++++++++++++++++
 1 file changed, 46 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/power/supply/bq27561.yaml
Documentation/devicetree/bindings/power/supply/bq27561.yaml new file +46 -0
new file mode 100644
index 000000000000..abc9acdbc704
@@ -0,0 +1,46 @@
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2020 Texas Instruments Incorporated
+%YAML 1.2
+---
+$id: "http://devicetree.org/schemas/power/supply/bq27561.yaml#"
+$schema: "http://devicetree.org/meta-schemas/core.yaml#"
+
+title: TI BQ27561 and BQ27750 Fuel Gauges
+
+maintainers:
+  - Dan Murphy <[email protected]>
+
+description: |
+  The bq27z561 device provides a feature-rich gas gauging solution for
+  single-cell battery pack applications.
+
+  Specifications about the charger can be found at:
+    https://www.ti.com/lit/gpn/bq27z561
+    https://www.ti.com/lit/gpn/bq27750
+
+properties:
+  compatible:
+    enum:
+      - ti,bq27561
+      - ti,bq27750
+
+  reg:
+    maxItems: 1
+    description: |
+      I2C address of the device which is 0x55
+
+required:
+  - compatible
+  - reg
+additionalProperties: false
+
+examples:
+  - |
+    i2c0 {
+      #address-cells = <1>;
+      #size-cells = <0>;
+      fuel_gauge@55 {
+        compatible = "ti,bq27561";
+        reg = <0x55>;
+      };
+    };
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 2/2 power: supply: Introduce the BQ27561 Fuel gauge driver

Introduce the BQ27561 Fuel gauge driver from Texas Instruments. The driver also supports the BQ27750 as it has the same register map and bit mask.

---
 drivers/power/supply/Kconfig      |   9 +
 drivers/power/supply/Makefile     |   1 +
 drivers/power/supply/bq27561_fg.c | 304 ++++++++++++++++++++++++++++++
 3 files changed, 314 insertions(+)
 create mode 100644 drivers/power/supply/bq27561_fg.c
drivers/power/supply/Kconfig +9 -0
index f3424fdce341..febf6bf8c4ea 100644
@@ -597,6 +597,15 @@ config CHARGER_BQ25890
 	help
 	  Say Y to enable support for the TI BQ25890 battery charger.
 
+config FUEL_GAUGE_BQ27561
+	tristate "Texas Instruments bq27561 fuel gauge driver"
+	depends on I2C
+	select REGMAP_I2C
+	depends on GPIOLIB || COMPILE_TEST
+	help
+	 Say Y here to enable support for fuel gauge with TI bq27561
+	 fuel gauge chips.
+
 config CHARGER_SMB347
 	tristate "Summit Microelectronics SMB347 Battery Charger"
 	depends on I2C
drivers/power/supply/Makefile +1 -0
index 6c7da920ea83..4e402292b276 100644
@@ -81,6 +81,7 @@ obj-$(CONFIG_CHARGER_BQ24190)	+= bq24190_charger.o
 obj-$(CONFIG_CHARGER_BQ24257)	+= bq24257_charger.o
 obj-$(CONFIG_CHARGER_BQ24735)	+= bq24735-charger.o
 obj-$(CONFIG_CHARGER_BQ25890)	+= bq25890_charger.o
+obj-$(CONFIG_FUEL_GAUGE_BQ27561)	+= bq27561_fg.o
 obj-$(CONFIG_CHARGER_SMB347)	+= smb347-charger.o
 obj-$(CONFIG_CHARGER_TPS65090)	+= tps65090-charger.o
 obj-$(CONFIG_CHARGER_TPS65217)	+= tps65217_charger.o
drivers/power/supply/bq27561_fg.c new file +304 -0

304 lines not on this page · raw message

Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH 2/2] power: supply: Introduce the BQ27561 Fuel gauge driver

Hi,

Quoted 3 lines · Dan Murphy

Introduce the BQ27561 Fuel gauge driver from Texas Instruments. The driver also supports the BQ27750 as it has the same register map and bit mask.

Why a separate driver instead of adding support to bq27xxx? The register map seems to be similar enough.

Quoted 13 lines
---
 drivers/power/supply/Kconfig      |   9 +
 drivers/power/supply/Makefile     |   1 +
 drivers/power/supply/bq27561_fg.c | 304 ++++++++++++++++++++++++++++++
 3 files changed, 314 insertions(+)
 create mode 100644 drivers/power/supply/bq27561_fg.c
drivers/power/supply/Kconfig +0 -0
index f3424fdce341..febf6bf8c4ea 100644

drivers/power/supply/Kconfig config CHARGER_BQ25890 · line 603

Quoted 7 lines
drivers/power/supply/Kconfig +4 -0
@@ -597,6 +597,15 @@ config CHARGER_BQ25890
 	help
 	  Say Y to enable support for the TI BQ25890 battery charger.
 
+config FUEL_GAUGE_BQ27561
+	tristate "Texas Instruments bq27561 fuel gauge driver"
+	depends on I2C
+	select REGMAP_I2C

good :)

Quoted 84 lines
drivers/power/supply/Kconfig +5 -0
+	depends on GPIOLIB || COMPILE_TEST
+	help
+	 Say Y here to enable support for fuel gauge with TI bq27561
+	 fuel gauge chips.
+
 config CHARGER_SMB347
 	tristate "Summit Microelectronics SMB347 Battery Charger"
 	depends on I2C
drivers/power/supply/Makefile +1 -0
index 6c7da920ea83..4e402292b276 100644
@@ -81,6 +81,7 @@ obj-$(CONFIG_CHARGER_BQ24190)	+= bq24190_charger.o
 obj-$(CONFIG_CHARGER_BQ24257)	+= bq24257_charger.o
 obj-$(CONFIG_CHARGER_BQ24735)	+= bq24735-charger.o
 obj-$(CONFIG_CHARGER_BQ25890)	+= bq25890_charger.o
+obj-$(CONFIG_FUEL_GAUGE_BQ27561)	+= bq27561_fg.o
 obj-$(CONFIG_CHARGER_SMB347)	+= smb347-charger.o
 obj-$(CONFIG_CHARGER_TPS65090)	+= tps65090-charger.o
 obj-$(CONFIG_CHARGER_TPS65217)	+= tps65217_charger.o
drivers/power/supply/bq27561_fg.c new file +58 -0
new file mode 100644
index 000000000000..b9f7cdc00d2e
@@ -0,0 +1,304 @@
+// SPDX-License-Identifier: GPL-2.0
+// BQ27561 Fuel Gauge Driver
+// Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com/
+// Author: Dan Murphy <[email protected]>
+
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/power_supply.h>
+#include <linux/types.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/moduleparam.h>
+#include <linux/slab.h>
+#include <linux/of.h>
+
+#define BQ27561_REG_CNTL	0x00
+#define BQ27561_REG_AR		0x02
+#define BQ27561_REG_TEMP	0x06
+#define BQ27561_REG_VOLT	0x08
+#define BQ27561_REG_REMAIN_CAP	0x10
+#define BQ27561_REG_BATTSTAT	0x0a
+#define BQ27561_REG_CURRENT	0x0c
+#define BQ27561_REG_FCC		0x12
+#define BQ27561_REG_AVG_CURRENT	0x14
+#define BQ27561_REG_TTE		0x16
+#define BQ27561_REG_TTF		0x18
+#define BQ27561_REG_CYCLECNT	0x2a
+#define BQ27561_REG_RSOC	0x2c
+
+#define BQ27561_VAL_MULTIPLIER	1000
+#define BQ27561_TEMP_K_TO_C	2732
+#define BQ27561_TIME_MIN_TO_S	60
+
+#define BQ27561_BATT_DEPLETED	BIT(4)
+#define BQ27561_BATT_CHARGED	BIT(5)
+#define BQ27561_BATT_DISCHARGING	BIT(6)
+
+enum {
+	BQ27561,
+	BQ27750,
+};
+
+struct bq27561_device {
+	struct device *dev;
+	struct i2c_client *client;
+	struct power_supply *battery;
+};
+
+static enum power_supply_property bq27561_properties[] = {
+	POWER_SUPPLY_PROP_STATUS,
+	POWER_SUPPLY_PROP_CAPACITY,
+	POWER_SUPPLY_PROP_TEMP,
+	POWER_SUPPLY_PROP_VOLTAGE_NOW,
+	POWER_SUPPLY_PROP_CURRENT_NOW,

drivers/power/supply/bq27561_fg.c line 68

Quoted 10 lines
drivers/power/supply/bq27561_fg.c new file +10 -0
+	POWER_SUPPLY_PROP_CHARGE_NOW,
+	POWER_SUPPLY_PROP_CHARGE_FULL,
+	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
+};
+
+static int bq27561_get_temp(struct bq27561_device *bq)
+{
+	unsigned int temp;
+
+	temp = i2c_smbus_read_word_data(bq->client, BQ27561_REG_TEMP);

please use regmap instead of direct i2c read/write commands!

drivers/power/supply/bq27561_fg.c line 70

Quoted 2 lines
drivers/power/supply/bq27561_fg.c new file +2 -0
+	if (temp < 0)
+		return temp;

so e.g. -EIO will become -0.5°C?

Quoted 82 lines
drivers/power/supply/bq27561_fg.c new file +82 -0
+	return temp - BQ27561_TEMP_K_TO_C;
+}
+
+static int bq27561_get_voltage(struct bq27561_device *bq)
+{
+	unsigned int batt_volt;
+
+	batt_volt = i2c_smbus_read_word_data(bq->client, BQ27561_REG_VOLT);
+	if (batt_volt < 0)
+		return batt_volt;
+
+	return batt_volt * BQ27561_VAL_MULTIPLIER;
+}
+
+static int bq27561_get_current(struct bq27561_device *bq)
+{
+	unsigned int batt_curr;
+
+	batt_curr = i2c_smbus_read_word_data(bq->client, BQ27561_REG_CURRENT);
+	if (batt_curr < 0)
+		return batt_curr;
+
+	return batt_curr * BQ27561_VAL_MULTIPLIER;
+}
+
+static int bq27561_get_charge_capacity(struct bq27561_device *bq)
+{
+	unsigned int charge_cap;
+
+	charge_cap = i2c_smbus_read_word_data(bq->client, BQ27561_REG_FCC);
+	if (charge_cap < 0)
+		return charge_cap;
+
+	return charge_cap * BQ27561_VAL_MULTIPLIER;
+}
+
+static int bq27561_get_batt_status(struct bq27561_device *bq)
+{
+	unsigned int batt_stat;
+	unsigned int curr;
+	int ret;
+
+	batt_stat = i2c_smbus_read_word_data(bq->client, BQ27561_REG_BATTSTAT);
+	if (batt_stat < 0)
+		return batt_stat;
+
+	curr = i2c_smbus_read_word_data(bq->client, BQ27561_REG_CURRENT);
+	if (curr < 0)
+		return curr;
+
+	if (batt_stat & BQ27561_BATT_CHARGED)
+		ret = POWER_SUPPLY_STATUS_FULL;
+	else if (batt_stat & BQ27561_BATT_DISCHARGING || curr == 0)
+		ret = POWER_SUPPLY_STATUS_DISCHARGING;
+	else if (curr > 0)
+		ret = POWER_SUPPLY_STATUS_CHARGING;
+	else
+		ret = POWER_SUPPLY_STATUS_UNKNOWN;
+
+	return ret;
+}
+
+static int bq27561_get_empty_time(struct bq27561_device *bq)
+{
+	unsigned int empty_time;
+
+	empty_time = i2c_smbus_read_word_data(bq->client, BQ27561_REG_TTE);
+	if (empty_time < 0)
+		return empty_time;
+
+	return empty_time * BQ27561_TIME_MIN_TO_S;
+}
+
+static int bq27561_get_property(struct power_supply *psy,
+				enum power_supply_property psp,
+				union power_supply_propval *val)
+{
+	struct bq27561_device *bq = power_supply_get_drvdata(psy);
+	int ret = 0;
+
+	switch (psp) {
+	case POWER_SUPPLY_PROP_VOLTAGE_NOW:

drivers/power/supply/bq27561_fg.c line 163

Quoted 10 lines
drivers/power/supply/bq27561_fg.c new file +10 -0
+		ret = bq27561_get_voltage(bq);
+		if (ret < 0)
+			return ret;
+
+		val->intval = ret;
+		break;
+	case POWER_SUPPLY_PROP_CURRENT_NOW:
+		ret = bq27561_get_current(bq);
+		if (ret < 0)
+			return ret;

current can be negative (charging vs discharging).

drivers/power/supply/bq27561_fg.c line 176

Quoted 13 lines
drivers/power/supply/bq27561_fg.c new file +13 -0
+
+		val->intval = ret;
+		break;
+	case POWER_SUPPLY_PROP_STATUS:
+		ret = bq27561_get_batt_status(bq);
+		if (ret < 0)
+			return ret;
+
+		val->intval = ret;
+		break;
+	case POWER_SUPPLY_PROP_TEMP:
+		val->intval = bq27561_get_temp(bq);
+		break;

no error handling?

Quoted 79 lines
drivers/power/supply/bq27561_fg.c new file +79 -0
+	case POWER_SUPPLY_PROP_CAPACITY:
+		ret = i2c_smbus_read_word_data(bq->client, BQ27561_REG_RSOC);
+		if (ret < 0)
+			return ret;
+
+		val->intval = ret;
+		break;
+	case POWER_SUPPLY_PROP_CHARGE_FULL:
+		ret = bq27561_get_charge_capacity(bq);
+		if (ret < 0)
+			return ret;
+
+		val->intval = ret;
+		break;
+	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
+		ret = bq27561_get_empty_time(bq);
+		if (ret < 0)
+			return ret;
+
+		val->intval = ret;
+		break;
+	case POWER_SUPPLY_PROP_CHARGE_NOW:
+		ret = i2c_smbus_read_word_data(bq->client, BQ27561_REG_AR);
+		if (ret < 0)
+			return ret;
+
+		val->intval = ret;
+		break;
+	default:
+		return -EINVAL;
+	};
+
+	return 0;
+}
+
+static int bq27561_set_property(struct power_supply *psy,
+				enum power_supply_property psp,
+				const union power_supply_propval *val)
+{
+	struct bq27561_device *bq = power_supply_get_drvdata(psy);
+	int ret = 0;
+
+	switch (psp) {
+	case POWER_SUPPLY_PROP_CHARGE_NOW:
+		ret = i2c_smbus_write_word_data(bq->client, BQ27561_REG_AR,
+						val->intval);
+		break;
+	default:
+		return -EINVAL;
+	};
+
+	return ret;
+}
+
+static int bq27561_prop_is_writeable(struct power_supply *psy,
+				     enum power_supply_property prop)
+{
+	switch (prop) {
+	case POWER_SUPPLY_PROP_CHARGE_NOW:
+		return 1;
+	default:
+		return 0;
+	};
+}
+
+static struct power_supply_desc bq27561_power_supply_desc = {
+	.type = POWER_SUPPLY_TYPE_BATTERY,
+	.properties = bq27561_properties,
+	.num_properties = ARRAY_SIZE(bq27561_properties),
+	.get_property = bq27561_get_property,
+	.set_property = bq27561_set_property,
+	.property_is_writeable = bq27561_prop_is_writeable,
+};
+
+static int bq27561_probe(struct i2c_client *client,
+				 const struct i2c_device_id *id)
+{
+	struct bq27561_device *bq;
+	struct power_supply_config psy_cfg;

drivers/power/supply/bq27561_fg.c line 265

Quoted 10 lines
drivers/power/supply/bq27561_fg.c new file +10 -0
+
+	bq = devm_kzalloc(&client->dev, sizeof(*bq), GFP_KERNEL);
+	if (!bq)
+		return -ENOMEM;
+
+	bq->dev = &client->dev;
+	bq->client = client;
+	i2c_set_clientdata(client, bq);
+
+	psy_cfg.drv_data = bq;

You should setup psy_cfg.of_node, so that it can be referenced in DT.

Quoted 20 lines
drivers/power/supply/bq27561_fg.c new file +20 -0
+	bq27561_power_supply_desc.name = id->name;
+	bq->battery = devm_power_supply_register(bq->dev,
+						 &bq27561_power_supply_desc,
+						 &psy_cfg);
+
+	if (IS_ERR(bq->battery))
+		return -EINVAL;
+
+	return 0;
+}
+
+static const struct of_device_id bq27561_match_table[] = {
+	{.compatible = "ti,bq27561",},
+	{.compatible = "ti,bq27750",},
+	{},
+};
+MODULE_DEVICE_TABLE(of, bq_fg_match_table);
+
+static const struct i2c_device_id bq27561_id[] = {
+	{"bq27561", BQ27561},

drivers/power/supply/bq27561_fg.c line 295

Quoted 10 lines
drivers/power/supply/bq27561_fg.c new file +10 -0
+	{"bq27750", BQ27750},
+	{},
+};
+MODULE_DEVICE_TABLE(i2c, bq27561_id);
+
+static struct i2c_driver bq27561_battery_driver = {
+	.driver = {
+		   .name = "bq27561-fuel-gauge",
+		   .of_match_table = bq27561_match_table,
+		   .owner = THIS_MODULE,

i2c_register_driver() sets it owner automatically, so drop this.

drivers/power/supply/bq27561_fg.c line 304

Quoted 9 lines
drivers/power/supply/bq27561_fg.c new file +9 -0
+		   },
+	.probe = bq27561_probe,
+	.id_table = bq27561_id,
+};
+module_i2c_driver(bq27561_battery_driver);
+
+MODULE_AUTHOR("Texas Instruments");
+MODULE_DESCRIPTION("bq27561 battery monitor driver");
+MODULE_LICENSE("GPL v2");

-- Sebastian

author
#3 lore raw

Re: [PATCH 2/2] power: supply: Introduce the BQ27561 Fuel gauge driver

Sebastian

Quoted 8 lines · Sebastian Reichel

Hi,

Quoted 3 lines · Dan Murphy

Introduce the BQ27561 Fuel gauge driver from Texas Instruments. The driver also supports the BQ27750 as it has the same register map and bit mask.

Why a separate driver instead of adding support to bq27xxx? The register map seems to be similar enough.

I will need to see if I can integrate this into the bq27xxx.  I have to look at these 2 devices as well as another device that we need to integrate

But preliminary analysis suggests that we can integrate these 2 devices plus the third one into the driver.

Quoted 16 lines
Quoted 16 lines
---
  drivers/power/supply/Kconfig      |   9 +
  drivers/power/supply/Makefile     |   1 +
  drivers/power/supply/bq27561_fg.c | 304 ++++++++++++++++++++++++++++++
  3 files changed, 314 insertions(+)

create mode 100644 drivers/power/supply/bq27561_fg.c

drivers/power/supply/Kconfig +0 -0
index f3424fdce341..febf6bf8c4ea 100644
@@ -597,6 +597,15 @@ config CHARGER_BQ25890
  	help
  	  Say Y to enable support for the TI BQ25890 battery charger.
 
Quoted 5 lines

drivers/power/supply/Kconfig config CHARGER_BQ25890 · line 603

Quoted 4 lines
drivers/power/supply/Kconfig +4 -0
+config FUEL_GAUGE_BQ27561
+	tristate "Texas Instruments bq27561 fuel gauge driver"
+	depends on I2C
+	select REGMAP_I2C

good :)

Ack

Quoted 88 lines
Quoted 88 lines
drivers/power/supply/Kconfig +5 -0
+	depends on GPIOLIB || COMPILE_TEST
+	help
+	 Say Y here to enable support for fuel gauge with TI bq27561
+	 fuel gauge chips.
+
  config CHARGER_SMB347
  	tristate "Summit Microelectronics SMB347 Battery Charger"
  	depends on I2C
drivers/power/supply/Makefile +1 -0
index 6c7da920ea83..4e402292b276 100644
@@ -81,6 +81,7 @@ obj-$(CONFIG_CHARGER_BQ24190)	+= bq24190_charger.o
  obj-$(CONFIG_CHARGER_BQ24257)	+= bq24257_charger.o
  obj-$(CONFIG_CHARGER_BQ24735)	+= bq24735-charger.o
  obj-$(CONFIG_CHARGER_BQ25890)	+= bq25890_charger.o
+obj-$(CONFIG_FUEL_GAUGE_BQ27561)	+= bq27561_fg.o
  obj-$(CONFIG_CHARGER_SMB347)	+= smb347-charger.o
  obj-$(CONFIG_CHARGER_TPS65090)	+= tps65090-charger.o
  obj-$(CONFIG_CHARGER_TPS65217)	+= tps65217_charger.o
drivers/power/supply/bq27561_fg.c new file +61 -0
new file mode 100644
index 000000000000..b9f7cdc00d2e
@@ -0,0 +1,304 @@
+// SPDX-License-Identifier: GPL-2.0
+// BQ27561 Fuel Gauge Driver
+// Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com/
+// Author: Dan Murphy <[email protected]>
+
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/power_supply.h>
+#include <linux/types.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/moduleparam.h>
+#include <linux/slab.h>
+#include <linux/of.h>
+
+#define BQ27561_REG_CNTL	0x00
+#define BQ27561_REG_AR		0x02
+#define BQ27561_REG_TEMP	0x06
+#define BQ27561_REG_VOLT	0x08
+#define BQ27561_REG_REMAIN_CAP	0x10
+#define BQ27561_REG_BATTSTAT	0x0a
+#define BQ27561_REG_CURRENT	0x0c
+#define BQ27561_REG_FCC		0x12
+#define BQ27561_REG_AVG_CURRENT	0x14
+#define BQ27561_REG_TTE		0x16
+#define BQ27561_REG_TTF		0x18
+#define BQ27561_REG_CYCLECNT	0x2a
+#define BQ27561_REG_RSOC	0x2c
+
+#define BQ27561_VAL_MULTIPLIER	1000
+#define BQ27561_TEMP_K_TO_C	2732
+#define BQ27561_TIME_MIN_TO_S	60
+
+#define BQ27561_BATT_DEPLETED	BIT(4)
+#define BQ27561_BATT_CHARGED	BIT(5)
+#define BQ27561_BATT_DISCHARGING	BIT(6)
+
+enum {
+	BQ27561,
+	BQ27750,
+};
+
+struct bq27561_device {
+	struct device *dev;
+	struct i2c_client *client;
+	struct power_supply *battery;
+};
+
+static enum power_supply_property bq27561_properties[] = {
+	POWER_SUPPLY_PROP_STATUS,
+	POWER_SUPPLY_PROP_CAPACITY,
+	POWER_SUPPLY_PROP_TEMP,
+	POWER_SUPPLY_PROP_VOLTAGE_NOW,
+	POWER_SUPPLY_PROP_CURRENT_NOW,
+	POWER_SUPPLY_PROP_CHARGE_NOW,
+	POWER_SUPPLY_PROP_CHARGE_FULL,
+	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
Quoted 9 lines

drivers/power/supply/bq27561_fg.c line 68

Quoted 7 lines
drivers/power/supply/bq27561_fg.c new file +7 -0
+};
+
+static int bq27561_get_temp(struct bq27561_device *bq)
+{
+	unsigned int temp;
+
+	temp = i2c_smbus_read_word_data(bq->client, BQ27561_REG_TEMP);

please use regmap instead of direct i2c read/write commands!

OK I did not look at regmap close enough to see if it supports smbus.  But apparently it does.

I just need to make sure.

Quoted 3 lines

drivers/power/supply/bq27561_fg.c line 70

Quoted 2 lines
drivers/power/supply/bq27561_fg.c new file +2 -0
+	if (temp < 0)
+		return temp;

so e.g. -EIO will become -0.5°C?

Ack.  It is hard to get a negative temp to be recorded by the thermister in testing.

Quoted 86 lines
Quoted 86 lines
drivers/power/supply/bq27561_fg.c new file +85 -0
+	return temp - BQ27561_TEMP_K_TO_C;
+}
+
+static int bq27561_get_voltage(struct bq27561_device *bq)
+{
+	unsigned int batt_volt;
+
+	batt_volt = i2c_smbus_read_word_data(bq->client, BQ27561_REG_VOLT);
+	if (batt_volt < 0)
+		return batt_volt;
+
+	return batt_volt * BQ27561_VAL_MULTIPLIER;
+}
+
+static int bq27561_get_current(struct bq27561_device *bq)
+{
+	unsigned int batt_curr;
+
+	batt_curr = i2c_smbus_read_word_data(bq->client, BQ27561_REG_CURRENT);
+	if (batt_curr < 0)
+		return batt_curr;
+
+	return batt_curr * BQ27561_VAL_MULTIPLIER;
+}
+
+static int bq27561_get_charge_capacity(struct bq27561_device *bq)
+{
+	unsigned int charge_cap;
+
+	charge_cap = i2c_smbus_read_word_data(bq->client, BQ27561_REG_FCC);
+	if (charge_cap < 0)
+		return charge_cap;
+
+	return charge_cap * BQ27561_VAL_MULTIPLIER;
+}
+
+static int bq27561_get_batt_status(struct bq27561_device *bq)
+{
+	unsigned int batt_stat;
+	unsigned int curr;
+	int ret;
+
+	batt_stat = i2c_smbus_read_word_data(bq->client, BQ27561_REG_BATTSTAT);
+	if (batt_stat < 0)
+		return batt_stat;
+
+	curr = i2c_smbus_read_word_data(bq->client, BQ27561_REG_CURRENT);
+	if (curr < 0)
+		return curr;
+
+	if (batt_stat & BQ27561_BATT_CHARGED)
+		ret = POWER_SUPPLY_STATUS_FULL;
+	else if (batt_stat & BQ27561_BATT_DISCHARGING || curr == 0)
+		ret = POWER_SUPPLY_STATUS_DISCHARGING;
+	else if (curr > 0)
+		ret = POWER_SUPPLY_STATUS_CHARGING;
+	else
+		ret = POWER_SUPPLY_STATUS_UNKNOWN;
+
+	return ret;
+}
+
+static int bq27561_get_empty_time(struct bq27561_device *bq)
+{
+	unsigned int empty_time;
+
+	empty_time = i2c_smbus_read_word_data(bq->client, BQ27561_REG_TTE);
+	if (empty_time < 0)
+		return empty_time;
+
+	return empty_time * BQ27561_TIME_MIN_TO_S;
+}
+
+static int bq27561_get_property(struct power_supply *psy,
+				enum power_supply_property psp,
+				union power_supply_propval *val)
+{
+	struct bq27561_device *bq = power_supply_get_drvdata(psy);
+	int ret = 0;
+
+	switch (psp) {
+	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
+		ret = bq27561_get_voltage(bq);
+		if (ret < 0)
+			return ret;
Quoted 8 lines

drivers/power/supply/bq27561_fg.c line 163

Quoted 7 lines
drivers/power/supply/bq27561_fg.c new file +7 -0
+
+		val->intval = ret;
+		break;
+	case POWER_SUPPLY_PROP_CURRENT_NOW:
+		ret = bq27561_get_current(bq);
+		if (ret < 0)
+			return ret;

current can be negative (charging vs discharging).

Ack

Quoted 15 lines
Quoted 4 lines
drivers/power/supply/bq27561_fg.c new file +3 -0
+
+		val->intval = ret;
+		break;

drivers/power/supply/bq27561_fg.c line 176

Quoted 10 lines
drivers/power/supply/bq27561_fg.c new file +10 -0
+	case POWER_SUPPLY_PROP_STATUS:
+		ret = bq27561_get_batt_status(bq);
+		if (ret < 0)
+			return ret;
+
+		val->intval = ret;
+		break;
+	case POWER_SUPPLY_PROP_TEMP:
+		val->intval = bq27561_get_temp(bq);
+		break;

no error handling?

Ack

Quoted 84 lines
Quoted 84 lines
drivers/power/supply/bq27561_fg.c new file +83 -0
+	case POWER_SUPPLY_PROP_CAPACITY:
+		ret = i2c_smbus_read_word_data(bq->client, BQ27561_REG_RSOC);
+		if (ret < 0)
+			return ret;
+
+		val->intval = ret;
+		break;
+	case POWER_SUPPLY_PROP_CHARGE_FULL:
+		ret = bq27561_get_charge_capacity(bq);
+		if (ret < 0)
+			return ret;
+
+		val->intval = ret;
+		break;
+	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
+		ret = bq27561_get_empty_time(bq);
+		if (ret < 0)
+			return ret;
+
+		val->intval = ret;
+		break;
+	case POWER_SUPPLY_PROP_CHARGE_NOW:
+		ret = i2c_smbus_read_word_data(bq->client, BQ27561_REG_AR);
+		if (ret < 0)
+			return ret;
+
+		val->intval = ret;
+		break;
+	default:
+		return -EINVAL;
+	};
+
+	return 0;
+}
+
+static int bq27561_set_property(struct power_supply *psy,
+				enum power_supply_property psp,
+				const union power_supply_propval *val)
+{
+	struct bq27561_device *bq = power_supply_get_drvdata(psy);
+	int ret = 0;
+
+	switch (psp) {
+	case POWER_SUPPLY_PROP_CHARGE_NOW:
+		ret = i2c_smbus_write_word_data(bq->client, BQ27561_REG_AR,
+						val->intval);
+		break;
+	default:
+		return -EINVAL;
+	};
+
+	return ret;
+}
+
+static int bq27561_prop_is_writeable(struct power_supply *psy,
+				     enum power_supply_property prop)
+{
+	switch (prop) {
+	case POWER_SUPPLY_PROP_CHARGE_NOW:
+		return 1;
+	default:
+		return 0;
+	};
+}
+
+static struct power_supply_desc bq27561_power_supply_desc = {
+	.type = POWER_SUPPLY_TYPE_BATTERY,
+	.properties = bq27561_properties,
+	.num_properties = ARRAY_SIZE(bq27561_properties),
+	.get_property = bq27561_get_property,
+	.set_property = bq27561_set_property,
+	.property_is_writeable = bq27561_prop_is_writeable,
+};
+
+static int bq27561_probe(struct i2c_client *client,
+				 const struct i2c_device_id *id)
+{
+	struct bq27561_device *bq;
+	struct power_supply_config psy_cfg;
+
+	bq = devm_kzalloc(&client->dev, sizeof(*bq), GFP_KERNEL);
+	if (!bq)
+		return -ENOMEM;
Quoted 9 lines

drivers/power/supply/bq27561_fg.c line 265

Quoted 6 lines
drivers/power/supply/bq27561_fg.c new file +6 -0
+
+	bq->dev = &client->dev;
+	bq->client = client;
+	i2c_set_clientdata(client, bq);
+
+	psy_cfg.drv_data = bq;

You should setup psy_cfg.of_node, so that it can be referenced in DT.

Ack

Quoted 43 lines
Quoted 20 lines
drivers/power/supply/bq27561_fg.c new file +20 -0
+	bq27561_power_supply_desc.name = id->name;
+	bq->battery = devm_power_supply_register(bq->dev,
+						 &bq27561_power_supply_desc,
+						 &psy_cfg);
+
+	if (IS_ERR(bq->battery))
+		return -EINVAL;
+
+	return 0;
+}
+
+static const struct of_device_id bq27561_match_table[] = {
+	{.compatible = "ti,bq27561",},
+	{.compatible = "ti,bq27750",},
+	{},
+};
+MODULE_DEVICE_TABLE(of, bq_fg_match_table);
+
+static const struct i2c_device_id bq27561_id[] = {
+	{"bq27561", BQ27561},

drivers/power/supply/bq27561_fg.c line 295

Quoted 10 lines
drivers/power/supply/bq27561_fg.c new file +10 -0
+	{"bq27750", BQ27750},
+	{},
+};
+MODULE_DEVICE_TABLE(i2c, bq27561_id);
+
+static struct i2c_driver bq27561_battery_driver = {
+	.driver = {
+		   .name = "bq27561-fuel-gauge",
+		   .of_match_table = bq27561_match_table,
+		   .owner = THIS_MODULE,

i2c_register_driver() sets it owner automatically, so drop this.

drivers/power/supply/bq27561_fg.c line 304

Quoted 9 lines
drivers/power/supply/bq27561_fg.c new file +9 -0
+		   },
+	.probe = bq27561_probe,
+	.id_table = bq27561_id,
+};
+module_i2c_driver(bq27561_battery_driver);
+
+MODULE_AUTHOR("Texas Instruments");
+MODULE_DESCRIPTION("bq27561 battery monitor driver");
+MODULE_LICENSE("GPL v2");

-- Sebastian

#4 lore raw
In reply to Dan Murphy

Hi,

Quoted 6 lines · Dan Murphy

Introduce the Texas Instrument BQ27561 and BQ27750 fuel gauge device tree bindings.

---

I think its best to just add them to bq27xxx.txt. Conversion to YAML would still be nice of course :)

In any case the binding is incomplete, since batteries should contain a power-supplies node referencing their charger via a phandle, see power_suppliy.txt (which still needs to be converted to YAML).

-- Sebastian

Quoted 59 lines
 .../bindings/power/supply/bq27561.yaml        | 46 +++++++++++++++++++
 1 file changed, 46 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/power/supply/bq27561.yaml
Documentation/devicetree/bindings/power/supply/bq27561.yaml new file +46 -0
new file mode 100644
index 000000000000..abc9acdbc704
@@ -0,0 +1,46 @@
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2020 Texas Instruments Incorporated
+%YAML 1.2
+---
+$id: "http://devicetree.org/schemas/power/supply/bq27561.yaml#"
+$schema: "http://devicetree.org/meta-schemas/core.yaml#"
+
+title: TI BQ27561 and BQ27750 Fuel Gauges
+
+maintainers:
+  - Dan Murphy <[email protected]>
+
+description: |
+  The bq27z561 device provides a feature-rich gas gauging solution for
+  single-cell battery pack applications.
+
+  Specifications about the charger can be found at:
+    https://www.ti.com/lit/gpn/bq27z561
+    https://www.ti.com/lit/gpn/bq27750
+
+properties:
+  compatible:
+    enum:
+      - ti,bq27561
+      - ti,bq27750
+
+  reg:
+    maxItems: 1
+    description: |
+      I2C address of the device which is 0x55
+
+required:
+  - compatible
+  - reg
+additionalProperties: false
+
+examples:
+  - |
+    i2c0 {
+      #address-cells = <1>;
+      #size-cells = <0>;
+      fuel_gauge@55 {
+        compatible = "ti,bq27561";
+        reg = <0x55>;
+      };
+    };
Signature · 1 line
2.25.1
author
#5 lore raw

Sebastien

Quoted 11 lines · Sebastian Reichel

Hi,

Quoted 6 lines · Dan Murphy

Introduce the Texas Instrument BQ27561 and BQ27750 fuel gauge device tree bindings.

---

I think its best to just add them to bq27xxx.txt. Conversion to YAML would still be nice of course :)

Ack

Quoted 3 lines

In any case the binding is incomplete, since batteries should contain a power-supplies node referencing their charger via a phandle, see power_suppliy.txt (which still needs to be converted to YAML).

Ack

Dan