xref: /linux/drivers/clk/clk-devres.c (revision e58e871becec2d3b04ed91c0c16fe8deac9c9dfa)
1 /*
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 
12 static void devm_clk_release(struct device *dev, void *res)
13 {
14 	clk_put(*(struct clk **)res);
15 }
16 
17 struct 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 }
35 EXPORT_SYMBOL(devm_clk_get);
36 
37 static 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 
47 void devm_clk_put(struct device *dev, struct clk *clk)
48 {
49 	int ret;
50 
51 	ret = devres_release(dev, devm_clk_release, devm_clk_match, clk);
52 
53 	WARN_ON(ret);
54 }
55 EXPORT_SYMBOL(devm_clk_put);
56 
57 struct 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 }
76 EXPORT_SYMBOL(devm_get_clk_from_child);
77