Bluetooth: Remove unneeded initializer for management command table
[linux/fpc-iii.git] / drivers / clk / clkdev.c
blob043fd3633373982f0408de1618370c6d63d5887b
1 /*
2 * drivers/clk/clkdev.c
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>
23 #include <linux/of.h>
25 #include "clk.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)
35 struct clk *clk;
37 if (!clkspec)
38 return ERR_PTR(-EINVAL);
40 of_clk_lock();
41 clk = __of_clk_get_from_provider(clkspec, dev_id, con_id);
42 of_clk_unlock();
43 return clk;
46 /**
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;
63 struct clk *clk;
64 int rc;
66 if (index < 0)
67 return ERR_PTR(-EINVAL);
69 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
70 &clkspec);
71 if (rc)
72 return ERR_PTR(rc);
74 clk = __of_clk_get_by_clkspec(&clkspec, dev_id, con_id);
75 of_node_put(clkspec.np);
77 return clk;
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,
87 const char *dev_id,
88 const char *name)
90 struct clk *clk = ERR_PTR(-ENOENT);
92 /* Walk up the tree of devices looking for a clock that matches */
93 while (np) {
94 int index = 0;
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.
101 if (name)
102 index = of_property_match_string(np, "clock-names", name);
103 clk = __of_clk_get(np, index, dev_id, name);
104 if (!IS_ERR(clk)) {
105 break;
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);
110 return clk;
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
116 * clocks.
118 np = np->parent;
119 if (np && !of_get_property(np, "clock-ranges", NULL))
120 break;
123 return clk;
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
133 * providers.
135 struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
137 if (!np)
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,
147 const char *dev_id,
148 const char *name)
150 return ERR_PTR(-ENOENT);
152 #endif
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;
168 if (dev_id)
169 best_possible += 2;
170 if (con_id)
171 best_possible += 1;
173 list_for_each_entry(p, &clocks, node) {
174 match = 0;
175 if (p->dev_id) {
176 if (!dev_id || strcmp(p->dev_id, dev_id))
177 continue;
178 match += 2;
180 if (p->con_id) {
181 if (!con_id || strcmp(p->con_id, con_id))
182 continue;
183 match += 1;
186 if (match > best_found) {
187 cl = p;
188 if (match != best_possible)
189 best_found = match;
190 else
191 break;
194 return cl;
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);
205 if (!cl)
206 goto out;
208 clk = __clk_create_clk(__clk_get_hw(cl->clk), dev_id, con_id);
209 if (IS_ERR(clk))
210 goto out;
212 if (!__clk_get(clk)) {
213 __clk_free_clk(clk);
214 cl = NULL;
215 goto out;
218 out:
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;
228 struct clk *clk;
230 if (dev) {
231 clk = __of_clk_get_by_name(dev->of_node, dev_id, con_id);
232 if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
233 return clk;
236 return clk_get_sys(dev_id, con_id);
238 EXPORT_SYMBOL(clk_get);
240 void clk_put(struct clk *clk)
242 __clk_put(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);
257 while (num--) {
258 list_add_tail(&cl->node, &clocks);
259 cl++;
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,
275 va_list ap)
277 struct clk_lookup_alloc *cla;
279 cla = __clkdev_alloc(sizeof(*cla));
280 if (!cla)
281 return NULL;
283 cla->cl.clk = clk;
284 if (con_id) {
285 strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
286 cla->cl.con_id = cla->con_id;
289 if (dev_fmt) {
290 vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
291 cla->cl.dev_id = cla->dev_id;
294 return &cla->cl;
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;
301 va_list ap;
303 va_start(ap, dev_fmt);
304 cl = vclkdev_alloc(clk, con_id, dev_fmt, ap);
305 va_end(ap);
307 return cl;
309 EXPORT_SYMBOL(clkdev_alloc);
311 int clk_add_alias(const char *alias, const char *alias_dev_name, char *id,
312 struct device *dev)
314 struct clk *r = clk_get(dev, id);
315 struct clk_lookup *l;
317 if (IS_ERR(r))
318 return PTR_ERR(r);
320 l = clkdev_alloc(r, alias, alias_dev_name);
321 clk_put(r);
322 if (!l)
323 return -ENODEV;
324 clkdev_add(l);
325 return 0;
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);
335 list_del(&cl->node);
336 mutex_unlock(&clocks_mutex);
337 kfree(cl);
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
348 * clkdev.
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;
359 va_list ap;
361 if (IS_ERR(clk))
362 return PTR_ERR(clk);
364 va_start(ap, dev_fmt);
365 cl = vclkdev_alloc(clk, con_id, dev_fmt, ap);
366 va_end(ap);
368 if (!cl)
369 return -ENOMEM;
371 clkdev_add(cl);
373 return 0;
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)
390 unsigned i;
392 if (IS_ERR(clk))
393 return PTR_ERR(clk);
395 for (i = 0; i < num; i++, cl++) {
396 cl->clk = clk;
397 clkdev_add(cl);
400 return 0;
402 EXPORT_SYMBOL(clk_register_clkdevs);