mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Heidelberg <david@ixit.cz>
To: rinat.muhamedgaliev@gmail.com, linux-pm@vger.kernel.org
Cc: pali@kernel.org, sre@kernel.org, afd@ti.com,
	konrad.dybcio@oss.qualcomm.com, krzk@kernel.org,
	linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v4] power: supply: bq27xxx: detect bq27541 behind bq27411 compatible
Date: Sun, 27 Sep 2026 23:57:21 +0200	[thread overview]
Message-ID: <d6dd3ff8-a728-43ab-9ce3-874bf2a2a457@ixit.cz> (raw)
In-Reply-To: <20260928-master-v4-1-052c73ec1767@gmail.com>

On 27/09/2026 23:17, Rinat Muhamedgaliev via B4 Relay wrote:
> From: Rinat Muhamedgaliev <rinat.muhamedgaliev@gmail.com>
> 
> OnePlus 6 and 6T replacement battery packs may contain either a bq27411 or a bq27541 fuel gauge at I2C address 0x55. The device tree currently identifies the gauge as bq27411, but a bq27541 uses a different register map and then reports invalid battery values.
> 
> Read the DeviceType control subcommand when probing a bq27411. Keep the existing profile for DeviceType 0x0421, but select the bq27541 profile for DeviceType 0x0541. This retains the established DT ABI and supports replacement packs without introducing a generic compatible.
> 
> Tested on OnePlus 6T (fajita) with DeviceType 0x0541: voltage, state of charge, and temperature were reported correctly. Testing on hardware with DeviceType 0x0421 would be appreciated.
> 
> Signed-off-by: Rinat Muhamedgaliev <rinat.muhamedgaliev@gmail.com>
> ---
> OnePlus 6 and 6T replacement battery packs can contain either a bq27411 or a
> bq27541 fuel gauge. The latter requires a different register map and produces
> invalid battery readings when interpreted as a bq27411.
> 
> v4 drops the proposed generic DT compatible and DTS changes. The I2C driver
> instead reads DeviceType while probing the existing bq27411 compatible, and
> selects the bq27541 profile if the device reports 0x0541.
> 
> The bq27541 path was tested on a OnePlus 6T. Testing on an OnePlus 6 or 6T
> whose fuel gauge reports DeviceType 0x0421 (bq27411) would be appreciated.
> 
> Changes in v4:
> - Drop the generic compatible and binding update.
> - Keep the established OnePlus DTS unchanged.
> - Detect bq27541 from DeviceType in the bq27411 probe path.
> - Send as a new thread.
> ---
>   drivers/power/supply/bq27xxx_battery_i2c.c | 37 ++++++++++++++++++++++++++++++
>   1 file changed, 37 insertions(+)
> 
> diff --git a/drivers/power/supply/bq27xxx_battery_i2c.c b/drivers/power/supply/bq27xxx_battery_i2c.c
> index 94b00bb89c17..732164423423 100644
> --- a/drivers/power/supply/bq27xxx_battery_i2c.c
> +++ b/drivers/power/supply/bq27xxx_battery_i2c.c
> @@ -16,6 +16,11 @@
>   static DEFINE_IDR(battery_id);
>   static DEFINE_MUTEX(battery_mutex);
>   
> +#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;
> @@ -136,6 +141,32 @@ static int bq27xxx_battery_i2c_bulk_write(struct bq27xxx_device_info *di,
>   	return 0;
>   }
>   
> +static int bq27xxx_battery_i2c_check_device_type(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:
> +		return 0;
> +	case BQ27541_DEVICE_TYPE:
> +		dev_warn(di->dev, "detected bq27541 instead of bq27411\n");

For extend-ability, I would not hardcode the name but print the chip originally 
supplied.

> +		di->chip = BQ27541;
> +		return 0;
> +	default:
> +		dev_err(di->dev, "unsupported device type 0x%04x\n", ret);

I would change wording to "unrecognized" here.

> +		return -ENODEV;
> +	}
> +}
> +
>   static int bq27xxx_battery_i2c_probe(struct i2c_client *client,
>   				     const struct i2c_device_id *id)
>   {
> @@ -169,6 +200,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 == BQ27411) {
> +		ret = bq27xxx_battery_i2c_check_device_type(di);
> +		if (ret)
> +			goto err_failed;

my few cents here is that until now we ignored mismatch, which may led to people 
defining wrong compatible (and maybe some chips partly worked with different 
compatible?) and now we would hard-fail.

I would say warning from the bq27xxx_battery_i2c_check_device_type is good 
enough, but lets hear what maintainers thinks :)

David

> +	}
> +
>   	ret = bq27xxx_battery_setup(di);
>   	if (ret)
>   		goto err_failed;
> 
> ---
> base-commit: 830b3c68c1fb1e9176028d02ef86f3cf76aa2476
> change-id: 20260927-master-bd0afd9696ef
> 
> Best regards,
> --
> Rinat Muhamedgaliev <rinat.muhamedgaliev@gmail.com>
> 
> 


  reply	other threads:[~2026-09-27 21:57 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-27 21:17 Rinat Muhamedgaliev via B4 Relay
2026-09-27 21:57 ` David Heidelberg [this message]
2026-09-27 22:00 ` Pali Rohár
2026-09-27 22:13   ` David Heidelberg
2026-09-27 22:31     ` Pali Rohár
2026-09-29 16:13       ` Andrew Davis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d6dd3ff8-a728-43ab-9ce3-874bf2a2a457@ixit.cz \
    --to=david@ixit.cz \
    --cc=afd@ti.com \
    --cc=konrad.dybcio@oss.qualcomm.com \
    --cc=krzk@kernel.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=pali@kernel.org \
    --cc=rinat.muhamedgaliev@gmail.com \
    --cc=sre@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®