[PATCH v2 1/2] mtd: rawnand: parse GPIO CS with devm_gpiod_get_index_optional()

From: zhaofeng2

Date: Tue Nov 17 2026 - 21:13:10 EST


From: Zhao Feng <zhaofeng2@xxxxxxxxxxxxxxxxxxxx>

rawnand_dt_parse_gpio_cs() acquires the CS GPIO descriptors with
gpiod_get_index_optional() and hands them to the caller, which must
release them on error and at remove time. If a later loop iteration
fails, the descriptors acquired so far are leaked as well.

Acquire them with devm_gpiod_get_index_optional() instead, so the
device-managed framework releases them on probe failure and at device
removal, and its only caller, the arasan driver, can drop its manual
gpiod_put() bookkeeping.

Fixes: b85c943d181a ("mtd: rawnand: Add a helper to parse the gpio-cs DT property")
Signed-off-by: Zhao Feng <zhaofeng2@xxxxxxxxxxxxxxxxxxxx>
---
'cs_array' is already devm-allocated, so after this change both the
descriptors and the array are released by devres.

v2: respun to use devm_gpiod_get_index_optional() per Sashiko's review
and Miquel's request (managed version instead of manual rollback).

Verified against current upstream master. Not runtime-tested (no
hardware using gpio-cs).

drivers/mtd/nand/raw/nand_base.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

--- a/drivers/mtd/nand/raw/nand_base.c
+++ b/drivers/mtd/nand/raw/nand_base.c
@@ -5479,8 +5479,8 @@
return -ENOMEM;

for (i = 0; i < ndescs; i++) {
- descs[i] = gpiod_get_index_optional(dev, "cs", i,
- GPIOD_OUT_HIGH);
+ descs[i] = devm_gpiod_get_index_optional(dev, "cs", i,
+ GPIOD_OUT_HIGH);
if (IS_ERR(descs[i]))
return PTR_ERR(descs[i]);
}