blob: 3a218c3a06ae8d8169c384b722b5f08dd738ca5a [file] [log] [blame]
Lars-Peter Clausen8ef997b2012-09-11 19:56:23 +01001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation.
5 */
6
7#include <linux/clk.h>
8#include <linux/device.h>
9#include <linux/export.h>
10#include <linux/gfp.h>
11
12static void devm_clk_release(struct device *dev, void *res)
13{
14 clk_put(*(struct clk **)res);
15}
16
17struct clk *devm_clk_get(struct device *dev, const char *id)
18{
19 struct clk **ptr, *clk;
20
21 ptr = devres_alloc(devm_clk_release, sizeof(*ptr), GFP_KERNEL);
22 if (!ptr)
23 return ERR_PTR(-ENOMEM);
24
25 clk = clk_get(dev, id);
26 if (!IS_ERR(clk)) {
27 *ptr = clk;
28 devres_add(dev, ptr);
29 } else {
30 devres_free(ptr);
31 }
32
33 return clk;
34}
35EXPORT_SYMBOL(devm_clk_get);
36
37static int devm_clk_match(struct device *dev, void *res, void *data)
38{
39 struct clk **c = res;
40 if (!c || !*c) {
41 WARN_ON(!c || !*c);
42 return 0;
43 }
44 return *c == data;
45}
46
47void devm_clk_put(struct device *dev, struct clk *clk)
48{
49 int ret;
50
Mark Brown20332ff2012-09-19 12:43:21 +010051 ret = devres_release(dev, devm_clk_release, devm_clk_match, clk);
Lars-Peter Clausen8ef997b2012-09-11 19:56:23 +010052
53 WARN_ON(ret);
54}
55EXPORT_SYMBOL(devm_clk_put);
Kuninori Morimoto71a2f112016-12-05 05:23:20 +000056
57struct clk *devm_get_clk_from_child(struct device *dev,
58 struct device_node *np, const char *con_id)
59{
60 struct clk **ptr, *clk;
61
62 ptr = devres_alloc(devm_clk_release, sizeof(*ptr), GFP_KERNEL);
63 if (!ptr)
64 return ERR_PTR(-ENOMEM);
65
66 clk = of_clk_get_by_name(np, con_id);
67 if (!IS_ERR(clk)) {
68 *ptr = clk;
69 devres_add(dev, ptr);
70 } else {
71 devres_free(ptr);
72 }
73
74 return clk;
75}
76EXPORT_SYMBOL(devm_get_clk_from_child);