1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/device.h>
4 #include <linux/export.h>
7 struct devm_clk_state {
9 void (*exit)(struct clk *clk);
12 static void devm_clk_release(struct device *dev, void *res)
14 struct devm_clk_state *state = res;
17 state->exit(state->clk);
22 static struct clk *__devm_clk_get(struct device *dev, const char *id,
23 struct clk *(*get)(struct device *dev, const char *id),
24 int (*init)(struct clk *clk),
25 void (*exit)(struct clk *clk))
27 struct devm_clk_state *state;
31 state = devres_alloc(devm_clk_release, sizeof(*state), GFP_KERNEL);
33 return ERR_PTR(-ENOMEM);
50 devres_add(dev, state);
63 struct clk *devm_clk_get(struct device *dev, const char *id)
65 return __devm_clk_get(dev, id, clk_get, NULL, NULL);
67 EXPORT_SYMBOL(devm_clk_get);
69 struct clk *devm_clk_get_prepared(struct device *dev, const char *id)
71 return __devm_clk_get(dev, id, clk_get, clk_prepare, clk_unprepare);
73 EXPORT_SYMBOL_GPL(devm_clk_get_prepared);
75 struct clk *devm_clk_get_enabled(struct device *dev, const char *id)
77 return __devm_clk_get(dev, id, clk_get,
78 clk_prepare_enable, clk_disable_unprepare);
80 EXPORT_SYMBOL_GPL(devm_clk_get_enabled);
82 struct clk *devm_clk_get_optional(struct device *dev, const char *id)
84 return __devm_clk_get(dev, id, clk_get_optional, NULL, NULL);
86 EXPORT_SYMBOL(devm_clk_get_optional);
88 struct clk *devm_clk_get_optional_prepared(struct device *dev, const char *id)
90 return __devm_clk_get(dev, id, clk_get_optional,
91 clk_prepare, clk_unprepare);
93 EXPORT_SYMBOL_GPL(devm_clk_get_optional_prepared);
95 struct clk *devm_clk_get_optional_enabled(struct device *dev, const char *id)
97 return __devm_clk_get(dev, id, clk_get_optional,
98 clk_prepare_enable, clk_disable_unprepare);
100 EXPORT_SYMBOL_GPL(devm_clk_get_optional_enabled);
102 struct clk_bulk_devres {
103 struct clk_bulk_data *clks;
107 static void devm_clk_bulk_release(struct device *dev, void *res)
109 struct clk_bulk_devres *devres = res;
111 clk_bulk_put(devres->num_clks, devres->clks);
114 static int __devm_clk_bulk_get(struct device *dev, int num_clks,
115 struct clk_bulk_data *clks, bool optional)
117 struct clk_bulk_devres *devres;
120 devres = devres_alloc(devm_clk_bulk_release,
121 sizeof(*devres), GFP_KERNEL);
126 ret = clk_bulk_get_optional(dev, num_clks, clks);
128 ret = clk_bulk_get(dev, num_clks, clks);
131 devres->num_clks = num_clks;
132 devres_add(dev, devres);
140 int __must_check devm_clk_bulk_get(struct device *dev, int num_clks,
141 struct clk_bulk_data *clks)
143 return __devm_clk_bulk_get(dev, num_clks, clks, false);
145 EXPORT_SYMBOL_GPL(devm_clk_bulk_get);
147 int __must_check devm_clk_bulk_get_optional(struct device *dev, int num_clks,
148 struct clk_bulk_data *clks)
150 return __devm_clk_bulk_get(dev, num_clks, clks, true);
152 EXPORT_SYMBOL_GPL(devm_clk_bulk_get_optional);
154 static void devm_clk_bulk_release_all(struct device *dev, void *res)
156 struct clk_bulk_devres *devres = res;
158 clk_bulk_put_all(devres->num_clks, devres->clks);
161 int __must_check devm_clk_bulk_get_all(struct device *dev,
162 struct clk_bulk_data **clks)
164 struct clk_bulk_devres *devres;
167 devres = devres_alloc(devm_clk_bulk_release_all,
168 sizeof(*devres), GFP_KERNEL);
172 ret = clk_bulk_get_all(dev, &devres->clks);
174 *clks = devres->clks;
175 devres->num_clks = ret;
176 devres_add(dev, devres);
183 EXPORT_SYMBOL_GPL(devm_clk_bulk_get_all);
185 static int devm_clk_match(struct device *dev, void *res, void *data)
187 struct clk **c = res;
195 void devm_clk_put(struct device *dev, struct clk *clk)
199 ret = devres_release(dev, devm_clk_release, devm_clk_match, clk);
203 EXPORT_SYMBOL(devm_clk_put);
205 struct clk *devm_get_clk_from_child(struct device *dev,
206 struct device_node *np, const char *con_id)
208 struct clk **ptr, *clk;
210 ptr = devres_alloc(devm_clk_release, sizeof(*ptr), GFP_KERNEL);
212 return ERR_PTR(-ENOMEM);
214 clk = of_clk_get_by_name(np, con_id);
217 devres_add(dev, ptr);
224 EXPORT_SYMBOL(devm_get_clk_from_child);