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

From: James Hilliard

Date: Fri Nov 13 2026 - 15:38:03 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 geometry available after removal while releasing DMA and clocks.

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

Cc: Han Xu <han.xu@xxxxxxx>
Cc: imx@xxxxxxxxxxxxxxx
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/mtd/nand/raw/gpmi-nand/gpmi-nand.c | 17 +++++++++++++----
1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/drivers/mtd/nand/raw/gpmi-nand/gpmi-nand.c b/drivers/mtd/nand/raw/gpmi-nand/gpmi-nand.c
index 4822485bd512..8fc78e679f0f 100644
--- a/drivers/mtd/nand/raw/gpmi-nand/gpmi-nand.c
+++ b/drivers/mtd/nand/raw/gpmi-nand/gpmi-nand.c
@@ -2744,7 +2744,6 @@ static int gpmi_nand_init(struct gpmi_nand_data *this)
if (ret)
return ret;

- nand_controller_init(&this->base);
this->base.ops = &gpmi_nand_controller_ops;
chip->controller = &this->base;

@@ -2759,9 +2758,15 @@ static int gpmi_nand_init(struct gpmi_nand_data *this)
if (ret)
goto err_nand_cleanup;

- ret = mtd_device_register(mtd, NULL, 0);
+ ret = nand_set_lifetime(chip, NULL);
if (ret)
goto err_nand_cleanup;
+ ret = mtd_device_register(mtd, NULL, 0);
+ if (ret) {
+ nand_disconnect(chip);
+ mtd_put_master(mtd);
+ goto err_out;
+ }
return 0;

err_nand_cleanup:
@@ -2787,9 +2792,12 @@ static int gpmi_nand_probe(struct platform_device *pdev)
struct gpmi_nand_data *this;
int ret;

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

this->devdata = of_device_get_match_data(&pdev->dev);
platform_set_drvdata(pdev, this);
@@ -2838,7 +2846,7 @@ static void gpmi_nand_remove(struct platform_device *pdev)

ret = mtd_device_unregister(nand_to_mtd(chip));
WARN_ON(ret);
- nand_cleanup(chip);
+ nand_disconnect(chip);
gpmi_free_dma_buffer(this);
release_resources(this);
pm_runtime_dont_use_autosuspend(&pdev->dev);
@@ -2846,6 +2854,7 @@ static void gpmi_nand_remove(struct platform_device *pdev)
#ifndef CONFIG_PM
gpmi_disable_clk(this);
#endif
+ mtd_put_master(nand_to_mtd(chip));
}

static int gpmi_pm_suspend(struct device *dev)

--
2.53.0