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);
30 #if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK)
32 static struct clk *__of_clk_get_by_clkspec(struct of_phandle_args *clkspec,
33 const char *dev_id, const char *con_id)
38 return ERR_PTR(-EINVAL);
41 clk = __of_clk_get_from_provider(clkspec, dev_id, con_id);
47 * of_clk_get_by_clkspec() - Lookup a clock form a clock provider
48 * @clkspec: pointer to a clock specifier data structure
50 * This function looks up a struct clk from the registered list of clock
51 * providers, an input is a clock specifier data structure as returned
52 * from the of_parse_phandle_with_args() function call.
54 struct clk *of_clk_get_by_clkspec(struct of_phandle_args *clkspec)
56 return __of_clk_get_by_clkspec(clkspec, NULL, __func__);
59 static struct clk *__of_clk_get(struct device_node *np, int index,
60 const char *dev_id, const char *con_id)
62 struct of_phandle_args clkspec;
67 return ERR_PTR(-EINVAL);
69 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
74 clk = __of_clk_get_by_clkspec(&clkspec, dev_id, con_id);
75 of_node_put(clkspec.np);
80 struct clk *of_clk_get(struct device_node *np, int index)
82 return __of_clk_get(np, index, np->full_name, NULL);
84 EXPORT_SYMBOL(of_clk_get);
86 static struct clk *__of_clk_get_by_name(struct device_node *np,
90 struct clk *clk = ERR_PTR(-ENOENT);
92 /* Walk up the tree of devices looking for a clock that matches */
97 * For named clocks, first look up the name in the
98 * "clock-names" property. If it cannot be found, then
99 * index will be an error code, and of_clk_get() will fail.
102 index = of_property_match_string(np, "clock-names", name);
103 clk = __of_clk_get(np, index, dev_id, name);
106 } else if (name && index >= 0) {
107 if (PTR_ERR(clk) != -EPROBE_DEFER)
108 pr_err("ERROR: could not get clock %s:%s(%i)\n",
109 np->full_name, name ? name : "", index);
114 * No matching clock found on this node. If the parent node
115 * has a "clock-ranges" property, then we can try one of its
119 if (np && !of_get_property(np, "clock-ranges", NULL))
127 * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
128 * @np: pointer to clock consumer node
129 * @name: name of consumer's clock input, or NULL for the first clock reference
131 * This function parses the clocks and clock-names properties,
132 * and uses them to look up the struct clk from the registered list of clock
135 struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
138 return ERR_PTR(-ENOENT);
140 return __of_clk_get_by_name(np, np->full_name, name);
142 EXPORT_SYMBOL(of_clk_get_by_name);
144 #else /* defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) */
146 static struct clk *__of_clk_get_by_name(struct device_node *np,
150 return ERR_PTR(-ENOENT);
155 * Find the correct struct clk for the device and connection ID.
156 * We do slightly fuzzy matching here:
157 * An entry with a NULL ID is assumed to be a wildcard.
158 * If an entry has a device ID, it must match
159 * If an entry has a connection ID, it must match
160 * Then we take the most specific entry - with the following
161 * order of precedence: dev+con > dev only > con only.
163 static struct clk_lookup *clk_find(const char *dev_id, const char *con_id)
165 struct clk_lookup *p, *cl = NULL;
166 int match, best_found = 0, best_possible = 0;
173 list_for_each_entry(p, &clocks, node) {
176 if (!dev_id || strcmp(p->dev_id, dev_id))
181 if (!con_id || strcmp(p->con_id, con_id))
186 if (match > best_found) {
188 if (match != best_possible)
197 struct clk *clk_get_sys(const char *dev_id, const char *con_id)
199 struct clk_lookup *cl;
200 struct clk *clk = NULL;
202 mutex_lock(&clocks_mutex);
204 cl = clk_find(dev_id, con_id);
208 clk = __clk_create_clk(__clk_get_hw(cl->clk), dev_id, con_id);
212 if (!__clk_get(clk)) {
219 mutex_unlock(&clocks_mutex);
221 return cl ? clk : ERR_PTR(-ENOENT);
223 EXPORT_SYMBOL(clk_get_sys);
225 struct clk *clk_get(struct device *dev, const char *con_id)
227 const char *dev_id = dev ? dev_name(dev) : NULL;
231 clk = __of_clk_get_by_name(dev->of_node, dev_id, con_id);
232 if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
236 return clk_get_sys(dev_id, con_id);
238 EXPORT_SYMBOL(clk_get);
240 void clk_put(struct clk *clk)
244 EXPORT_SYMBOL(clk_put);
246 void clkdev_add(struct clk_lookup *cl)
248 mutex_lock(&clocks_mutex);
249 list_add_tail(&cl->node, &clocks);
250 mutex_unlock(&clocks_mutex);
252 EXPORT_SYMBOL(clkdev_add);
254 void __init clkdev_add_table(struct clk_lookup *cl, size_t num)
256 mutex_lock(&clocks_mutex);
258 list_add_tail(&cl->node, &clocks);
261 mutex_unlock(&clocks_mutex);
264 #define MAX_DEV_ID 20
265 #define MAX_CON_ID 16
267 struct clk_lookup_alloc {
268 struct clk_lookup cl;
269 char dev_id[MAX_DEV_ID];
270 char con_id[MAX_CON_ID];
273 static struct clk_lookup * __init_refok
274 vclkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt,
277 struct clk_lookup_alloc *cla;
279 cla = __clkdev_alloc(sizeof(*cla));
285 strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
286 cla->cl.con_id = cla->con_id;
290 vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
291 cla->cl.dev_id = cla->dev_id;
297 struct clk_lookup * __init_refok
298 clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...)
300 struct clk_lookup *cl;
303 va_start(ap, dev_fmt);
304 cl = vclkdev_alloc(clk, con_id, dev_fmt, ap);
309 EXPORT_SYMBOL(clkdev_alloc);
311 int clk_add_alias(const char *alias, const char *alias_dev_name, char *id,
314 struct clk *r = clk_get(dev, id);
315 struct clk_lookup *l;
320 l = clkdev_alloc(r, alias, alias_dev_name);
327 EXPORT_SYMBOL(clk_add_alias);
330 * clkdev_drop - remove a clock dynamically allocated
332 void clkdev_drop(struct clk_lookup *cl)
334 mutex_lock(&clocks_mutex);
336 mutex_unlock(&clocks_mutex);
339 EXPORT_SYMBOL(clkdev_drop);
342 * clk_register_clkdev - register one clock lookup for a struct clk
343 * @clk: struct clk to associate with all clk_lookups
344 * @con_id: connection ID string on device
345 * @dev_id: format string describing device name
347 * con_id or dev_id may be NULL as a wildcard, just as in the rest of
350 * To make things easier for mass registration, we detect error clks
351 * from a previous clk_register() call, and return the error code for
352 * those. This is to permit this function to be called immediately
353 * after clk_register().
355 int clk_register_clkdev(struct clk *clk, const char *con_id,
356 const char *dev_fmt, ...)
358 struct clk_lookup *cl;
364 va_start(ap, dev_fmt);
365 cl = vclkdev_alloc(clk, con_id, dev_fmt, ap);
375 EXPORT_SYMBOL(clk_register_clkdev);
378 * clk_register_clkdevs - register a set of clk_lookup for a struct clk
379 * @clk: struct clk to associate with all clk_lookups
380 * @cl: array of clk_lookup structures with con_id and dev_id pre-initialized
381 * @num: number of clk_lookup structures to register
383 * To make things easier for mass registration, we detect error clks
384 * from a previous clk_register() call, and return the error code for
385 * those. This is to permit this function to be called immediately
386 * after clk_register().
388 int clk_register_clkdevs(struct clk *clk, struct clk_lookup *cl, size_t num)
395 for (i = 0; i < num; i++, cl++) {
402 EXPORT_SYMBOL(clk_register_clkdevs);