Re: [PATCH v1] clk: Add devm_clk_{prepare,enable,prepare_enable}

Bjorn Andersson
2019-07-15T21:45:33Z

On Mon 15 Jul 08:34 PDT 2019, Marc Gonzalez wrote:

[..]
> diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
> index c0990703ce54..5e85548357c0 100644
> --- a/drivers/clk/clk.c
> +++ b/drivers/clk/clk.c
> @@ -914,6 +914,18 @@ int clk_prepare(struct clk *clk)
> }
> EXPORT_SYMBOL_GPL(clk_prepare);
>
> +static void unprepare(void *clk)

This deserves a less generic name.

> +{
> + clk_unprepare(clk);
> +}
> +
> +int devm_clk_prepare(struct device *dev, struct clk *clk)
> +{
> + int rc = clk_prepare(clk);
> + return rc ? : devm_add_action_or_reset(dev, unprepare, clk);
> +}
> +EXPORT_SYMBOL_GPL(devm_clk_prepare);
> +
> static void clk_core_disable(struct clk_core *core)
> {
> lockdep_assert_held(&enable_lock);
> @@ -1136,6 +1148,18 @@ int clk_enable(struct clk *clk)
> }
> EXPORT_SYMBOL_GPL(clk_enable);
>
> +static void disable(void *clk)
> +{
> + clk_disable(clk);
> +}
> +
> +int devm_clk_enable(struct device *dev, struct clk *clk)

clk_enable() is used in code that can't sleep, in what scenario do you
envision it being useful to enable a clock from such region until devres
cleans up the associated device?

> +{
> + int rc = clk_enable(clk);
> + return rc ? : devm_add_action_or_reset(dev, disable, clk);

devm_add_action_or_reset() allocates the devres object with GFP_KERNEL,
so this won't work.

> +}
> +EXPORT_SYMBOL_GPL(devm_clk_enable);
> +
> static int clk_core_prepare_enable(struct clk_core *core)
> {
> int ret;
> diff --git a/include/linux/clk.h b/include/linux/clk.h
> index 3c096c7a51dc..d09b5207e3f1 100644
> --- a/include/linux/clk.h
> +++ b/include/linux/clk.h
> @@ -895,6 +895,14 @@ static inline void clk_restore_context(void) {}
>
> #endif
>
> +int devm_clk_prepare(struct device *dev, struct clk *clk);
> +int devm_clk_enable(struct device *dev, struct clk *clk);
> +static inline int devm_clk_prepare_enable(struct device *dev, struct clk *clk)

devm_clk_prepare_enable() sounds very useful, devm_clk_prepare() might
be useful, so keep those and drop devm_clk_enable().

Regards,
Bjorn

> +{
> + int rc = devm_clk_prepare(dev, clk);
> + return rc ? : devm_clk_enable(dev, clk);
> +}
> +
> /* clk_prepare_enable helps cases using clk_enable in non-atomic context. */
> static inline int clk_prepare_enable(struct clk *clk)
> {
> --
> 2.17.1