[PATCH] regmap: mmio: prepare/unprepare clk only when read/write

From: peng . fan
Date: Thu Apr 23 2020 - 01:55:16 EST


From: Peng Fan <peng.fan@xxxxxxx>

use clk_prepare at the very beginning will invoke pm runtime resume,
if the clk is binded with a power domain. This will cause runtime
power high. Let's use clk_prepare_enable and clk_disable_unprepare
when read/write to avoid the runtime power issue.

Signed-off-by: Peng Fan <peng.fan@xxxxxxx>
---
drivers/base/regmap/regmap-mmio.c | 19 +++++--------------
1 file changed, 5 insertions(+), 14 deletions(-)

diff --git a/drivers/base/regmap/regmap-mmio.c b/drivers/base/regmap/regmap-mmio.c
index af967d8f975e..a1ad7419c4a3 100644
--- a/drivers/base/regmap/regmap-mmio.c
+++ b/drivers/base/regmap/regmap-mmio.c
@@ -118,7 +118,7 @@ static int regmap_mmio_write(void *context, unsigned int reg, unsigned int val)
int ret;

if (!IS_ERR(ctx->clk)) {
- ret = clk_enable(ctx->clk);
+ ret = clk_prepare_enable(ctx->clk);
if (ret < 0)
return ret;
}
@@ -126,7 +126,7 @@ static int regmap_mmio_write(void *context, unsigned int reg, unsigned int val)
ctx->reg_write(ctx, reg, val);

if (!IS_ERR(ctx->clk))
- clk_disable(ctx->clk);
+ clk_disable_unprepare(ctx->clk);

return 0;
}
@@ -175,7 +175,7 @@ static int regmap_mmio_read(void *context, unsigned int reg, unsigned int *val)
int ret;

if (!IS_ERR(ctx->clk)) {
- ret = clk_enable(ctx->clk);
+ ret = clk_prepare_enable(ctx->clk);
if (ret < 0)
return ret;
}
@@ -183,7 +183,7 @@ static int regmap_mmio_read(void *context, unsigned int reg, unsigned int *val)
*val = ctx->reg_read(ctx, reg);

if (!IS_ERR(ctx->clk))
- clk_disable(ctx->clk);
+ clk_disable_unprepare(ctx->clk);

return 0;
}
@@ -193,7 +193,6 @@ static void regmap_mmio_free_context(void *context)
struct regmap_mmio_context *ctx = context;

if (!IS_ERR(ctx->clk)) {
- clk_unprepare(ctx->clk);
if (!ctx->attached_clk)
clk_put(ctx->clk);
}
@@ -305,12 +304,6 @@ static struct regmap_mmio_context *regmap_mmio_gen_context(struct device *dev,
goto err_free;
}

- ret = clk_prepare(ctx->clk);
- if (ret < 0) {
- clk_put(ctx->clk);
- goto err_free;
- }
-
return ctx;

err_free:
@@ -361,7 +354,7 @@ int regmap_mmio_attach_clk(struct regmap *map, struct clk *clk)
ctx->clk = clk;
ctx->attached_clk = true;

- return clk_prepare(ctx->clk);
+ return 0;
}
EXPORT_SYMBOL_GPL(regmap_mmio_attach_clk);

@@ -369,8 +362,6 @@ void regmap_mmio_detach_clk(struct regmap *map)
{
struct regmap_mmio_context *ctx = map->bus_context;

- clk_unprepare(ctx->clk);
-
ctx->attached_clk = false;
ctx->clk = NULL;
}
--
2.16.4