4 * Copyright (C) 2008 Russell King.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Helper for the clk API to assist looking up a struct clk.
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/device.h>
15 #include <linux/list.h>
16 #include <linux/errno.h>
17 #include <linux/err.h>
18 #include <linux/string.h>
19 #include <linux/mutex.h>
20 #include <linux/clk.h>
21 #include <linux/clkdev.h>
22 #include <linux/clk-provider.h>
27 static LIST_HEAD(clocks);
28 static DEFINE_MUTEX(clocks_mutex);
31 * Find the correct struct clk for the device and connection ID.
32 * We do slightly fuzzy matching here:
33 * An entry with a NULL ID is assumed to be a wildcard.
34 * If an entry has a device ID, it must match
35 * If an entry has a connection ID, it must match
36 * Then we take the most specific entry - with the following
37 * order of precedence: dev+con > dev only > con only.
39 static struct clk_lookup *clk_find(const char *dev_id, const char *con_id)
41 struct clk_lookup *p, *cl = NULL;
42 int match, best_found = 0, best_possible = 0;
49 lockdep_assert_held(&clocks_mutex);
51 list_for_each_entry(p, &clocks, node) {
54 if (!dev_id || strcmp(p->dev_id, dev_id))
59 if (!con_id || strcmp(p->con_id, con_id))
64 if (match > best_found) {
66 if (match != best_possible)
75 static struct clk *__clk_get_sys(struct device *dev, const char *dev_id,
78 struct clk_lookup *cl;
79 struct clk *clk = NULL;
81 mutex_lock(&clocks_mutex);
83 cl = clk_find(dev_id, con_id);
87 clk = clk_hw_create_clk(dev, cl->clk_hw, dev_id, con_id);
91 mutex_unlock(&clocks_mutex);
93 return cl ? clk : ERR_PTR(-ENOENT);
96 struct clk *clk_get_sys(const char *dev_id, const char *con_id)
98 return __clk_get_sys(NULL, dev_id, con_id);
100 EXPORT_SYMBOL(clk_get_sys);
102 struct clk *clk_get(struct device *dev, const char *con_id)
104 const char *dev_id = dev ? dev_name(dev) : NULL;
107 if (dev && dev->of_node) {
108 hw = of_clk_get_hw(dev->of_node, 0, con_id);
109 if (!IS_ERR(hw) || PTR_ERR(hw) == -EPROBE_DEFER)
110 return clk_hw_create_clk(dev, hw, dev_id, con_id);
113 return __clk_get_sys(dev, dev_id, con_id);
115 EXPORT_SYMBOL(clk_get);
117 void clk_put(struct clk *clk)
121 EXPORT_SYMBOL(clk_put);
123 static void __clkdev_add(struct clk_lookup *cl)
125 mutex_lock(&clocks_mutex);
126 list_add_tail(&cl->node, &clocks);
127 mutex_unlock(&clocks_mutex);
130 void clkdev_add(struct clk_lookup *cl)
133 cl->clk_hw = __clk_get_hw(cl->clk);
136 EXPORT_SYMBOL(clkdev_add);
138 void clkdev_add_table(struct clk_lookup *cl, size_t num)
140 mutex_lock(&clocks_mutex);
142 cl->clk_hw = __clk_get_hw(cl->clk);
143 list_add_tail(&cl->node, &clocks);
146 mutex_unlock(&clocks_mutex);
149 #define MAX_DEV_ID 20
150 #define MAX_CON_ID 16
152 struct clk_lookup_alloc {
153 struct clk_lookup cl;
154 char dev_id[MAX_DEV_ID];
155 char con_id[MAX_CON_ID];
158 static struct clk_lookup * __ref
159 vclkdev_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt,
162 struct clk_lookup_alloc *cla;
164 cla = kzalloc(sizeof(*cla), GFP_KERNEL);
170 strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
171 cla->cl.con_id = cla->con_id;
175 vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
176 cla->cl.dev_id = cla->dev_id;
182 static struct clk_lookup *
183 vclkdev_create(struct clk_hw *hw, const char *con_id, const char *dev_fmt,
186 struct clk_lookup *cl;
188 cl = vclkdev_alloc(hw, con_id, dev_fmt, ap);
195 struct clk_lookup * __ref
196 clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...)
198 struct clk_lookup *cl;
201 va_start(ap, dev_fmt);
202 cl = vclkdev_alloc(__clk_get_hw(clk), con_id, dev_fmt, ap);
207 EXPORT_SYMBOL(clkdev_alloc);
210 clkdev_hw_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt, ...)
212 struct clk_lookup *cl;
215 va_start(ap, dev_fmt);
216 cl = vclkdev_alloc(hw, con_id, dev_fmt, ap);
221 EXPORT_SYMBOL(clkdev_hw_alloc);
224 * clkdev_create - allocate and add a clkdev lookup structure
225 * @clk: struct clk to associate with all clk_lookups
226 * @con_id: connection ID string on device
227 * @dev_fmt: format string describing device name
229 * Returns a clk_lookup structure, which can be later unregistered and
232 struct clk_lookup *clkdev_create(struct clk *clk, const char *con_id,
233 const char *dev_fmt, ...)
235 struct clk_lookup *cl;
238 va_start(ap, dev_fmt);
239 cl = vclkdev_create(__clk_get_hw(clk), con_id, dev_fmt, ap);
244 EXPORT_SYMBOL_GPL(clkdev_create);
247 * clkdev_hw_create - allocate and add a clkdev lookup structure
248 * @hw: struct clk_hw to associate with all clk_lookups
249 * @con_id: connection ID string on device
250 * @dev_fmt: format string describing device name
252 * Returns a clk_lookup structure, which can be later unregistered and
255 struct clk_lookup *clkdev_hw_create(struct clk_hw *hw, const char *con_id,
256 const char *dev_fmt, ...)
258 struct clk_lookup *cl;
261 va_start(ap, dev_fmt);
262 cl = vclkdev_create(hw, con_id, dev_fmt, ap);
267 EXPORT_SYMBOL_GPL(clkdev_hw_create);
269 int clk_add_alias(const char *alias, const char *alias_dev_name,
270 const char *con_id, struct device *dev)
272 struct clk *r = clk_get(dev, con_id);
273 struct clk_lookup *l;
278 l = clkdev_create(r, alias, alias_dev_name ? "%s" : NULL,
282 return l ? 0 : -ENODEV;
284 EXPORT_SYMBOL(clk_add_alias);
287 * clkdev_drop - remove a clock dynamically allocated
289 void clkdev_drop(struct clk_lookup *cl)
291 mutex_lock(&clocks_mutex);
293 mutex_unlock(&clocks_mutex);
296 EXPORT_SYMBOL(clkdev_drop);
298 static struct clk_lookup *__clk_register_clkdev(struct clk_hw *hw,
300 const char *dev_id, ...)
302 struct clk_lookup *cl;
305 va_start(ap, dev_id);
306 cl = vclkdev_create(hw, con_id, dev_id, ap);
312 static int do_clk_register_clkdev(struct clk_hw *hw,
313 struct clk_lookup **cl, const char *con_id, const char *dev_id)
318 * Since dev_id can be NULL, and NULL is handled specially, we must
319 * pass it as either a NULL format string, or with "%s".
322 *cl = __clk_register_clkdev(hw, con_id, "%s", dev_id);
324 *cl = __clk_register_clkdev(hw, con_id, NULL);
326 return *cl ? 0 : -ENOMEM;
330 * clk_register_clkdev - register one clock lookup for a struct clk
331 * @clk: struct clk to associate with all clk_lookups
332 * @con_id: connection ID string on device
333 * @dev_id: string describing device name
335 * con_id or dev_id may be NULL as a wildcard, just as in the rest of
338 * To make things easier for mass registration, we detect error clks
339 * from a previous clk_register() call, and return the error code for
340 * those. This is to permit this function to be called immediately
341 * after clk_register().
343 int clk_register_clkdev(struct clk *clk, const char *con_id,
346 struct clk_lookup *cl;
351 return do_clk_register_clkdev(__clk_get_hw(clk), &cl, con_id,
354 EXPORT_SYMBOL(clk_register_clkdev);
357 * clk_hw_register_clkdev - register one clock lookup for a struct clk_hw
358 * @hw: struct clk_hw to associate with all clk_lookups
359 * @con_id: connection ID string on device
360 * @dev_id: format string describing device name
362 * con_id or dev_id may be NULL as a wildcard, just as in the rest of
365 * To make things easier for mass registration, we detect error clk_hws
366 * from a previous clk_hw_register_*() call, and return the error code for
367 * those. This is to permit this function to be called immediately
368 * after clk_hw_register_*().
370 int clk_hw_register_clkdev(struct clk_hw *hw, const char *con_id,
373 struct clk_lookup *cl;
375 return do_clk_register_clkdev(hw, &cl, con_id, dev_id);
377 EXPORT_SYMBOL(clk_hw_register_clkdev);
379 static void devm_clkdev_release(struct device *dev, void *res)
381 clkdev_drop(*(struct clk_lookup **)res);
384 static int devm_clk_match_clkdev(struct device *dev, void *res, void *data)
386 struct clk_lookup **l = res;
392 * devm_clk_release_clkdev - Resource managed clkdev lookup release
393 * @dev: device this lookup is bound
394 * @con_id: connection ID string on device
395 * @dev_id: format string describing device name
397 * Drop the clkdev lookup created with devm_clk_hw_register_clkdev.
398 * Normally this function will not need to be called and the resource
399 * management code will ensure that the resource is freed.
401 void devm_clk_release_clkdev(struct device *dev, const char *con_id,
404 struct clk_lookup *cl;
407 mutex_lock(&clocks_mutex);
408 cl = clk_find(dev_id, con_id);
409 mutex_unlock(&clocks_mutex);
412 rval = devres_release(dev, devm_clkdev_release,
413 devm_clk_match_clkdev, cl);
416 EXPORT_SYMBOL(devm_clk_release_clkdev);
419 * devm_clk_hw_register_clkdev - managed clk lookup registration for clk_hw
420 * @dev: device this lookup is bound
421 * @hw: struct clk_hw to associate with all clk_lookups
422 * @con_id: connection ID string on device
423 * @dev_id: format string describing device name
425 * con_id or dev_id may be NULL as a wildcard, just as in the rest of
428 * To make things easier for mass registration, we detect error clk_hws
429 * from a previous clk_hw_register_*() call, and return the error code for
430 * those. This is to permit this function to be called immediately
431 * after clk_hw_register_*().
433 int devm_clk_hw_register_clkdev(struct device *dev, struct clk_hw *hw,
434 const char *con_id, const char *dev_id)
437 struct clk_lookup **cl;
439 cl = devres_alloc(devm_clkdev_release, sizeof(*cl), GFP_KERNEL);
441 rval = do_clk_register_clkdev(hw, cl, con_id, dev_id);
449 EXPORT_SYMBOL(devm_clk_hw_register_clkdev);