1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/device.h>
4 #include <linux/export.h>
7 static void devm_clk_release(struct device
*dev
, void *res
)
9 clk_put(*(struct clk
**)res
);
12 struct clk
*devm_clk_get(struct device
*dev
, const char *id
)
14 struct clk
**ptr
, *clk
;
16 ptr
= devres_alloc(devm_clk_release
, sizeof(*ptr
), GFP_KERNEL
);
18 return ERR_PTR(-ENOMEM
);
20 clk
= clk_get(dev
, id
);
30 EXPORT_SYMBOL(devm_clk_get
);
32 struct clk
*devm_clk_get_optional(struct device
*dev
, const char *id
)
34 struct clk
*clk
= devm_clk_get(dev
, id
);
36 if (clk
== ERR_PTR(-ENOENT
))
41 EXPORT_SYMBOL(devm_clk_get_optional
);
43 struct clk_bulk_devres
{
44 struct clk_bulk_data
*clks
;
48 static void devm_clk_bulk_release(struct device
*dev
, void *res
)
50 struct clk_bulk_devres
*devres
= res
;
52 clk_bulk_put(devres
->num_clks
, devres
->clks
);
55 static int __devm_clk_bulk_get(struct device
*dev
, int num_clks
,
56 struct clk_bulk_data
*clks
, bool optional
)
58 struct clk_bulk_devres
*devres
;
61 devres
= devres_alloc(devm_clk_bulk_release
,
62 sizeof(*devres
), GFP_KERNEL
);
67 ret
= clk_bulk_get_optional(dev
, num_clks
, clks
);
69 ret
= clk_bulk_get(dev
, num_clks
, clks
);
72 devres
->num_clks
= num_clks
;
73 devres_add(dev
, devres
);
81 int __must_check
devm_clk_bulk_get(struct device
*dev
, int num_clks
,
82 struct clk_bulk_data
*clks
)
84 return __devm_clk_bulk_get(dev
, num_clks
, clks
, false);
86 EXPORT_SYMBOL_GPL(devm_clk_bulk_get
);
88 int __must_check
devm_clk_bulk_get_optional(struct device
*dev
, int num_clks
,
89 struct clk_bulk_data
*clks
)
91 return __devm_clk_bulk_get(dev
, num_clks
, clks
, true);
93 EXPORT_SYMBOL_GPL(devm_clk_bulk_get_optional
);
95 int __must_check
devm_clk_bulk_get_all(struct device
*dev
,
96 struct clk_bulk_data
**clks
)
98 struct clk_bulk_devres
*devres
;
101 devres
= devres_alloc(devm_clk_bulk_release
,
102 sizeof(*devres
), GFP_KERNEL
);
106 ret
= clk_bulk_get_all(dev
, &devres
->clks
);
108 *clks
= devres
->clks
;
109 devres
->num_clks
= ret
;
110 devres_add(dev
, devres
);
117 EXPORT_SYMBOL_GPL(devm_clk_bulk_get_all
);
119 static int devm_clk_match(struct device
*dev
, void *res
, void *data
)
121 struct clk
**c
= res
;
129 void devm_clk_put(struct device
*dev
, struct clk
*clk
)
133 ret
= devres_release(dev
, devm_clk_release
, devm_clk_match
, clk
);
137 EXPORT_SYMBOL(devm_clk_put
);
139 struct clk
*devm_get_clk_from_child(struct device
*dev
,
140 struct device_node
*np
, const char *con_id
)
142 struct clk
**ptr
, *clk
;
144 ptr
= devres_alloc(devm_clk_release
, sizeof(*ptr
), GFP_KERNEL
);
146 return ERR_PTR(-ENOMEM
);
148 clk
= of_clk_get_by_name(np
, con_id
);
151 devres_add(dev
, ptr
);
158 EXPORT_SYMBOL(devm_get_clk_from_child
);