1
2
3
4
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
37struct clk_bulk_devres {
38 struct clk_bulk_data *clks;
39 int num_clks;
40};
41
42static void devm_clk_bulk_release(struct device *dev, void *res)
43{
44 struct clk_bulk_devres *devres = res;
45
46 clk_bulk_put(devres->num_clks, devres->clks);
47}
48
49int __must_check devm_clk_bulk_get(struct device *dev, int num_clks,
50 struct clk_bulk_data *clks)
51{
52 struct clk_bulk_devres *devres;
53 int ret;
54
55 devres = devres_alloc(devm_clk_bulk_release,
56 sizeof(*devres), GFP_KERNEL);
57 if (!devres)
58 return -ENOMEM;
59
60 ret = clk_bulk_get(dev, num_clks, clks);
61 if (!ret) {
62 devres->clks = clks;
63 devres->num_clks = num_clks;
64 devres_add(dev, devres);
65 } else {
66 devres_free(devres);
67 }
68
69 return ret;
70}
71EXPORT_SYMBOL_GPL(devm_clk_bulk_get);
72
73static int devm_clk_match(struct device *dev, void *res, void *data)
74{
75 struct clk **c = res;
76 if (!c || !*c) {
77 WARN_ON(!c || !*c);
78 return 0;
79 }
80 return *c == data;
81}
82
83void devm_clk_put(struct device *dev, struct clk *clk)
84{
85 int ret;
86
87 ret = devres_release(dev, devm_clk_release, devm_clk_match, clk);
88
89 WARN_ON(ret);
90}
91EXPORT_SYMBOL(devm_clk_put);
92
93struct clk *devm_get_clk_from_child(struct device *dev,
94 struct device_node *np, const char *con_id)
95{
96 struct clk **ptr, *clk;
97
98 ptr = devres_alloc(devm_clk_release, sizeof(*ptr), GFP_KERNEL);
99 if (!ptr)
100 return ERR_PTR(-ENOMEM);
101
102 clk = of_clk_get_by_name(np, con_id);
103 if (!IS_ERR(clk)) {
104 *ptr = clk;
105 devres_add(dev, ptr);
106 } else {
107 devres_free(ptr);
108 }
109
110 return clk;
111}
112EXPORT_SYMBOL(devm_get_clk_from_child);
113