diff options
author | Jean-Jacques Hiblot | 2019-10-22 14:00:04 +0200 |
---|---|---|
committer | Lukasz Majewski | 2019-10-22 16:14:05 +0200 |
commit | 52720c536ffdbe0e6aece79840e2791d87204cf7 (patch) | |
tree | 190272b13beda559e06c6ad8c274014b1a267854 /drivers | |
parent | 8a1661f20e6cdda682820eba9ccc79bff5a3d7f8 (diff) |
drivers: clk: Add a managed API to get clocks from the device-tree
Add devm_clk_get(), devm_clk_get_optional() to get clocks from the
device-tree. The clocks is automatically released and the data structure
freed when the device is unbound.
Also add devm_clk_put() to release the clock and free the data structure
manually.
Signed-off-by: Jean-Jacques Hiblot <jjhiblot@ti.com>
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/clk/clk-uclass.c | 48 | ||||
-rw-r--r-- | drivers/mtd/nand/raw/brcmnand/brcmnand_compat.c | 30 | ||||
-rw-r--r-- | drivers/mtd/nand/raw/brcmnand/brcmnand_compat.h | 4 |
3 files changed, 48 insertions, 34 deletions
diff --git a/drivers/clk/clk-uclass.c b/drivers/clk/clk-uclass.c index 82a62c02d99..80346987b9d 100644 --- a/drivers/clk/clk-uclass.c +++ b/drivers/clk/clk-uclass.c @@ -625,6 +625,54 @@ bool clk_is_match(const struct clk *p, const struct clk *q) return false; } +static void devm_clk_release(struct udevice *dev, void *res) +{ + clk_free(res); +} + +static int devm_clk_match(struct udevice *dev, void *res, void *data) +{ + return res == data; +} + +struct clk *devm_clk_get(struct udevice *dev, const char *id) +{ + int rc; + struct clk *clk; + + clk = devres_alloc(devm_clk_release, sizeof(struct clk), __GFP_ZERO); + if (unlikely(!clk)) + return ERR_PTR(-ENOMEM); + + rc = clk_get_by_name(dev, id, clk); + if (rc) + return ERR_PTR(rc); + + devres_add(dev, clk); + return clk; +} + +struct clk *devm_clk_get_optional(struct udevice *dev, const char *id) +{ + struct clk *clk = devm_clk_get(dev, id); + + if (IS_ERR(clk)) + return NULL; + + return clk; +} + +void devm_clk_put(struct udevice *dev, struct clk *clk) +{ + int rc; + + if (!clk) + return; + + rc = devres_release(dev, devm_clk_release, devm_clk_match, clk); + WARN_ON(rc); +} + UCLASS_DRIVER(clk) = { .id = UCLASS_CLK, .name = "clk", diff --git a/drivers/mtd/nand/raw/brcmnand/brcmnand_compat.c b/drivers/mtd/nand/raw/brcmnand/brcmnand_compat.c index 96b27e6e5ad..883948355ca 100644 --- a/drivers/mtd/nand/raw/brcmnand/brcmnand_compat.c +++ b/drivers/mtd/nand/raw/brcmnand/brcmnand_compat.c @@ -3,36 +3,6 @@ #include <common.h> #include "brcmnand_compat.h" -struct clk *devm_clk_get(struct udevice *dev, const char *id) -{ - struct clk *clk; - int ret; - - clk = devm_kzalloc(dev, sizeof(*clk), GFP_KERNEL); - if (!clk) { - debug("%s: can't allocate clock\n", __func__); - return ERR_PTR(-ENOMEM); - } - - ret = clk_get_by_name(dev, id, clk); - if (ret < 0) { - debug("%s: can't get clock (ret = %d)!\n", __func__, ret); - return ERR_PTR(ret); - } - - return clk; -} - -int clk_prepare_enable(struct clk *clk) -{ - return clk_enable(clk); -} - -void clk_disable_unprepare(struct clk *clk) -{ - clk_disable(clk); -} - static char *devm_kvasprintf(struct udevice *dev, gfp_t gfp, const char *fmt, va_list ap) { diff --git a/drivers/mtd/nand/raw/brcmnand/brcmnand_compat.h b/drivers/mtd/nand/raw/brcmnand/brcmnand_compat.h index 02cab0f828c..6f9bec7085c 100644 --- a/drivers/mtd/nand/raw/brcmnand/brcmnand_compat.h +++ b/drivers/mtd/nand/raw/brcmnand/brcmnand_compat.h @@ -6,10 +6,6 @@ #include <clk.h> #include <dm.h> -struct clk *devm_clk_get(struct udevice *dev, const char *id); -int clk_prepare_enable(struct clk *clk); -void clk_disable_unprepare(struct clk *clk); - char *devm_kasprintf(struct udevice *dev, gfp_t gfp, const char *fmt, ...); #endif /* __BRCMNAND_COMPAT_H */ |