diff mbox series

[v2,1/3] clk: generalize devm_clk_get() a bit

Message ID 20210301110821.1445756-2-uwe@kleine-koenig.org
State Superseded
Headers show
Series clk: provide new devm helpers for prepared and enabled clocks | expand

Commit Message

Uwe Kleine-König March 1, 2021, 11:08 a.m. UTC
From: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>

Allow to add an exit hook to devm managed clocks. Also use
clk_get_optional() in devm_clk_get_optional instead of open coding it.
The generalisation will be used in the next commit to add some more
devm_clk helpers.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/clk/clk-devres.c | 75 +++++++++++++++++++++++++++++-----------
 1 file changed, 54 insertions(+), 21 deletions(-)

Comments

kernel test robot March 1, 2021, 12:41 p.m. UTC | #1
Hi "Uwe,

I love your patch! Perhaps something to improve:

[auto build test WARNING on fe07bfda2fb9cdef8a4d4008a409bb02f35f1bd8]

url:    https://github.com/0day-ci/linux/commits/Uwe-Kleine-K-nig/clk-provide-new-devm-helpers-for-prepared-and-enabled-clocks/20210301-191522
base:   fe07bfda2fb9cdef8a4d4008a409bb02f35f1bd8
config: nds32-defconfig (attached as .config)
compiler: nds32le-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/0day-ci/linux/commit/bf2779180284ed39480360900c07ce553f75e06a
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Uwe-Kleine-K-nig/clk-provide-new-devm-helpers-for-prepared-and-enabled-clocks/20210301-191522
        git checkout bf2779180284ed39480360900c07ce553f75e06a
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=nds32 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

>> drivers/clk/clk-devres.c:22:13: warning: no previous prototype for '__devm_clk_get' [-Wmissing-prototypes]
      22 | struct clk *__devm_clk_get(struct device *dev, const char *id,
         |             ^~~~~~~~~~~~~~


vim +/__devm_clk_get +22 drivers/clk/clk-devres.c

    21	
  > 22	struct clk *__devm_clk_get(struct device *dev, const char *id,
    23				   struct clk *(*get)(struct device *dev, const char *id),
    24				   int (*init)(struct clk *clk),
    25				   void (*exit)(struct clk *clk))
    26	{
    27		struct devm_clk_state *state;
    28		struct clk *clk;
    29		int ret;
    30	
    31		state = devres_alloc(devm_clk_release, sizeof(*state), GFP_KERNEL);
    32		if (!state)
    33			return ERR_PTR(-ENOMEM);
    34	
    35		clk = get(dev, id);
    36		if (IS_ERR(clk)) {
    37			ret = PTR_ERR(clk);
    38			goto err_clk_get;
    39		}
    40	
    41		if (init) {
    42			ret = init(clk);
    43			if (ret)
    44				goto err_clk_init;
    45		}
    46	
    47		state->clk = clk;
    48		state->exit = exit;
    49	
    50		devres_add(dev, state);
    51	
    52		return clk;
    53	
    54	err_clk_init:
    55	
    56		clk_put(clk);
    57	err_clk_get:
    58	
    59		devres_free(state);
    60		return ERR_PTR(ret);
    61	}
    62	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
kernel test robot March 1, 2021, 12:46 p.m. UTC | #2
Hi "Uwe,

I love your patch! Perhaps something to improve:

[auto build test WARNING on fe07bfda2fb9cdef8a4d4008a409bb02f35f1bd8]

url:    https://github.com/0day-ci/linux/commits/Uwe-Kleine-K-nig/clk-provide-new-devm-helpers-for-prepared-and-enabled-clocks/20210301-191522
base:   fe07bfda2fb9cdef8a4d4008a409bb02f35f1bd8
config: arm64-randconfig-r036-20210301 (attached as .config)
compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project 5de09ef02e24d234d9fc0cd1c6dfe18a1bb784b0)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # install arm64 cross compiling tool for clang build
        # apt-get install binutils-aarch64-linux-gnu
        # https://github.com/0day-ci/linux/commit/bf2779180284ed39480360900c07ce553f75e06a
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Uwe-Kleine-K-nig/clk-provide-new-devm-helpers-for-prepared-and-enabled-clocks/20210301-191522
        git checkout bf2779180284ed39480360900c07ce553f75e06a
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=arm64 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

