[PATCH v4 26/58] mtd: rawnand: vf610: retain open-handle state on removal

From: James Hilliard

Date: Fri Nov 13 2026 - 14:08:50 EST


Keep chip metadata and controller storage alive until the final MTD
reference is released. Unregister and disconnect each published chip
before releasing the resources used by its hardware operations.

Keep managed register, IRQ and clock teardown after chip disconnection.

Run NAND cleanup during disconnection; the final core-owned put only
releases retained allocation and layout data.

Cc: Stefan Agner <stefan@xxxxxxxx>
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/mtd/nand/raw/vf610_nfc.c | 18 +++++++++++++-----
1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/drivers/mtd/nand/raw/vf610_nfc.c b/drivers/mtd/nand/raw/vf610_nfc.c
index 57da9eab940e..16e84ade92db 100644
--- a/drivers/mtd/nand/raw/vf610_nfc.c
+++ b/drivers/mtd/nand/raw/vf610_nfc.c
@@ -813,15 +813,17 @@ static int vf610_nfc_probe(struct platform_device *pdev)
int err;
int irq;

- nfc = devm_kzalloc(&pdev->dev, sizeof(*nfc), GFP_KERNEL);
+ nfc = kzalloc_obj(*nfc);
if (!nfc)
return -ENOMEM;
+ err = devm_nand_controller_init(&pdev->dev, &nfc->base, nfc);
+ if (err)
+ return err;

nfc->dev = &pdev->dev;
chip = &nfc->chip;
mtd = nand_to_mtd(chip);

- mtd->owner = THIS_MODULE;
mtd->parent_dev = nfc->dev;
mtd->name = DRV_NAME;

@@ -873,7 +875,6 @@ static int vf610_nfc_probe(struct platform_device *pdev)

vf610_nfc_preinit_controller(nfc);

- nand_controller_init(&nfc->base);
nfc->base.ops = &vf610_nfc_controller_ops;
chip->controller = &nfc->base;

@@ -885,9 +886,15 @@ static int vf610_nfc_probe(struct platform_device *pdev)
platform_set_drvdata(pdev, nfc);

/* Register device in MTD */
- err = mtd_device_register(mtd, NULL, 0);
+ err = nand_set_lifetime(chip, NULL);
if (err)
goto err_cleanup_nand;
+ err = mtd_device_register(mtd, NULL, 0);
+ if (err) {
+ nand_disconnect(chip);
+ mtd_put_master(mtd);
+ return err;
+ }
return 0;

err_cleanup_nand:
@@ -903,7 +910,8 @@ static void vf610_nfc_remove(struct platform_device *pdev)

ret = mtd_device_unregister(nand_to_mtd(chip));
WARN_ON(ret);
- nand_cleanup(chip);
+ nand_disconnect(chip);
+ mtd_put_master(nand_to_mtd(chip));
}

#ifdef CONFIG_PM_SLEEP

--
2.53.0