1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * drivers/clk/clkdev.c
4 *
5 * Copyright (C) 2008 Russell King.
6 *
7 * Helper for the clk API to assist looking up a struct clk.
8 */
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/device.h>
12 #include <linux/list.h>
13 #include <linux/errno.h>
14 #include <linux/err.h>
15 #include <linux/string.h>
16 #include <linux/mutex.h>
17 #include <linux/clk.h>
18 #include <linux/clkdev.h>
19 #include <linux/clk-provider.h>
20 #include <linux/of.h>
21
22 #include "clk.h"
23
24 static LIST_HEAD(clocks);
25 static DEFINE_MUTEX(clocks_mutex);
26
27 /*
28 * Find the correct struct clk for the device and connection ID.
29 * We do slightly fuzzy matching here:
30 * An entry with a NULL ID is assumed to be a wildcard.
31 * If an entry has a device ID, it must match
32 * If an entry has a connection ID, it must match
33 * Then we take the most specific entry - with the following
34 * order of precedence: dev+con > dev only > con only.
35 */
clk_find(const char * dev_id,const char * con_id)36 static struct clk_lookup *clk_find(const char *dev_id, const char *con_id)
37 {
38 struct clk_lookup *p, *cl = NULL;
39 int match, best_found = 0, best_possible = 0;
40
41 if (dev_id)
42 best_possible += 2;
43 if (con_id)
44 best_possible += 1;
45
46 lockdep_assert_held(&clocks_mutex);
47
48 list_for_each_entry(p, &clocks, node) {
49 match = 0;
50 if (p->dev_id) {
51 if (!dev_id || strcmp(p->dev_id, dev_id))
52 continue;
53 match += 2;
54 }
55 if (p->con_id) {
56 if (!con_id || strcmp(p->con_id, con_id))
57 continue;
58 match += 1;
59 }
60
61 if (match > best_found) {
62 cl = p;
63 if (match != best_possible)
64 best_found = match;
65 else
66 break;
67 }
68 }
69 return cl;
70 }
71
clk_find_hw(const char * dev_id,const char * con_id)72 struct clk_hw *clk_find_hw(const char *dev_id, const char *con_id)
73 {
74 struct clk_lookup *cl;
75 struct clk_hw *hw = ERR_PTR(-ENOENT);
76
77 mutex_lock(&clocks_mutex);
78 cl = clk_find(dev_id, con_id);
79 if (cl)
80 hw = cl->clk_hw;
81 mutex_unlock(&clocks_mutex);
82
83 return hw;
84 }
85
__clk_get_sys(struct device * dev,const char * dev_id,const char * con_id)86 static struct clk *__clk_get_sys(struct device *dev, const char *dev_id,
87 const char *con_id)
88 {
89 struct clk_hw *hw = clk_find_hw(dev_id, con_id);
90
91 return clk_hw_create_clk(dev, hw, dev_id, con_id);
92 }
93
clk_get_sys(const char * dev_id,const char * con_id)94 struct clk *clk_get_sys(const char *dev_id, const char *con_id)
95 {
96 return __clk_get_sys(NULL, dev_id, con_id);
97 }
98 EXPORT_SYMBOL(clk_get_sys);
99
clk_get(struct device * dev,const char * con_id)100 struct clk *clk_get(struct device *dev, const char *con_id)
101 {
102 const char *dev_id = dev ? dev_name(dev) : NULL;
103 struct clk_hw *hw;
104
105 if (dev && dev->of_node) {
106 hw = of_clk_get_hw(dev->of_node, 0, con_id);
107 if (!IS_ERR(hw) || PTR_ERR(hw) == -EPROBE_DEFER)
108 return clk_hw_create_clk(dev, hw, dev_id, con_id);
109 }
110
111 return __clk_get_sys(dev, dev_id, con_id);
112 }
113 EXPORT_SYMBOL(clk_get);
114
clk_put(struct clk * clk)115 void clk_put(struct clk *clk)
116 {
117 __clk_put(clk);
118 }
119 EXPORT_SYMBOL(clk_put);
120
__clkdev_add(struct clk_lookup * cl)121 static void __clkdev_add(struct clk_lookup *cl)
122 {
123 mutex_lock(&clocks_mutex);
124 list_add_tail(&cl->node, &clocks);
125 mutex_unlock(&clocks_mutex);
126 }
127
clkdev_add(struct clk_lookup * cl)128 void clkdev_add(struct clk_lookup *cl)
129 {
130 if (!cl->clk_hw)
131 cl->clk_hw = __clk_get_hw(cl->clk);
132 __clkdev_add(cl);
133 }
134 EXPORT_SYMBOL(clkdev_add);
135
clkdev_add_table(struct clk_lookup * cl,size_t num)136 void clkdev_add_table(struct clk_lookup *cl, size_t num)
137 {
138 mutex_lock(&clocks_mutex);
139 while (num--) {
140 cl->clk_hw = __clk_get_hw(cl->clk);
141 list_add_tail(&cl->node, &clocks);
142 cl++;
143 }
144 mutex_unlock(&clocks_mutex);
145 }
146
147 #define MAX_DEV_ID 24
148 #define MAX_CON_ID 16
149
150 struct clk_lookup_alloc {
151 struct clk_lookup cl;
152 char dev_id[MAX_DEV_ID];
153 char con_id[MAX_CON_ID];
154 };
155
156 static __printf(3, 0) struct clk_lookup * __ref
vclkdev_alloc(struct clk_hw * hw,const char * con_id,const char * dev_fmt,va_list ap)157 vclkdev_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt,
158 va_list ap)
159 {
160 struct clk_lookup_alloc *cla;
161 struct va_format vaf;
162 const char *failure;
163 va_list ap_copy;
164 size_t max_size;
165 ssize_t res;
166
167 cla = kzalloc(sizeof(*cla), GFP_KERNEL);
168 if (!cla)
169 return NULL;
170
171 va_copy(ap_copy, ap);
172
173 cla->cl.clk_hw = hw;
174 if (con_id) {
175 res = strscpy(cla->con_id, con_id, sizeof(cla->con_id));
176 if (res < 0) {
177 max_size = sizeof(cla->con_id);
178 failure = "connection";
179 goto fail;
180 }
181 cla->cl.con_id = cla->con_id;
182 }
183
184 if (dev_fmt) {
185 res = vsnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
186 if (res >= sizeof(cla->dev_id)) {
187 max_size = sizeof(cla->dev_id);
188 failure = "device";
189 goto fail;
190 }
191 cla->cl.dev_id = cla->dev_id;
192 }
193
194 va_end(ap_copy);
195
196 return &cla->cl;
197
198 fail:
199 if (dev_fmt)
200 vaf.fmt = dev_fmt;
201 else
202 vaf.fmt = "null-device";
203 vaf.va = &ap_copy;
204 pr_err("%pV:%s: %s ID is greater than %zu\n",
205 &vaf, con_id, failure, max_size);
206 va_end(ap_copy);
207
208 /*
209 * Don't fail in this case, but as the entry won't ever match just
210 * fill it with something that also won't match.
211 */
212 strscpy(cla->con_id, "bad", sizeof(cla->con_id));
213 strscpy(cla->dev_id, "bad", sizeof(cla->dev_id));
214
215 return &cla->cl;
216 }
217
218 static __printf(3, 0) struct clk_lookup *
vclkdev_create(struct clk_hw * hw,const char * con_id,const char * dev_fmt,va_list ap)219 vclkdev_create(struct clk_hw *hw, const char *con_id, const char *dev_fmt,
220 va_list ap)
221 {
222 struct clk_lookup *cl;
223
224 cl = vclkdev_alloc(hw, con_id, dev_fmt, ap);
225 if (cl)
226 __clkdev_add(cl);
227
228 return cl;
229 }
230
231 /**
232 * clkdev_create - allocate and add a clkdev lookup structure
233 * @clk: struct clk to associate with all clk_lookups
234 * @con_id: connection ID string on device
235 * @dev_fmt: format string describing device name
236 *
237 * Returns a clk_lookup structure, which can be later unregistered and
238 * freed.
239 */
clkdev_create(struct clk * clk,const char * con_id,const char * dev_fmt,...)240 struct clk_lookup *clkdev_create(struct clk *clk, const char *con_id,
241 const char *dev_fmt, ...)
242 {
243 struct clk_lookup *cl;
244 va_list ap;
245
246 va_start(ap, dev_fmt);
247 cl = vclkdev_create(__clk_get_hw(clk), con_id, dev_fmt, ap);
248 va_end(ap);
249
250 return cl;
251 }
252 EXPORT_SYMBOL_GPL(clkdev_create);
253
254 /**
255 * clkdev_hw_create - allocate and add a clkdev lookup structure
256 * @hw: struct clk_hw to associate with all clk_lookups
257 * @con_id: connection ID string on device
258 * @dev_fmt: format string describing device name
259 *
260 * Returns a clk_lookup structure, which can be later unregistered and
261 * freed.
262 */
clkdev_hw_create(struct clk_hw * hw,const char * con_id,const char * dev_fmt,...)263 struct clk_lookup *clkdev_hw_create(struct clk_hw *hw, const char *con_id,
264 const char *dev_fmt, ...)
265 {
266 struct clk_lookup *cl;
267 va_list ap;
268
269 va_start(ap, dev_fmt);
270 cl = vclkdev_create(hw, con_id, dev_fmt, ap);
271 va_end(ap);
272
273 return cl;
274 }
275 EXPORT_SYMBOL_GPL(clkdev_hw_create);
276
clk_add_alias(const char * alias,const char * alias_dev_name,const char * con_id,struct device * dev)277 int clk_add_alias(const char *alias, const char *alias_dev_name,
278 const char *con_id, struct device *dev)
279 {
280 struct clk *r = clk_get(dev, con_id);
281 struct clk_lookup *l;
282
283 if (IS_ERR(r))
284 return PTR_ERR(r);
285
286 l = clkdev_create(r, alias, alias_dev_name ? "%s" : NULL,
287 alias_dev_name);
288 clk_put(r);
289
290 return l ? 0 : -ENODEV;
291 }
292 EXPORT_SYMBOL(clk_add_alias);
293
294 /*
295 * clkdev_drop - remove a clock dynamically allocated
296 */
clkdev_drop(struct clk_lookup * cl)297 void clkdev_drop(struct clk_lookup *cl)
298 {
299 mutex_lock(&clocks_mutex);
300 list_del(&cl->node);
301 mutex_unlock(&clocks_mutex);
302 kfree(cl);
303 }
304 EXPORT_SYMBOL(clkdev_drop);
305
306 static __printf(3, 4) struct clk_lookup *
__clk_register_clkdev(struct clk_hw * hw,const char * con_id,const char * dev_id,...)307 __clk_register_clkdev(struct clk_hw *hw, const char *con_id, const char *dev_id, ...)
308 {
309 struct clk_lookup *cl;
310 va_list ap;
311
312 va_start(ap, dev_id);
313 cl = vclkdev_create(hw, con_id, dev_id, ap);
314 va_end(ap);
315
316 return cl;
317 }
318
do_clk_register_clkdev(struct clk_hw * hw,struct clk_lookup ** cl,const char * con_id,const char * dev_id)319 static int do_clk_register_clkdev(struct clk_hw *hw,
320 struct clk_lookup **cl, const char *con_id, const char *dev_id)
321 {
322 if (IS_ERR(hw))
323 return PTR_ERR(hw);
324 /*
325 * Since dev_id can be NULL, and NULL is handled specially, we must
326 * pass it as either a NULL format string, or with "%s".
327 */
328 if (dev_id)
329 *cl = __clk_register_clkdev(hw, con_id, "%s", dev_id);
330 else
331 *cl = __clk_register_clkdev(hw, con_id, NULL);
332
333 return *cl ? 0 : -ENOMEM;
334 }
335
336 /**
337 * clk_register_clkdev - register one clock lookup for a struct clk
338 * @clk: struct clk to associate with all clk_lookups
339 * @con_id: connection ID string on device
340 * @dev_id: string describing device name
341 *
342 * con_id or dev_id may be NULL as a wildcard, just as in the rest of
343 * clkdev.
344 *
345 * To make things easier for mass registration, we detect error clks
346 * from a previous clk_register() call, and return the error code for
347 * those. This is to permit this function to be called immediately
348 * after clk_register().
349 */
clk_register_clkdev(struct clk * clk,const char * con_id,const char * dev_id)350 int clk_register_clkdev(struct clk *clk, const char *con_id,
351 const char *dev_id)
352 {
353 struct clk_lookup *cl;
354
355 if (IS_ERR(clk))
356 return PTR_ERR(clk);
357
358 return do_clk_register_clkdev(__clk_get_hw(clk), &cl, con_id,
359 dev_id);
360 }
361 EXPORT_SYMBOL(clk_register_clkdev);
362
363 /**
364 * clk_hw_register_clkdev - register one clock lookup for a struct clk_hw
365 * @hw: struct clk_hw to associate with all clk_lookups
366 * @con_id: connection ID string on device
367 * @dev_id: format string describing device name
368 *
369 * con_id or dev_id may be NULL as a wildcard, just as in the rest of
370 * clkdev.
371 *
372 * To make things easier for mass registration, we detect error clk_hws
373 * from a previous clk_hw_register_*() call, and return the error code for
374 * those. This is to permit this function to be called immediately
375 * after clk_hw_register_*().
376 */
clk_hw_register_clkdev(struct clk_hw * hw,const char * con_id,const char * dev_id)377 int clk_hw_register_clkdev(struct clk_hw *hw, const char *con_id,
378 const char *dev_id)
379 {
380 struct clk_lookup *cl;
381
382 return do_clk_register_clkdev(hw, &cl, con_id, dev_id);
383 }
384 EXPORT_SYMBOL(clk_hw_register_clkdev);
385
devm_clkdev_release(void * res)386 static void devm_clkdev_release(void *res)
387 {
388 clkdev_drop(res);
389 }
390
391 /**
392 * devm_clk_hw_register_clkdev - managed clk lookup registration for clk_hw
393 * @dev: device this lookup is bound
394 * @hw: struct clk_hw to associate with all clk_lookups
395 * @con_id: connection ID string on device
396 * @dev_id: format string describing device name
397 *
398 * con_id or dev_id may be NULL as a wildcard, just as in the rest of
399 * clkdev.
400 *
401 * To make things easier for mass registration, we detect error clk_hws
402 * from a previous clk_hw_register_*() call, and return the error code for
403 * those. This is to permit this function to be called immediately
404 * after clk_hw_register_*().
405 */
devm_clk_hw_register_clkdev(struct device * dev,struct clk_hw * hw,const char * con_id,const char * dev_id)406 int devm_clk_hw_register_clkdev(struct device *dev, struct clk_hw *hw,
407 const char *con_id, const char *dev_id)
408 {
409 struct clk_lookup *cl;
410 int rval;
411
412 rval = do_clk_register_clkdev(hw, &cl, con_id, dev_id);
413 if (rval)
414 return rval;
415
416 return devm_add_action_or_reset(dev, devm_clkdev_release, cl);
417 }
418 EXPORT_SYMBOL(devm_clk_hw_register_clkdev);
419