>> drivers/clk/clk-devres.c:22:13: warning: no previous prototype for function '__devm_clk_get' [-Wmissing-prototypes]
   struct clk *__devm_clk_get(struct device *dev, const char *id,
               ^
   drivers/clk/clk-devres.c:22:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   struct clk *__devm_clk_get(struct device *dev, const char *id,
   ^
   static 
   1 warning generated.


vim +/__devm_clk_get +22 drivers/clk/clk-devres.c

    21	
  > 22	struct clk *__devm_clk_get(struct device *dev, const char *id,
    23				   struct clk *(*get)(struct device *dev, const char *id),
    24				   int (*init)(struct clk *clk),
    25				   void (*exit)(struct clk *clk))
    26	{
    27		struct devm_clk_state *state;
    28		struct clk *clk;
    29		int ret;
    30	
    31		state = devres_alloc(devm_clk_release, sizeof(*state), GFP_KERNEL);
    32		if (!state)
    33			return ERR_PTR(-ENOMEM);
    34	
    35		clk = get(dev, id);
    36		if (IS_ERR(clk)) {
    37			ret = PTR_ERR(clk);
    38			goto err_clk_get;
    39		}
    40	
    41		if (init) {
    42			ret = init(clk);
    43			if (ret)
    44				goto err_clk_init;
    45		}
    46	
    47		state->clk = clk;
    48		state->exit = exit;
    49	
    50		devres_add(dev, state);
    51	
    52		return clk;
    53	
    54	err_clk_init:
    55	
    56		clk_put(clk);
    57	err_clk_get:
    58	
    59		devres_free(state);
    60		return ERR_PTR(ret);
    61	}
    62	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
diff mbox series

Patch

diff --git a/drivers/clk/clk-devres.c b/drivers/clk/clk-devres.c
index be160764911b..7ba8670deeb6 100644
--- a/drivers/clk/clk-devres.c
+++ b/drivers/clk/clk-devres.c
@@ -4,39 +4,72 @@ 
 #include <linux/export.h>
 #include <linux/gfp.h>
 
+struct devm_clk_state {
+	struct clk *clk;
+	void (*exit)(struct clk *clk);
+};
+
 static void devm_clk_release(struct device *dev, void *res)
 {
-	clk_put(*(struct clk **)res);
+	struct devm_clk_state *state = *(struct devm_clk_state **)res;
+
+	if (state->exit)
+		state->exit(state->clk);
+
+	clk_put(state->clk);
+}
+
+struct clk *__devm_clk_get(struct device *dev, const char *id,
+			   struct clk *(*get)(struct device *dev, const char *id),
+			   int (*init)(struct clk *clk),
+			   void (*exit)(struct clk *clk))
+{
+	struct devm_clk_state *state;
+	struct clk *clk;
+	int ret;
+
+	state = devres_alloc(devm_clk_release, sizeof(*state), GFP_KERNEL);
+	if (!state)
+		return ERR_PTR(-ENOMEM);
+
+	clk = get(dev, id);
+	if (IS_ERR(clk)) {
+		ret = PTR_ERR(clk);
+		goto err_clk_get;
+	}
+
+	if (init) {
+		ret = init(clk);
+		if (ret)
+			goto err_clk_init;
+	}
+
+	state->clk = clk;
+	state->exit = exit;
+
+	devres_add(dev, state);
+
+	return clk;
+
+err_clk_init:
+
+	clk_put(clk);
+err_clk_get:
+
+	devres_free(state);
+	return ERR_PTR(ret);
 }
 
 struct clk *devm_clk_get(struct device *dev, const char *id)
 {
-	struct clk **ptr, *clk;
+	return __devm_clk_get(dev, id, clk_get, NULL, NULL);
 
-	ptr = devres_alloc(devm_clk_release, sizeof(*ptr), GFP_KERNEL);
-	if (!ptr)
-		return ERR_PTR(-ENOMEM);
-
-	clk = clk_get(dev, id);
-	if (!IS_ERR(clk)) {
-		*ptr = clk;
-		devres_add(dev, ptr);
-	} else {
-		devres_free(ptr);
-	}
-
-	return clk;
 }
 EXPORT_SYMBOL(devm_clk_get);
 
 struct clk *devm_clk_get_optional(struct device *dev, const char *id)
 {
-	struct clk *clk = devm_clk_get(dev, id);
-
-	if (clk == ERR_PTR(-ENOENT))
-		return NULL;
-
-	return clk;
+	return __devm_clk_get(dev, id, clk_get_optional, NULL, NULL);
 }
 EXPORT_SYMBOL(devm_clk_get_optional);