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)
33 * of_clk_get_by_clkspec() - Lookup a clock form a clock provider
34 * @clkspec: pointer to a clock specifier data structure
36 * This function looks up a struct clk from the registered list of clock
37 * providers, an input is a clock specifier data structure as returned
38 * from the of_parse_phandle_with_args() function call.
40 struct clk *of_clk_get_by_clkspec(struct of_phandle_args *clkspec)
45 return ERR_PTR(-EINVAL);
48 clk = __of_clk_get_from_provider(clkspec);
50 if (!IS_ERR(clk) && !__clk_get(clk))
51 clk = ERR_PTR(-ENOENT);
57 static struct clk *__of_clk_get(struct device_node *np, int index)
59 struct of_phandle_args clkspec;
64 return ERR_PTR(-EINVAL);
66 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
71 clk = of_clk_get_by_clkspec(&clkspec);
72 of_node_put(clkspec.np);
77 struct clk *of_clk_get(struct device_node *np, int index)
79 struct clk *clk = __of_clk_get(np, index);
82 clk = __clk_create_clk(__clk_get_hw(clk), np->full_name, NULL);
86 EXPORT_SYMBOL(of_clk_get);
88 static struct clk *__of_clk_get_by_name(struct device_node *np,
92 struct clk *clk = ERR_PTR(-ENOENT);
94 /* Walk up the tree of devices looking for a clock that matches */
99 * For named clocks, first look up the name in the
100 * "clock-names" property. If it cannot be found, then
101 * index will be an error code, and of_clk_get() will fail.
104 index = of_property_match_string(np, "clock-names", name);
105 clk = __of_clk_get(np, index);
107 clk = __clk_create_clk(__clk_get_hw(clk), dev_id, name);
110 else if (name && index >= 0) {
111 if (PTR_ERR(clk) != -EPROBE_DEFER)
112 pr_err("ERROR: could not get clock %s:%s(%i)\n",
113 np->full_name, name ? name : "", index);
118 * No matching clock found on this node. If the parent node
119 * has a "clock-ranges" property, then we can try one of its
123 if (np && !of_get_property(np, "clock-ranges", NULL))
131 * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
132 * @np: pointer to clock consumer node
133 * @name: name of consumer's clock input, or NULL for the first clock reference
135 * This function parses the clocks and clock-names properties,
136 * and uses them to look up the struct clk from the registered list of clock
139 struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
142 return ERR_PTR(-ENOENT);
144 return __of_clk_get_by_name(np, np->full_name, name);
146 EXPORT_SYMBOL(of_clk_get_by_name);
148 #else /* defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) */
150 static struct clk *__of_clk_get_by_name(struct device_node *np,
154 return ERR_PTR(-ENOENT);
159 * Find the correct struct clk for the device and connection ID.
160 * We do slightly fuzzy matching here:
161 * An entry with a NULL ID is assumed to be a wildcard.
162 * If an entry has a device ID, it must match
163 * If an entry has a connection ID, it must match
164 * Then we take the most specific entry - with the following
165 * order of precedence: dev+con > dev only > con only.
167 static struct clk_lookup *clk_find(const char *dev_id, const char *con_id)
169 struct clk_lookup *p, *cl = NULL;
170 int match, best_found = 0, best_possible = 0;
177 list_for_each_entry(p, &clocks, node) {
180 if (!dev_id || strcmp(p->dev_id, dev_id))
185 if (!con_id || strcmp(p->con_id, con_id))
190 if (match > best_found) {
192 if (match != best_possible)
201 struct clk *clk_get_sys(const char *dev_id, const char *con_id)
203 struct clk_lookup *cl;
204 struct clk *clk = NULL;
206 mutex_lock(&clocks_mutex);
208 cl = clk_find(dev_id, con_id);
212 if (!__clk_get(cl->clk)) {
217 #if defined(CONFIG_COMMON_CLK)
218 clk = __clk_create_clk(__clk_get_hw(cl->clk), dev_id, con_id);
224 mutex_unlock(&clocks_mutex);
226 return cl ? clk : ERR_PTR(-ENOENT);
228 EXPORT_SYMBOL(clk_get_sys);
230 struct clk *clk_get(struct device *dev, const char *con_id)
232 const char *dev_id = dev ? dev_name(dev) : NULL;
236 clk = __of_clk_get_by_name(dev->of_node, dev_id, con_id);
237 if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
241 return clk_get_sys(dev_id, con_id);
243 EXPORT_SYMBOL(clk_get);
245 void clk_put(struct clk *clk)
249 EXPORT_SYMBOL(clk_put);
251 void clkdev_add(struct clk_lookup *cl)
253 mutex_lock(&clocks_mutex);
254 list_add_tail(&cl->node, &clocks);
255 mutex_unlock(&clocks_mutex);
257 EXPORT_SYMBOL(clkdev_add);
259 void __init clkdev_add_table(struct clk_lookup *cl, size_t num)
261 mutex_lock(&clocks_mutex);
263 list_add_tail(&cl->node, &clocks);
266 mutex_unlock(&clocks_mutex);
269 #define MAX_DEV_ID 20
270 #define MAX_CON_ID 16
272 struct clk_lookup_alloc {
273 struct clk_lookup cl;
274 char dev_id[MAX_DEV_ID];
275 char con_id[MAX_CON_ID];
278 static struct clk_lookup * __init_refok
279 vclkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt,
282 struct clk_lookup_alloc *cla;
284 cla = __clkdev_alloc(sizeof(*cla));
290 strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
291 cla->cl.con_id = cla->con_id;
295 vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
296 cla->cl.dev_id = cla->dev_id;
302 struct clk_lookup * __init_refok
303 clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...)
305 struct clk_lookup *cl;
308 va_start(ap, dev_fmt);
309 cl = vclkdev_alloc(clk, con_id, dev_fmt, ap);
314 EXPORT_SYMBOL(clkdev_alloc);
316 int clk_add_alias(const char *alias, const char *alias_dev_name, char *id,
319 struct clk *r = clk_get(dev, id);
320 struct clk_lookup *l;
325 l = clkdev_alloc(r, alias, alias_dev_name);
332 EXPORT_SYMBOL(clk_add_alias);
335 * clkdev_drop - remove a clock dynamically allocated
337 void clkdev_drop(struct clk_lookup *cl)
339 mutex_lock(&clocks_mutex);
341 mutex_unlock(&clocks_mutex);
344 EXPORT_SYMBOL(clkdev_drop);
347 * clk_register_clkdev - register one clock lookup for a struct clk
348 * @clk: struct clk to associate with all clk_lookups
349 * @con_id: connection ID string on device
350 * @dev_id: format string describing device name
352 * con_id or dev_id may be NULL as a wildcard, just as in the rest of
355 * To make things easier for mass registration, we detect error clks
356 * from a previous clk_register() call, and return the error code for
357 * those. This is to permit this function to be called immediately
358 * after clk_register().
360 int clk_register_clkdev(struct clk *clk, const char *con_id,
361 const char *dev_fmt, ...)
363 struct clk_lookup *cl;
369 va_start(ap, dev_fmt);
370 cl = vclkdev_alloc(clk, con_id, dev_fmt, ap);
382 * clk_register_clkdevs - register a set of clk_lookup for a struct clk
383 * @clk: struct clk to associate with all clk_lookups
384 * @cl: array of clk_lookup structures with con_id and dev_id pre-initialized
385 * @num: number of clk_lookup structures to register
387 * To make things easier for mass registration, we detect error clks
388 * from a previous clk_register() call, and return the error code for
389 * those. This is to permit this function to be called immediately
390 * after clk_register().
392 int clk_register_clkdevs(struct clk *clk, struct clk_lookup *cl, size_t num)
399 for (i = 0; i < num; i++, cl++) {
406 EXPORT_SYMBOL(clk_register_clkdevs);