Merge branch 'clk-at91' into clk-next
[linux-2.6-block.git] / drivers / clk / clkdev.c
1 /*
2  * drivers/clk/clkdev.c
3  *
4  *  Copyright (C) 2008 Russell King.
5  *
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.
9  *
10  * Helper for the clk API to assist looking up a struct clk.
11  */
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>
24
25 #include "clk.h"
26
27 static LIST_HEAD(clocks);
28 static DEFINE_MUTEX(clocks_mutex);
29
30 #if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK)
31 static struct clk *__of_clk_get(struct device_node *np, int index,
32                                const char *dev_id, const char *con_id)
33 {
34         struct of_phandle_args clkspec;
35         struct clk *clk;
36         int rc;
37
38         rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
39                                         &clkspec);
40         if (rc)
41                 return ERR_PTR(rc);
42
43         clk = __of_clk_get_from_provider(&clkspec, dev_id, con_id);
44         of_node_put(clkspec.np);
45
46         return clk;
47 }
48
49 struct clk *of_clk_get(struct device_node *np, int index)
50 {
51         return __of_clk_get(np, index, np->full_name, NULL);
52 }
53 EXPORT_SYMBOL(of_clk_get);
54
55 /*
56  * Beware the return values when np is valid, but no clock provider is found.
57  * If name == NULL, the function returns -ENOENT.
58  * If name != NULL, the function returns -EINVAL. This is because __of_clk_get()
59  * is called even if of_property_match_string() returns an error.
60  */
61 static struct clk *__of_clk_get_by_name(struct device_node *np,
62                                         const char *dev_id,
63                                         const char *name)
64 {
65         struct clk *clk = ERR_PTR(-ENOENT);
66
67         /* Walk up the tree of devices looking for a clock that matches */
68         while (np) {
69                 int index = 0;
70
71                 /*
72                  * For named clocks, first look up the name in the
73                  * "clock-names" property.  If it cannot be found, then
74                  * index will be an error code, and of_clk_get() will fail.
75                  */
76                 if (name)
77                         index = of_property_match_string(np, "clock-names", name);
78                 clk = __of_clk_get(np, index, dev_id, name);
79                 if (!IS_ERR(clk)) {
80                         break;
81                 } else if (name && index >= 0) {
82                         if (PTR_ERR(clk) != -EPROBE_DEFER)
83                                 pr_err("ERROR: could not get clock %pOF:%s(%i)\n",
84                                         np, name ? name : "", index);
85                         return clk;
86                 }
87
88                 /*
89                  * No matching clock found on this node.  If the parent node
90                  * has a "clock-ranges" property, then we can try one of its
91                  * clocks.
92                  */
93                 np = np->parent;
94                 if (np && !of_get_property(np, "clock-ranges", NULL))
95                         break;
96         }
97
98         return clk;
99 }
100
101 /**
102  * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
103  * @np: pointer to clock consumer node
104  * @name: name of consumer's clock input, or NULL for the first clock reference
105  *
106  * This function parses the clocks and clock-names properties,
107  * and uses them to look up the struct clk from the registered list of clock
108  * providers.
109  */
110 struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
111 {
112         if (!np)
113                 return ERR_PTR(-ENOENT);
114
115         return __of_clk_get_by_name(np, np->full_name, name);
116 }
117 EXPORT_SYMBOL(of_clk_get_by_name);
118
119 #else /* defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) */
120
121 static struct clk *__of_clk_get_by_name(struct device_node *np,
122                                         const char *dev_id,
123                                         const char *name)
124 {
125         return ERR_PTR(-ENOENT);
126 }
127 #endif
128
129 /*
130  * Find the correct struct clk for the device and connection ID.
131  * We do slightly fuzzy matching here:
132  *  An entry with a NULL ID is assumed to be a wildcard.
133  *  If an entry has a device ID, it must match
134  *  If an entry has a connection ID, it must match
135  * Then we take the most specific entry - with the following
136  * order of precedence: dev+con > dev only > con only.
137  */
138 static struct clk_lookup *clk_find(const char *dev_id, const char *con_id)
139 {
140         struct clk_lookup *p, *cl = NULL;
141         int match, best_found = 0, best_possible = 0;
142
143         if (dev_id)
144                 best_possible += 2;
145         if (con_id)
146                 best_possible += 1;
147
148         list_for_each_entry(p, &clocks, node) {
149                 match = 0;
150                 if (p->dev_id) {
151                         if (!dev_id || strcmp(p->dev_id, dev_id))
152                                 continue;
153                         match += 2;
154                 }
155                 if (p->con_id) {
156                         if (!con_id || strcmp(p->con_id, con_id))
157                                 continue;
158                         match += 1;
159                 }
160
161                 if (match > best_found) {
162                         cl = p;
163                         if (match != best_possible)
164                                 best_found = match;
165                         else
166                                 break;
167                 }
168         }
169         return cl;
170 }
171
172 struct clk *clk_get_sys(const char *dev_id, const char *con_id)
173 {
174         struct clk_lookup *cl;
175         struct clk *clk = NULL;
176
177         mutex_lock(&clocks_mutex);
178
179         cl = clk_find(dev_id, con_id);
180         if (!cl)
181                 goto out;
182
183         clk = __clk_create_clk(cl->clk_hw, dev_id, con_id);
184         if (IS_ERR(clk))
185                 goto out;
186
187         if (!__clk_get(clk)) {
188                 __clk_free_clk(clk);
189                 cl = NULL;
190                 goto out;
191         }
192
193 out:
194         mutex_unlock(&clocks_mutex);
195
196         return cl ? clk : ERR_PTR(-ENOENT);
197 }
198 EXPORT_SYMBOL(clk_get_sys);
199
200 struct clk *clk_get(struct device *dev, const char *con_id)
201 {
202         const char *dev_id = dev ? dev_name(dev) : NULL;
203         struct clk *clk;
204
205         if (dev && dev->of_node) {
206                 clk = __of_clk_get_by_name(dev->of_node, dev_id, con_id);
207                 if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
208                         return clk;
209         }
210
211         return clk_get_sys(dev_id, con_id);
212 }
213 EXPORT_SYMBOL(clk_get);
214
215 void clk_put(struct clk *clk)
216 {
217         __clk_put(clk);
218 }
219 EXPORT_SYMBOL(clk_put);
220
221 static void __clkdev_add(struct clk_lookup *cl)
222 {
223         mutex_lock(&clocks_mutex);
224         list_add_tail(&cl->node, &clocks);
225         mutex_unlock(&clocks_mutex);
226 }
227
228 void clkdev_add(struct clk_lookup *cl)
229 {
230         if (!cl->clk_hw)
231                 cl->clk_hw = __clk_get_hw(cl->clk);
232         __clkdev_add(cl);
233 }
234 EXPORT_SYMBOL(clkdev_add);
235
236 void clkdev_add_table(struct clk_lookup *cl, size_t num)
237 {
238         mutex_lock(&clocks_mutex);
239         while (num--) {
240                 cl->clk_hw = __clk_get_hw(cl->clk);
241                 list_add_tail(&cl->node, &clocks);
242                 cl++;
243         }
244         mutex_unlock(&clocks_mutex);
245 }
246
247 #define MAX_DEV_ID      20
248 #define MAX_CON_ID      16
249
250 struct clk_lookup_alloc {
251         struct clk_lookup cl;
252         char    dev_id[MAX_DEV_ID];
253         char    con_id[MAX_CON_ID];
254 };
255
256 static struct clk_lookup * __ref
257 vclkdev_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt,
258         va_list ap)
259 {
260         struct clk_lookup_alloc *cla;
261
262         cla = kzalloc(sizeof(*cla), GFP_KERNEL);
263         if (!cla)
264                 return NULL;
265
266         cla->cl.clk_hw = hw;
267         if (con_id) {
268                 strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
269                 cla->cl.con_id = cla->con_id;
270         }
271
272         if (dev_fmt) {
273                 vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
274                 cla->cl.dev_id = cla->dev_id;
275         }
276
277         return &cla->cl;
278 }
279
280 static struct clk_lookup *
281 vclkdev_create(struct clk_hw *hw, const char *con_id, const char *dev_fmt,
282         va_list ap)
283 {
284         struct clk_lookup *cl;
285
286         cl = vclkdev_alloc(hw, con_id, dev_fmt, ap);
287         if (cl)
288                 __clkdev_add(cl);
289
290         return cl;
291 }
292
293 struct clk_lookup * __ref
294 clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...)
295 {
296         struct clk_lookup *cl;
297         va_list ap;
298
299         va_start(ap, dev_fmt);
300         cl = vclkdev_alloc(__clk_get_hw(clk), con_id, dev_fmt, ap);
301         va_end(ap);
302
303         return cl;
304 }
305 EXPORT_SYMBOL(clkdev_alloc);
306
307 struct clk_lookup *
308 clkdev_hw_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt, ...)
309 {
310         struct clk_lookup *cl;
311         va_list ap;
312
313         va_start(ap, dev_fmt);
314         cl = vclkdev_alloc(hw, con_id, dev_fmt, ap);
315         va_end(ap);
316
317         return cl;
318 }
319 EXPORT_SYMBOL(clkdev_hw_alloc);
320
321 /**
322  * clkdev_create - allocate and add a clkdev lookup structure
323  * @clk: struct clk to associate with all clk_lookups
324  * @con_id: connection ID string on device
325  * @dev_fmt: format string describing device name
326  *
327  * Returns a clk_lookup structure, which can be later unregistered and
328  * freed.
329  */
330 struct clk_lookup *clkdev_create(struct clk *clk, const char *con_id,
331         const char *dev_fmt, ...)
332 {
333         struct clk_lookup *cl;
334         va_list ap;
335
336         va_start(ap, dev_fmt);
337         cl = vclkdev_create(__clk_get_hw(clk), con_id, dev_fmt, ap);
338         va_end(ap);
339
340         return cl;
341 }
342 EXPORT_SYMBOL_GPL(clkdev_create);
343
344 /**
345  * clkdev_hw_create - allocate and add a clkdev lookup structure
346  * @hw: struct clk_hw to associate with all clk_lookups
347  * @con_id: connection ID string on device
348  * @dev_fmt: format string describing device name
349  *
350  * Returns a clk_lookup structure, which can be later unregistered and
351  * freed.
352  */
353 struct clk_lookup *clkdev_hw_create(struct clk_hw *hw, const char *con_id,
354         const char *dev_fmt, ...)
355 {
356         struct clk_lookup *cl;
357         va_list ap;
358
359         va_start(ap, dev_fmt);
360         cl = vclkdev_create(hw, con_id, dev_fmt, ap);
361         va_end(ap);
362
363         return cl;
364 }
365 EXPORT_SYMBOL_GPL(clkdev_hw_create);
366
367 int clk_add_alias(const char *alias, const char *alias_dev_name,
368         const char *con_id, struct device *dev)
369 {
370         struct clk *r = clk_get(dev, con_id);
371         struct clk_lookup *l;
372
373         if (IS_ERR(r))
374                 return PTR_ERR(r);
375
376         l = clkdev_create(r, alias, alias_dev_name ? "%s" : NULL,
377                           alias_dev_name);
378         clk_put(r);
379
380         return l ? 0 : -ENODEV;
381 }
382 EXPORT_SYMBOL(clk_add_alias);
383
384 /*
385  * clkdev_drop - remove a clock dynamically allocated
386  */
387 void clkdev_drop(struct clk_lookup *cl)
388 {
389         mutex_lock(&clocks_mutex);
390         list_del(&cl->node);
391         mutex_unlock(&clocks_mutex);
392         kfree(cl);
393 }
394 EXPORT_SYMBOL(clkdev_drop);
395
396 static struct clk_lookup *__clk_register_clkdev(struct clk_hw *hw,
397                                                 const char *con_id,
398                                                 const char *dev_id, ...)
399 {
400         struct clk_lookup *cl;
401         va_list ap;
402
403         va_start(ap, dev_id);
404         cl = vclkdev_create(hw, con_id, dev_id, ap);
405         va_end(ap);
406
407         return cl;
408 }
409
410 static int do_clk_register_clkdev(struct clk_hw *hw,
411         struct clk_lookup **cl, const char *con_id, const char *dev_id)
412 {
413         if (IS_ERR(hw))
414                 return PTR_ERR(hw);
415         /*
416          * Since dev_id can be NULL, and NULL is handled specially, we must
417          * pass it as either a NULL format string, or with "%s".
418          */
419         if (dev_id)
420                 *cl = __clk_register_clkdev(hw, con_id, "%s", dev_id);
421         else
422                 *cl = __clk_register_clkdev(hw, con_id, NULL);
423
424         return *cl ? 0 : -ENOMEM;
425 }
426
427 /**
428  * clk_register_clkdev - register one clock lookup for a struct clk
429  * @clk: struct clk to associate with all clk_lookups
430  * @con_id: connection ID string on device
431  * @dev_id: string describing device name
432  *
433  * con_id or dev_id may be NULL as a wildcard, just as in the rest of
434  * clkdev.
435  *
436  * To make things easier for mass registration, we detect error clks
437  * from a previous clk_register() call, and return the error code for
438  * those.  This is to permit this function to be called immediately
439  * after clk_register().
440  */
441 int clk_register_clkdev(struct clk *clk, const char *con_id,
442         const char *dev_id)
443 {
444         struct clk_lookup *cl;
445
446         if (IS_ERR(clk))
447                 return PTR_ERR(clk);
448
449         return do_clk_register_clkdev(__clk_get_hw(clk), &cl, con_id,
450                                               dev_id);
451 }
452 EXPORT_SYMBOL(clk_register_clkdev);
453
454 /**
455  * clk_hw_register_clkdev - register one clock lookup for a struct clk_hw
456  * @hw: struct clk_hw to associate with all clk_lookups
457  * @con_id: connection ID string on device
458  * @dev_id: format string describing device name
459  *
460  * con_id or dev_id may be NULL as a wildcard, just as in the rest of
461  * clkdev.
462  *
463  * To make things easier for mass registration, we detect error clk_hws
464  * from a previous clk_hw_register_*() call, and return the error code for
465  * those.  This is to permit this function to be called immediately
466  * after clk_hw_register_*().
467  */
468 int clk_hw_register_clkdev(struct clk_hw *hw, const char *con_id,
469         const char *dev_id)
470 {
471         struct clk_lookup *cl;
472
473         return do_clk_register_clkdev(hw, &cl, con_id, dev_id);
474 }
475 EXPORT_SYMBOL(clk_hw_register_clkdev);
476
477 static void devm_clkdev_release(struct device *dev, void *res)
478 {
479         clkdev_drop(*(struct clk_lookup **)res);
480 }
481
482 static int devm_clk_match_clkdev(struct device *dev, void *res, void *data)
483 {
484         struct clk_lookup **l = res;
485
486         return *l == data;
487 }
488
489 /**
490  * devm_clk_release_clkdev - Resource managed clkdev lookup release
491  * @dev: device this lookup is bound
492  * @con_id: connection ID string on device
493  * @dev_id: format string describing device name
494  *
495  * Drop the clkdev lookup created with devm_clk_hw_register_clkdev.
496  * Normally this function will not need to be called and the resource
497  * management code will ensure that the resource is freed.
498  */
499 void devm_clk_release_clkdev(struct device *dev, const char *con_id,
500                              const char *dev_id)
501 {
502         struct clk_lookup *cl;
503         int rval;
504
505         cl = clk_find(dev_id, con_id);
506         WARN_ON(!cl);
507         rval = devres_release(dev, devm_clkdev_release,
508                               devm_clk_match_clkdev, cl);
509         WARN_ON(rval);
510 }
511 EXPORT_SYMBOL(devm_clk_release_clkdev);
512
513 /**
514  * devm_clk_hw_register_clkdev - managed clk lookup registration for clk_hw
515  * @dev: device this lookup is bound
516  * @hw: struct clk_hw to associate with all clk_lookups
517  * @con_id: connection ID string on device
518  * @dev_id: format string describing device name
519  *
520  * con_id or dev_id may be NULL as a wildcard, just as in the rest of
521  * clkdev.
522  *
523  * To make things easier for mass registration, we detect error clk_hws
524  * from a previous clk_hw_register_*() call, and return the error code for
525  * those.  This is to permit this function to be called immediately
526  * after clk_hw_register_*().
527  */
528 int devm_clk_hw_register_clkdev(struct device *dev, struct clk_hw *hw,
529                                 const char *con_id, const char *dev_id)
530 {
531         int rval = -ENOMEM;
532         struct clk_lookup **cl;
533
534         cl = devres_alloc(devm_clkdev_release, sizeof(*cl), GFP_KERNEL);
535         if (cl) {
536                 rval = do_clk_register_clkdev(hw, cl, con_id, dev_id);
537                 if (!rval)
538                         devres_add(dev, cl);
539                 else
540                         devres_free(cl);
541         }
542         return rval;
543 }
544 EXPORT_SYMBOL(devm_clk_hw_register_clkdev);