* [PATCH 0/2] Add Maxim DS28E16 battery cycle counter
@ 2026-11-24 17:43 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:43 ` [PATCH 2/2] power: supply: Add Maxim DS28E16 battery cycle counter driver Armandas Kvietkus via B4 Relay
0 siblings, 2 replies; 5+ messages in thread
From: Armandas Kvietkus via B4 Relay @ 2026-11-24 17:43 UTC (permalink / raw)
To: Sebastian Reichel, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: linux-pm, devicetree, linux-kernel, Armandas Kvietkus
The DS28E16 is a battery authenticator that keeps the cycle count of the
pack in a decrement counter. This adds a binding and a power supply
driver that reads the count and lets userspace step it by one.
---
Armandas Kvietkus (2):
dt-bindings: power: supply: Add Maxim DS28E16
power: supply: Add Maxim DS28E16 battery cycle counter driver
.../bindings/power/supply/maxim,ds28e16.yaml | 39 +++
MAINTAINERS | 7 +
drivers/power/supply/Kconfig | 11 +
drivers/power/supply/Makefile | 1 +
drivers/power/supply/ds28e16_battery.c | 335 +++++++++++++++++++++
5 files changed, 393 insertions(+)
---
base-commit: abbe0a96bdbadae9e29489a1528fa436c55aca25
change-id: 20261124-ds28e16-dce0d381e12d
Best regards,
--
Armandas Kvietkus <armandas.kvietkus@proton.me>
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH 1/2] dt-bindings: power: supply: Add Maxim DS28E16 2026-11-24 17:43 [PATCH 0/2] Add Maxim DS28E16 battery cycle counter Armandas Kvietkus via B4 Relay @ 2026-11-24 17:43 ` 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 1 sibling, 1 reply; 5+ messages in thread From: Armandas Kvietkus via B4 Relay @ 2026-11-24 17:43 UTC (permalink / raw) To: Sebastian Reichel, Rob Herring, Krzysztof Kozlowski, Conor Dooley Cc: linux-pm, devicetree, linux-kernel, Armandas Kvietkus From: Armandas Kvietkus <armandas.kvietkus@proton.me> The DS28E16 is a 1-Wire secure authenticator found in some battery packs. Its decrement counter page holds the cycle count of the pack. Signed-off-by: Armandas Kvietkus <armandas.kvietkus@proton.me> --- .../bindings/power/supply/maxim,ds28e16.yaml | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/Documentation/devicetree/bindings/power/supply/maxim,ds28e16.yaml b/Documentation/devicetree/bindings/power/supply/maxim,ds28e16.yaml new file mode 100644 index 000000000000..b381bdf68239 --- /dev/null +++ b/Documentation/devicetree/bindings/power/supply/maxim,ds28e16.yaml @@ -0,0 +1,39 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/power/supply/maxim,ds28e16.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Maxim DS28E16 battery pack authenticator + +maintainers: + - Armandas Kvietkus <armandas.kvietkus@proton.me> + +description: + The DS28E16 is a secure authenticator inside some battery packs, for + example in Xiaomi SM8250 phones. It is connected to a GPIO that is used as + a 1-Wire data line. Page 2 of its memory holds a one way decrement counter + that stores the charge cycle count of the pack. + +properties: + compatible: + const: maxim,ds28e16 + + onewire-gpios: + maxItems: 1 + description: GPIO used as the 1-Wire data line. + +required: + - compatible + - onewire-gpios + +additionalProperties: false + +examples: + - | + #include <dt-bindings/gpio/gpio.h> + + battery-id { + compatible = "maxim,ds28e16"; + onewire-gpios = <&tlmm 25 GPIO_ACTIVE_HIGH>; + }; -- 2.56.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 1/2] dt-bindings: power: supply: Add Maxim DS28E16 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 0 siblings, 0 replies; 5+ messages in thread From: sashiko-bot @ 2026-11-24 17:50 UTC (permalink / raw) To: Armandas Kvietkus; +Cc: devicetree, robh, conor+dt Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] dt-bindings: power: supply: maxim,ds28e16 models 1-Wire slave as GPIO node -- commit e42cb4a20cd773ad55f015ac68fc65ab57afeaf9 Author: Armandas Kvietkus <armandas.kvietkus@proton.me> dt-bindings: power: supply: Add Maxim DS28E16 This commit adds a Device Tree binding schema for the Maxim DS28E16 1-Wire battery pack authenticator. > diff --git a/Documentation/devicetree/bindings/power/supply/maxim,ds28e16.yaml b/Documentation/devicetree/bindings/power/supply/maxim,ds28e16.yaml > new file mode 100644 > index 0000000000000..b381bdf68239a > --- /dev/null > +++ b/Documentation/devicetree/bindings/power/supply/maxim,ds28e16.yaml > @@ -0,0 +1,39 @@ [ ... ] > +properties: > + compatible: > + const: maxim,ds28e16 > + > + onewire-gpios: > + maxItems: 1 > + description: GPIO used as the 1-Wire data line. > + > +required: > + - compatible > + - onewire-gpios > + > +additionalProperties: false > + > +examples: > + - | > + #include <dt-bindings/gpio/gpio.h> > + > + battery-id { > + compatible = "maxim,ds28e16"; > + onewire-gpios = <&tlmm 25 GPIO_ACTIVE_HIGH>; > + }; [Severity: Medium] Should maxim,ds28e16 be modeled as a child node under a 1-Wire bus master controller rather than taking a custom onewire-gpios property directly? The DS28E16 is a 1-Wire slave device, while the GPIO belongs to the host acting as a bitbanged 1-Wire bus master (covered by Documentation/devicetree/bindings/w1/w1-gpio.yaml). Per Documentation/devicetree/bindings/w1/w1.txt and existing 1-Wire battery bindings such as Documentation/devicetree/bindings/power/supply/maxim,ds2760.yaml, 1-Wire slave devices are placed as sub-nodes of their 1-Wire bus master node so the binding reflects the hardware bus topology. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261124-ds28e16-v1-0-bed431df0578@proton.me?part=1 ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 2/2] power: supply: Add Maxim DS28E16 battery cycle counter driver 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:43 ` Armandas Kvietkus via B4 Relay 2026-11-24 17:58 ` sashiko-bot 1 sibling, 1 reply; 5+ messages in thread From: Armandas Kvietkus via B4 Relay @ 2026-11-24 17:43 UTC (permalink / raw) To: Sebastian Reichel, Rob Herring, Krzysztof Kozlowski, Conor Dooley Cc: linux-pm, devicetree, linux-kernel, Armandas Kvietkus From: Armandas Kvietkus <armandas.kvietkus@proton.me> Read the cycle count from the DS28E16 decrement counter and report it as a power supply property. Writing cycle_count steps the counter by one. The w1 core has no overdrive support, so the GPIO is driven directly. Signed-off-by: Armandas Kvietkus <armandas.kvietkus@proton.me> --- MAINTAINERS | 7 + drivers/power/supply/Kconfig | 11 ++ drivers/power/supply/Makefile | 1 + drivers/power/supply/ds28e16_battery.c | 335 +++++++++++++++++++++++++++++++++ 4 files changed, 354 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index d9e325244ac2..32cb7bd25395 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -16439,6 +16439,13 @@ S: Maintained F: Documentation/devicetree/bindings/iio/proximity/maxbotix,mb1232.yaml F: drivers/iio/proximity/mb1232.c +MAXIM DS28E16 BATTERY AUTHENTICATOR DRIVER +M: Armandas Kvietkus <armandas.kvietkus@proton.me> +L: linux-pm@vger.kernel.org +S: Maintained +F: Documentation/devicetree/bindings/power/supply/maxim,ds28e16.yaml +F: drivers/power/supply/ds28e16_battery.c + MAXIM GMSL2/3 SERIALIZERS AND DESERIALIZERS M: Cosmin Tanislav <cosmin.tanislav@analog.com> L: linux-media@vger.kernel.org diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig index 2d69f1adb951..5f787eaf8382 100644 --- a/drivers/power/supply/Kconfig +++ b/drivers/power/supply/Kconfig @@ -1183,6 +1183,17 @@ config BATTERY_CHARGER_SURFACE_RT This driver can also be built as a module. If so, the module will be called surface-rt-ec. +config BATTERY_DS28E16 + tristate "Maxim DS28E16 battery cycle counter" + depends on GPIOLIB && OF + help + Say Y here to enable the Maxim DS28E16 battery authenticator + driver. It reads the battery cycle count from the decrement + counter of the chip over a GPIO driven 1-Wire line. + + This driver can also be built as a module. If so, the module + will be called ds28e16_battery. + config CHARGER_QCOM_SMB2 tristate "Qualcomm PMI8998 PMIC charger driver" depends on MFD_SPMI_PMIC diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile index 459f9cbe5f30..c51eea9e82a6 100644 --- a/drivers/power/supply/Makefile +++ b/drivers/power/supply/Makefile @@ -136,3 +136,4 @@ obj-$(CONFIG_CHARGER_QCOM_SMB2) += qcom_smbx.o obj-$(CONFIG_FUEL_GAUGE_MM8013) += mm8013.o obj-$(CONFIG_MACSMC_POWER) += macsmc-power.o obj-$(CONFIG_CHARGER_MAX77759) += max77759_charger.o +obj-$(CONFIG_BATTERY_DS28E16) += ds28e16_battery.o diff --git a/drivers/power/supply/ds28e16_battery.c b/drivers/power/supply/ds28e16_battery.c new file mode 100644 index 000000000000..f23201eb6369 --- /dev/null +++ b/drivers/power/supply/ds28e16_battery.c @@ -0,0 +1,335 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include <linux/delay.h> +#include <linux/gpio/consumer.h> +#include <linux/irqflags.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/mod_devicetable.h> +#include <linux/platform_device.h> +#include <linux/power_supply.h> +#include <linux/property.h> +#include <linux/string.h> + +#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; +} + +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; +} + +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); +} + +static u8 ds28e16_read_byte(struct ds28e16 *ds) +{ + unsigned long flags; + u8 v = 0; + int i; + + local_irq_save(flags); + for (i = 0; i < 8; i++) + if (ds28e16_read_bit(ds)) + v |= BIT(i); + local_irq_restore(flags); + + return v; +} + +static void ds28e16_write_byte(struct ds28e16 *ds, u8 v) +{ + unsigned long flags; + int i; + + local_irq_save(flags); + gpiod_direction_output_raw(ds->gpio, 1); + for (i = 0; i < 8; i++) + ds28e16_write_bit(ds, (v >> i) & 1); + local_irq_restore(flags); +} + +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; +} + +static int ds28e16_cmd(struct ds28e16 *ds, const u8 *cmd, int n, + unsigned int delay_ms, u8 *rsp, int rsp_len) +{ + u8 buf[DS28E16_PAGE_LEN + 4]; + u16 crc = 0; + int i, len, ret; + + ret = ds28e16_reset(ds); + if (ret) + return ret; + + ds28e16_write_byte(ds, DS28E16_CMD_SKIP_ROM); + ds28e16_write_byte(ds, DS28E16_CMD_START); + crc = ds28e16_crc16(crc, DS28E16_CMD_START); + for (i = 0; i < n; i++) { + ds28e16_write_byte(ds, cmd[i]); + crc = ds28e16_crc16(crc, cmd[i]); + } + crc = ds28e16_crc16(crc, ds28e16_read_byte(ds)); + crc = ds28e16_crc16(crc, ds28e16_read_byte(ds)); + if (crc != 0xb001) + return -EIO; + + ds28e16_write_byte(ds, DS28E16_CMD_RELEASE); + msleep(delay_ms); + + ds28e16_read_byte(ds); + len = ds28e16_read_byte(ds); + if (len != rsp_len + 1) + return -EIO; + + for (i = 0; i < len + 2; i++) + buf[i] = ds28e16_read_byte(ds); + + crc = ds28e16_crc16(0, len); + for (i = 0; i < len + 2; i++) + crc = ds28e16_crc16(crc, buf[i]); + if (crc != 0xb001) + return -EIO; + + if (buf[0] != DS28E16_RESULT_OK) + return -EIO; + + if (rsp_len) + memcpy(rsp, buf + 1, rsp_len); + + return 0; +} + +static int ds28e16_read_page(struct ds28e16 *ds, u8 page, u8 *out) +{ + u8 cmd[3] = { 2, DS28E16_CMD_READ_MEM, page & 3 }; + + return ds28e16_cmd(ds, cmd, sizeof(cmd), DS28E16_READ_DELAY_MS, out, + DS28E16_PAGE_LEN); +} + +static int ds28e16_decrement(struct ds28e16 *ds) +{ + u8 cmd[2] = { 1, DS28E16_CMD_DECREMENT }; + + return ds28e16_cmd(ds, cmd, sizeof(cmd), DS28E16_DEC_DELAY_MS, NULL, 0); +} + +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; +} + +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; + default: + return -EINVAL; + } +} + +static int ds28e16_set_property(struct power_supply *psy, + enum power_supply_property psp, + const union power_supply_propval *val) +{ + struct ds28e16 *ds = power_supply_get_drvdata(psy); + int cur, now, ret; + + if (psp != POWER_SUPPLY_PROP_CYCLE_COUNT) + return -EINVAL; + + guard(mutex)(&ds->lock); + cur = ds28e16_read_cycle_count(ds); + if (cur < 0) { + gpiod_direction_input(ds->gpio); + return cur; + } + if (val->intval != cur + 1) { + gpiod_direction_input(ds->gpio); + return -EINVAL; + } + + ret = ds28e16_decrement(ds); + now = ds28e16_read_cycle_count(ds); + gpiod_direction_input(ds->gpio); + ds->valid = now >= 0; + if (ds->valid) + ds->cycle_count = now; + dev_info(ds->dev, "cycle count %d -> %d, decrement %d\n", cur, now, ret); + if (now < 0) + return now; + + return now == cur + 1 ? 0 : -EIO; +} + +static int ds28e16_property_is_writeable(struct power_supply *psy, + enum power_supply_property psp) +{ + return psp == POWER_SUPPLY_PROP_CYCLE_COUNT; +} + +static const enum power_supply_property ds28e16_props[] = { + POWER_SUPPLY_PROP_CYCLE_COUNT, + POWER_SUPPLY_PROP_PRESENT, +}; + +static const struct power_supply_desc ds28e16_psy_desc = { + .name = "battery-id", + .type = POWER_SUPPLY_TYPE_UNKNOWN, + .properties = ds28e16_props, + .num_properties = ARRAY_SIZE(ds28e16_props), + .get_property = ds28e16_get_property, + .set_property = ds28e16_set_property, + .property_is_writeable = ds28e16_property_is_writeable, +}; + +static int ds28e16_probe(struct platform_device *pdev) +{ + struct power_supply_config cfg = {}; + struct ds28e16 *ds; + int ret; + + ds = devm_kzalloc(&pdev->dev, sizeof(*ds), GFP_KERNEL); + if (!ds) + return -ENOMEM; + + ds->dev = &pdev->dev; + ds->gpio = devm_gpiod_get(&pdev->dev, "onewire", GPIOD_IN); + if (IS_ERR(ds->gpio)) + return dev_err_probe(&pdev->dev, PTR_ERR(ds->gpio), + "no onewire gpio\n"); + + ret = devm_mutex_init(&pdev->dev, &ds->lock); + if (ret) + return ret; + + cfg.drv_data = ds; + cfg.fwnode = dev_fwnode(&pdev->dev); + ds->psy = devm_power_supply_register(&pdev->dev, &ds28e16_psy_desc, + &cfg); + if (IS_ERR(ds->psy)) + return PTR_ERR(ds->psy); + + return 0; +} + +static const struct of_device_id ds28e16_of_match[] = { + { .compatible = "maxim,ds28e16" }, + { } +}; +MODULE_DEVICE_TABLE(of, ds28e16_of_match); + +static struct platform_driver ds28e16_driver = { + .probe = ds28e16_probe, + .driver = { + .name = "ds28e16-battery", + .of_match_table = ds28e16_of_match, + }, +}; +module_platform_driver(ds28e16_driver); + +MODULE_AUTHOR("Armandas Kvietkus <armandas.kvietkus@proton.me>"); +MODULE_DESCRIPTION("Maxim DS28E16 battery cycle counter"); +MODULE_LICENSE("GPL"); -- 2.56.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 2/2] power: supply: Add Maxim DS28E16 battery cycle counter driver 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 0 siblings, 0 replies; 5+ messages in thread From: sashiko-bot @ 2026-11-24 17:58 UTC (permalink / raw) To: Armandas Kvietkus; +Cc: devicetree, robh, conor+dt 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 ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-24 17:58 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 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 is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox