media: imx-jpeg: Add pm-runtime support for imx-jpeg
authorMirela Rabulea <mirela.rabulea@oss.nxp.com>
Thu, 7 Oct 2021 16:30:38 +0000 (17:30 +0100)
committerMauro Carvalho Chehab <mchehab+huawei@kernel.org>
Tue, 19 Oct 2021 07:08:39 +0000 (08:08 +0100)
Save some power by disabling/enabling the jpeg clocks with
every stream stop/start.
Do not use DL_FLAG_RPM_ACTIVE in mxc_jpeg_attach_pm_domains,
to ensure power domains are off after probe.

Signed-off-by: Mirela Rabulea <mirela.rabulea@oss.nxp.com>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
drivers/media/platform/imx-jpeg/mxc-jpeg.c
drivers/media/platform/imx-jpeg/mxc-jpeg.h

index e45b0c7..4ca96cf 100644 (file)
@@ -49,6 +49,7 @@
 #include <linux/slab.h>
 #include <linux/irqreturn.h>
 #include <linux/interrupt.h>
+#include <linux/pm_runtime.h>
 #include <linux/pm_domain.h>
 #include <linux/string.h>
 
@@ -1078,10 +1079,17 @@ static int mxc_jpeg_start_streaming(struct vb2_queue *q, unsigned int count)
 {
        struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(q);
        struct mxc_jpeg_q_data *q_data = mxc_jpeg_get_q_data(ctx, q->type);
+       int ret;
 
        dev_dbg(ctx->mxc_jpeg->dev, "Start streaming ctx=%p", ctx);
        q_data->sequence = 0;
 
+       ret = pm_runtime_resume_and_get(ctx->mxc_jpeg->dev);
+       if (ret < 0) {
+               dev_err(ctx->mxc_jpeg->dev, "Failed to power up jpeg\n");
+               return ret;
+       }
+
        return 0;
 }
 
@@ -1099,9 +1107,10 @@ static void mxc_jpeg_stop_streaming(struct vb2_queue *q)
                else
                        vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
                if (!vbuf)
-                       return;
+                       break;
                v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
        }
+       pm_runtime_put_sync(&ctx->mxc_jpeg->pdev->dev);
 }
 
 static int mxc_jpeg_valid_comp_id(struct device *dev,
@@ -1961,8 +1970,7 @@ static int mxc_jpeg_attach_pm_domains(struct mxc_jpeg_dev *jpeg)
 
                jpeg->pd_link[i] = device_link_add(dev, jpeg->pd_dev[i],
                                                   DL_FLAG_STATELESS |
-                                                  DL_FLAG_PM_RUNTIME |
-                                                  DL_FLAG_RPM_ACTIVE);
+                                                  DL_FLAG_PM_RUNTIME);
                if (!jpeg->pd_link[i]) {
                        ret = -EINVAL;
                        goto fail;
@@ -2025,6 +2033,19 @@ static int mxc_jpeg_probe(struct platform_device *pdev)
        jpeg->dev = dev;
        jpeg->mode = mode;
 
+       /* Get clocks */
+       jpeg->clk_ipg = devm_clk_get(dev, "ipg");
+       if (IS_ERR(jpeg->clk_ipg)) {
+               dev_err(dev, "failed to get clock: ipg\n");
+               goto err_clk;
+       }
+
+       jpeg->clk_per = devm_clk_get(dev, "per");
+       if (IS_ERR(jpeg->clk_per)) {
+               dev_err(dev, "failed to get clock: per\n");
+               goto err_clk;
+       }
+
        ret = mxc_jpeg_attach_pm_domains(jpeg);
        if (ret < 0) {
                dev_err(dev, "failed to attach power domains %d\n", ret);
@@ -2093,6 +2114,7 @@ static int mxc_jpeg_probe(struct platform_device *pdev)
                          jpeg->dec_vdev->minor);
 
        platform_set_drvdata(pdev, jpeg);
+       pm_runtime_enable(dev);
 
        return 0;
 
@@ -2109,9 +2131,52 @@ err_register:
        mxc_jpeg_detach_pm_domains(jpeg);
 
 err_irq:
+err_clk:
+       return ret;
+}
+
+#ifdef CONFIG_PM
+static int mxc_jpeg_runtime_resume(struct device *dev)
+{
+       struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev);
+       int ret;
+
+       ret = clk_prepare_enable(jpeg->clk_ipg);
+       if (ret < 0) {
+               dev_err(dev, "failed to enable clock: ipg\n");
+               goto err_ipg;
+       }
+
+       ret = clk_prepare_enable(jpeg->clk_per);
+       if (ret < 0) {
+               dev_err(dev, "failed to enable clock: per\n");
+               goto err_per;
+       }
+
+       return 0;
+
+err_per:
+       clk_disable_unprepare(jpeg->clk_ipg);
+err_ipg:
        return ret;
 }
 
+static int mxc_jpeg_runtime_suspend(struct device *dev)
+{
+       struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev);
+
+       clk_disable_unprepare(jpeg->clk_ipg);
+       clk_disable_unprepare(jpeg->clk_per);
+
+       return 0;
+}
+#endif
+
+static const struct dev_pm_ops mxc_jpeg_pm_ops = {
+       SET_RUNTIME_PM_OPS(mxc_jpeg_runtime_suspend,
+                          mxc_jpeg_runtime_resume, NULL)
+};
+
 static int mxc_jpeg_remove(struct platform_device *pdev)
 {
        unsigned int slot;
@@ -2120,6 +2185,7 @@ static int mxc_jpeg_remove(struct platform_device *pdev)
        for (slot = 0; slot < MXC_MAX_SLOTS; slot++)
                mxc_jpeg_free_slot_data(jpeg, slot);
 
+       pm_runtime_disable(&pdev->dev);
        video_unregister_device(jpeg->dec_vdev);
        v4l2_m2m_release(jpeg->m2m_dev);
        v4l2_device_unregister(&jpeg->v4l2_dev);
@@ -2136,6 +2202,7 @@ static struct platform_driver mxc_jpeg_driver = {
        .driver = {
                .name = "mxc-jpeg",
                .of_match_table = mxc_jpeg_match,
+               .pm = &mxc_jpeg_pm_ops,
        },
 };
 module_platform_driver(mxc_jpeg_driver);
index 4c21085..9fb2a5a 100644 (file)
@@ -109,6 +109,8 @@ struct mxc_jpeg_dev {
        spinlock_t                      hw_lock; /* hardware access lock */
        unsigned int                    mode;
        struct mutex                    lock; /* v4l2 ioctls serialization */
+       struct clk                      *clk_ipg;
+       struct clk                      *clk_per;
        struct platform_device          *pdev;
        struct device                   *dev;
        void __iomem                    *base_reg;