Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Armandas Kvietkus" <armandas.kvietkus@proton.me>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH 2/2] power: supply: Add Maxim DS28E16 battery cycle counter driver
Date: Tue, 24 Nov 2026 17:58:17 +0000	[thread overview]
Message-ID: <sashiko-outbox-231760@kernel.org> (raw)
In-Reply-To: <20261124-ds28e16-v1-2-bed431df0578@proton.me>

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] power: supply: ds28e16: sleeping GPIO direction calls under
  local_irq_save()
- [Medium] power: supply: ds28e16: unmasked counter underflow in
  ds28e16_set_property()
- [Medium] power: supply: ds28e16: push-pull high drive in
  ds28e16_read_bit()
- [Low] power: supply: ds28e16: raw GPIO accessors bypass DT flags in
  1-Wire I/O
- [Low] power: supply: ds28e16: duplicate crc16() in ds28e16_crc16() and
  unused macro
- [Low] power: supply: ds28e16: hardcoded PRESENT=1 and undocumented
  writable ABI
--

commit 1d14a8e7dbd2abdb56c7c466b16af18109974ace
Author: Armandas Kvietkus <armandas.kvietkus@proton.me>

power: supply: Add Maxim DS28E16 battery cycle counter driver

This commit adds a power supply driver for the Maxim DS28E16 battery
authenticator that reads and increments the battery cycle count over a
GPIO-driven 1-Wire line.

> diff --git a/drivers/power/supply/ds28e16_battery.c b/drivers/power/supply/ds28e16_battery.c
> new file mode 100644
> index 0000000000000..f23201eb63694
> --- /dev/null
> +++ b/drivers/power/supply/ds28e16_battery.c
> @@ -0,0 +1,335 @@

[ ... ]

> +#define DS28E16_CMD_READ_ROM	0x33
> +#define DS28E16_CMD_SKIP_ROM	0xcc
> +#define DS28E16_CMD_START	0x66
> +#define DS28E16_CMD_READ_MEM	0x44
> +#define DS28E16_CMD_RELEASE	0xaa
> +#define DS28E16_CMD_DECREMENT	0xc9
> +#define DS28E16_RESULT_OK	0xaa
> +#define DS28E16_DC_PAGE		2
> +#define DS28E16_DC_INIT		0x1ffff
> +#define DS28E16_PAGE_LEN	32
> +#define DS28E16_READ_DELAY_MS	50
> +#define DS28E16_DEC_DELAY_MS	50
> +#define DS28E16_TRIES		10
> +
> +struct ds28e16 {
> +	struct device *dev;
> +	struct gpio_desc *gpio;
> +	struct power_supply *psy;
> +	struct mutex lock;
> +	int cycle_count;
> +	bool valid;
> +};
> +
> +static int ds28e16_reset(struct ds28e16 *ds)
> +{
> +	unsigned long flags;
> +	int presence;
> +
> +	local_irq_save(flags);
> +	gpiod_direction_output_raw(ds->gpio, 0);
> +	udelay(50);
> +	gpiod_direction_input(ds->gpio);
> +	udelay(7);
> +	presence = gpiod_get_raw_value(ds->gpio);
> +	udelay(50);
> +	local_irq_restore(flags);
> +
> +	return presence ? -ENODEV : 0;
> +}

[Severity: High]
Can gpiod_direction_output_raw() and gpiod_direction_input() be called
under local_irq_save() here, in ds28e16_read_byte() -> ds28e16_read_bit(),
and in ds28e16_write_byte()?

Both helpers call gpiod_line_state_notify(), which takes the non-raw
rwlock_t desc->gdev->line_state_lock (triggering
CONFIG_PROVE_RAW_LOCK_NESTING splats and sleeping in atomic context on
PREEMPT_RT) and runs line-state notifiers 24 times per read byte inside
the 1 us and 5 us 1-Wire Overdrive bit windows.  gpiod_direction_input()
also calls gpio_set_bias(), which takes pctldev->mutex on pinctrl-backed
GPIOs when bias flags are set, and ds28e16_probe() never checks
gpiod_cansleep(ds->gpio) to reject sleeping GPIO expanders.

> +
> +static int ds28e16_read_bit(struct ds28e16 *ds)
> +{
> +	int v;
> +
> +	gpiod_direction_output_raw(ds->gpio, 0);
> +	udelay(1);
> +	gpiod_direction_input(ds->gpio);
> +	v = gpiod_get_raw_value(ds->gpio);
> +	udelay(5);
> +	gpiod_direction_output_raw(ds->gpio, 1);
> +	udelay(6);
> +
> +	return v;
> +}

[Severity: Medium]
Does gpiod_direction_output_raw(ds->gpio, 1) cause bus contention on the
open-drain 1-Wire line when reading a 0 bit?

In 1-Wire Overdrive mode, the DS28E16 pulls the line low for 6 to 16 us to
transmit a 0, so actively driving the GPIO high in push-pull mode 5 us
after releasing the line shorts the master's output stage against the
slave's pulldown transistor.

