xref: /linux/drivers/clk/clk-devres.c (revision 93d90ad708b8da6efc0e487b66111aa9db7f70c7)
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