[PATCH v4 06/18] media: verisilicon: Disable runtime PM before unpreparing the clocks on probe failure

From: Arun Iyengar via B4 Relay

Date: Sun Dec 06 2026 - 00:58:47 EST


From: Arun Iyengar <aiyengar1990@xxxxxxxxx>

The probe error path unprepares the clocks before it disables runtime
PM, so a VPU resumed through its video nodes before a late failure
keeps its clocks enabled, or suspends after they are unprepared.
Prepare the clocks before enabling runtime PM, and unwind like
hantro_remove(): suspend first, then unprepare.

Fixes: 775fec69008d ("media: add Rockchip VPU JPEG encoder driver")
Signed-off-by: Arun Iyengar <aiyengar1990@xxxxxxxxx>
---
drivers/media/platform/verisilicon/hantro_drv.c | 53 ++++++++++++++-----------
1 file changed, 30 insertions(+), 23 deletions(-)

diff --git a/drivers/media/platform/verisilicon/hantro_drv.c b/drivers/media/platform/verisilicon/hantro_drv.c
index a5d0c09a3534..957ed7a59197 100644
--- a/drivers/media/platform/verisilicon/hantro_drv.c
+++ b/drivers/media/platform/verisilicon/hantro_drv.c
@@ -1102,6 +1102,25 @@ static struct v4l2_m2m_dev *hantro_get_v4l2_m2m_dev(struct hantro_dev *vpu)
return v4l2_m2m_init(&vpu_m2m_ops);
}

+static int hantro_runtime_suspend(struct device *dev)
+{
+ struct hantro_dev *vpu = dev_get_drvdata(dev);
+
+ clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
+ return 0;
+}
+
+static void hantro_disable_runtime_pm(struct hantro_dev *vpu)
+{
+ pm_runtime_dont_use_autosuspend(vpu->dev);
+ pm_runtime_disable(vpu->dev);
+ /* Runtime PM forbidden through sysfs: the device is still active. */
+ if (IS_ENABLED(CONFIG_PM) && !pm_runtime_status_suspended(vpu->dev)) {
+ hantro_runtime_suspend(vpu->dev);
+ pm_runtime_set_suspended(vpu->dev);
+ }
+}
+
static int hantro_probe(struct platform_device *pdev)
{
struct hantro_dev *vpu;
@@ -1211,6 +1230,12 @@ static int hantro_probe(struct platform_device *pdev)
}
}

+ ret = clk_bulk_prepare(vpu->variant->num_clocks, vpu->clocks);
+ if (ret) {
+ dev_err(&pdev->dev, "Failed to prepare clocks\n");
+ return ret;
+ }
+
pm_runtime_set_autosuspend_delay(vpu->dev, 100);
pm_runtime_use_autosuspend(vpu->dev);
pm_runtime_enable(vpu->dev);
@@ -1221,16 +1246,10 @@ static int hantro_probe(struct platform_device *pdev)
goto err_pm_disable;
}

- ret = clk_bulk_prepare(vpu->variant->num_clocks, vpu->clocks);
- if (ret) {
- dev_err(&pdev->dev, "Failed to prepare clocks\n");
- goto err_pm_disable;
- }
-
ret = v4l2_device_register(&pdev->dev, &vpu->v4l2_dev);
if (ret) {
dev_err(&pdev->dev, "Failed to register v4l2 device\n");
- goto err_clk_unprepare;
+ goto err_pm_disable;
}
platform_set_drvdata(pdev, vpu);

@@ -1275,23 +1294,17 @@ static int hantro_probe(struct platform_device *pdev)
media_device_cleanup(&vpu->mdev);
v4l2_m2m_put(vpu->m2m_dev);
err_v4l2_unreg:
+ hantro_disable_runtime_pm(vpu);
v4l2_device_unregister(&vpu->v4l2_dev);
-err_clk_unprepare:
clk_bulk_unprepare(vpu->variant->num_clocks, vpu->clocks);
+ return ret;
err_pm_disable:
pm_runtime_dont_use_autosuspend(vpu->dev);
pm_runtime_disable(vpu->dev);
+ clk_bulk_unprepare(vpu->variant->num_clocks, vpu->clocks);
return ret;
}

-static int hantro_runtime_suspend(struct device *dev)
-{
- struct hantro_dev *vpu = dev_get_drvdata(dev);
-
- clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
- return 0;
-}
-
static void hantro_remove(struct platform_device *pdev)
{
struct hantro_dev *vpu = platform_get_drvdata(pdev);
@@ -1304,13 +1317,7 @@ static void hantro_remove(struct platform_device *pdev)
media_device_cleanup(&vpu->mdev);
v4l2_m2m_put(vpu->m2m_dev);
/* Suspend first: v4l2_device_unregister() clears the driver data. */
- pm_runtime_dont_use_autosuspend(vpu->dev);
- pm_runtime_disable(vpu->dev);
- /* Runtime PM forbidden through sysfs: the device is still active. */
- if (IS_ENABLED(CONFIG_PM) && !pm_runtime_status_suspended(vpu->dev)) {
- hantro_runtime_suspend(vpu->dev);
- pm_runtime_set_suspended(vpu->dev);
- }
+ hantro_disable_runtime_pm(vpu);
v4l2_device_unregister(&vpu->v4l2_dev);
clk_bulk_unprepare(vpu->variant->num_clocks, vpu->clocks);
}

--
2.55.0