> +
> +static void ds28e16_write_bit(struct ds28e16 *ds, int bit)
> +{
> +	gpiod_set_raw_value(ds->gpio, 0);
> +	udelay(1);
> +	if (bit)
> +		gpiod_set_raw_value(ds->gpio, 1);
> +	udelay(10);
> +	gpiod_set_raw_value(ds->gpio, 1);
> +	udelay(6);
> +}

[Severity: Low]
Should ds28e16_reset(), ds28e16_read_bit(), ds28e16_write_bit(), and
ds28e16_write_byte() use the logical gpiod_direction_output(),
gpiod_get_value(), and gpiod_set_value() helpers instead of the _raw
variants?  Using raw accessors bypasses GPIO polarity and open-drain flags
from Device Tree.

[ ... ]

> +static u16 ds28e16_crc16(u16 crc, u8 data)
> +{
> +	static const u8 oddparity[16] = {
> +		0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0
> +	};
> +	u16 d = (data ^ (crc & 0xff)) & 0xff;
> +
> +	crc >>= 8;
> +	if (oddparity[d & 0xf] ^ oddparity[d >> 4])
> +		crc ^= 0xc001;
> +	d <<= 6;
> +	crc ^= d;
> +	d <<= 1;
> +	crc ^= d;
> +
> +	return crc;
> +}

[Severity: Low]
This isn't a bug, but ds28e16_crc16() duplicates crc16() from
<linux/crc16.h> (with select CRC16 in Kconfig).  Also,
DS28E16_CMD_READ_ROM is unused.

[ ... ]

> +static int ds28e16_read_cycle_count(struct ds28e16 *ds)
> +{
> +	u8 page[DS28E16_PAGE_LEN];
> +	int i, ret = -EIO;
> +
> +	for (i = 0; i < DS28E16_TRIES; i++) {
> +		ret = ds28e16_read_page(ds, DS28E16_DC_PAGE, page);
> +		if (!ret)
> +			return DS28E16_DC_INIT -
> +			       (page[0] | page[1] << 8 | page[2] << 16);
> +	}
> +
> +	return ret;
> +}

[Severity: Medium]
Can ds28e16_read_cycle_count() return a negative non-errno value when any
of bits 7..1 in page[2] are set?

DS28E16_DC_INIT is 0x1ffff (17 bits), but page[0..2] forms an unmasked
24-bit value, so non-zero upper bits in page[2] produce values down to
-0xfe0000 that ds28e16_set_property() returns directly to VFS as an error
code.  In addition, ds28e16_set_property() does not reject writes when cur
has reached DS28E16_DC_INIT and the hardware decrement counter is already
0.

> +
> +static int ds28e16_get_property(struct power_supply *psy,
> +				enum power_supply_property psp,
> +				union power_supply_propval *val)
> +{
> +	struct ds28e16 *ds = power_supply_get_drvdata(psy);
> +	int ret;
> +
> +	switch (psp) {
> +	case POWER_SUPPLY_PROP_CYCLE_COUNT: {
> +		guard(mutex)(&ds->lock);
> +		if (!ds->valid) {
> +			ret = ds28e16_read_cycle_count(ds);
> +			gpiod_direction_input(ds->gpio);
> +			if (ret < 0)
> +				return -ENODATA;
> +			ds->cycle_count = ret;
> +			ds->valid = true;
> +		}
> +		val->intval = ds->cycle_count;
> +		return 0;
> +	}
> +	case POWER_SUPPLY_PROP_PRESENT:
> +		val->intval = 1;
> +		return 0;

[Severity: Low]
Should POWER_SUPPLY_PROP_PRESENT check whether the DS28E16 responds on the
1-Wire bus instead of always reporting 1 when no battery pack is
connected?

Also, ds28e16_psy_desc makes POWER_SUPPLY_PROP_CYCLE_COUNT writable on a
POWER_SUPPLY_TYPE_UNKNOWN supply, while
Documentation/ABI/testing/sysfs-class-power documents cycle_count as
read-only.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124-ds28e16-v1-0-bed431df0578@proton.me?part=2

      reply	other threads:[~2026-11-24 17:58 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 17:43 [PATCH 0/2] Add Maxim DS28E16 battery cycle counter Armandas Kvietkus via B4 Relay
2026-11-24 17:43 ` [PATCH 1/2] dt-bindings: power: supply: Add Maxim DS28E16 Armandas Kvietkus via B4 Relay
2026-11-24 17:50   ` sashiko-bot
2026-11-24 17:43 ` [PATCH 2/2] power: supply: Add Maxim DS28E16 battery cycle counter driver Armandas Kvietkus via B4 Relay
2026-11-24 17:58   ` sashiko-bot [this message]

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=sashiko-outbox-231760@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=armandas.kvietkus@proton.me \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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