clk: Make clk API return per-user struct clk instances
[firefly-linux-kernel-4.4.55.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
32 /**
33  * of_clk_get_by_clkspec() - Lookup a clock form a clock provider
34  * @clkspec: pointer to a clock specifier data structure
35  *
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.
39  */
40 struct clk *of_clk_get_by_clkspec(struct of_phandle_args *clkspec)
41 {
42         struct clk *clk;
43
44         if (!clkspec)
45                 return ERR_PTR(-EINVAL);
46
47         of_clk_lock();
48         clk = __of_clk_get_from_provider(clkspec);
49
50         if (!IS_ERR(clk) && !__clk_get(clk))
51                 clk = ERR_PTR(-ENOENT);
52
53         of_clk_unlock();
54         return clk;
55 }
56
57 static struct clk *__of_clk_get(struct device_node *np, int index)
58 {
59         struct of_phandle_args clkspec;
60         struct clk *clk;
61         int rc;
62
63         if (index < 0)
64                 return ERR_PTR(-EINVAL);
65
66         rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
67                                         &clkspec);
68         if (rc)
69                 return ERR_PTR(rc);
70
71         clk = of_clk_get_by_clkspec(&clkspec);
72         of_node_put(clkspec.np);
73
74         return clk;
75 }
76
77 struct clk *of_clk_get(struct device_node *np, int index)
78 {
79         struct clk *clk = __of_clk_get(np, index);
80
81         if (!IS_ERR(clk))
82                 clk = __clk_create_clk(__clk_get_hw(clk), np->full_name, NULL);
83
84         return clk;
85 }
86 EXPORT_SYMBOL(of_clk_get);
87
88 static struct clk *__of_clk_get_by_name(struct device_node *np,
89                                         const char *dev_id,
90                                         const char *name)
91 {
92         struct clk *clk = ERR_PTR(-ENOENT);
93
94         /* Walk up the tree of devices looking for a clock that matches */
95         while (np) {
96                 int index = 0;
97
98                 /*
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.
102                  */
103                 if (name)
104                         index = of_property_match_string(np, "clock-names", name);
105                 clk = __of_clk_get(np, index);
106                 if (!IS_ERR(clk)) {
107                         clk = __clk_create_clk(__clk_get_hw(clk), dev_id, name);
108                         break;
109                 }
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);
114                         return clk;
115                 }
116
117                 /*
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
120                  * clocks.
121                  */
122                 np = np->parent;
123                 if (np && !of_get_property(np, "clock-ranges", NULL))
124                         break;
125         }
126
127         return clk;
128 }
129
130 /**
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
134  *
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
137  * providers.
138  */
139 struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
140 {
141         if (!np)
142                 return ERR_PTR(-ENOENT);
143
144         return __of_clk_get_by_name(np, np->full_name, name);
145 }
146 EXPORT_SYMBOL(of_clk_get_by_name);
147
148 #else /* defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) */
149
150 static struct clk *__of_clk_get_by_name(struct device_node *np,
151                                         const char *dev_id,
152                                         const char *name)
153 {
154         return ERR_PTR(-ENOENT);
155 }
156 #endif
157
158 /*
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.
166  */
167 static struct clk_lookup *clk_find(const char *dev_id, const char *con_id)
168 {
169         struct clk_lookup *p, *cl = NULL;
170         int match, best_found = 0, best_possible = 0;
171
172         if (dev_id)
173                 best_possible += 2;
174         if (con_id)
175                 best_possible += 1;
176
177         list_for_each_entry(p, &clocks, node) {
178                 match = 0;
179                 if (p->dev_id) {
180                         if (!dev_id || strcmp(p->dev_id, dev_id))
181                                 continue;
182                         match += 2;
183                 }
184                 if (p->con_id) {
185                         if (!con_id || strcmp(p->con_id, con_id))
186                                 continue;
187                         match += 1;
188                 }
189
190                 if (match > best_found) {
191                         cl = p;
192                         if (match != best_possible)
193                                 best_found = match;
194                         else
195                                 break;
196                 }
197         }
198         return cl;
199 }
200
201 struct clk *clk_get_sys(const char *dev_id, const char *con_id)
202 {
203         struct clk_lookup *cl;
204         struct clk *clk = NULL;
205
206         mutex_lock(&clocks_mutex);
207
208         cl = clk_find(dev_id, con_id);
209         if (!cl)
210                 goto out;
211
212         if (!__clk_get(cl->clk)) {
213                 cl = NULL;
214                 goto out;
215         }
216
217 #if defined(CONFIG_COMMON_CLK)
218         clk = __clk_create_clk(__clk_get_hw(cl->clk), dev_id, con_id);
219 #else
220         clk = cl->clk;
221 #endif
222
223 out:
224         mutex_unlock(&clocks_mutex);
225
226         return cl ? clk : ERR_PTR(-ENOENT);
227 }
228 EXPORT_SYMBOL(clk_get_sys);
229
230 struct clk *clk_get(struct device *dev, const char *con_id)
231 {
232         const char *dev_id = dev ? dev_name(dev) : NULL;
233         struct clk *clk;
234
235         if (dev) {
236                 clk = __of_clk_get_by_name(dev->of_node, dev_id, con_id);
237                 if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
238                         return clk;
239         }
240
241         return clk_get_sys(dev_id, con_id);
242 }
243 EXPORT_SYMBOL(clk_get);
244
245 void clk_put(struct clk *clk)
246 {
247         __clk_put(clk);
248 }
249 EXPORT_SYMBOL(clk_put);
250
251 void clkdev_add(struct clk_lookup *cl)
252 {
253         mutex_lock(&clocks_mutex);
254         list_add_tail(&cl->node, &clocks);
255         mutex_unlock(&clocks_mutex);
256 }
257 EXPORT_SYMBOL(clkdev_add);
258
259 void __init clkdev_add_table(struct clk_lookup *cl, size_t num)
260 {
261         mutex_lock(&clocks_mutex);
262         while (num--) {
263                 list_add_tail(&cl->node, &clocks);
264                 cl++;
265         }
266         mutex_unlock(&clocks_mutex);
267 }
268
269 #define MAX_DEV_ID      20
270 #define MAX_CON_ID      16
271
272 struct clk_lookup_alloc {
273         struct clk_lookup cl;
274         char    dev_id[MAX_DEV_ID];
275         char    con_id[MAX_CON_ID];
276 };
277
278 static struct clk_lookup * __init_refok
279 vclkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt,
280         va_list ap)
281 {
282         struct clk_lookup_alloc *cla;
283
284         cla = __clkdev_alloc(sizeof(*cla));
285         if (!cla)
286                 return NULL;
287
288         cla->cl.clk = clk;
289         if (con_id) {
290                 strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
291                 cla->cl.con_id = cla->con_id;
292         }
293
294         if (dev_fmt) {
295                 vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
296                 cla->cl.dev_id = cla->dev_id;
297         }
298
299         return &cla->cl;
300 }
301
302 struct clk_lookup * __init_refok
303 clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...)
304 {
305         struct clk_lookup *cl;
306         va_list ap;
307
308         va_start(ap, dev_fmt);
309         cl = vclkdev_alloc(clk, con_id, dev_fmt, ap);
310         va_end(ap);
311
312         return cl;
313 }
314 EXPORT_SYMBOL(clkdev_alloc);
315
316 int clk_add_alias(const char *alias, const char *alias_dev_name, char *id,
317         struct device *dev)
318 {
319         struct clk *r = clk_get(dev, id);
320         struct clk_lookup *l;
321
322         if (IS_ERR(r))
323                 return PTR_ERR(r);
324
325         l = clkdev_alloc(r, alias, alias_dev_name);
326         clk_put(r);
327         if (!l)
328                 return -ENODEV;
329         clkdev_add(l);
330         return 0;
331 }
332 EXPORT_SYMBOL(clk_add_alias);
333
334 /*
335  * clkdev_drop - remove a clock dynamically allocated
336  */
337 void clkdev_drop(struct clk_lookup *cl)
338 {
339         mutex_lock(&clocks_mutex);
340         list_del(&cl->node);
341         mutex_unlock(&clocks_mutex);
342         kfree(cl);
343 }
344 EXPORT_SYMBOL(clkdev_drop);
345
346 /**
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
351  *
352  * con_id or dev_id may be NULL as a wildcard, just as in the rest of
353  * clkdev.
354  *
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().
359  */
360 int clk_register_clkdev(struct clk *clk, const char *con_id,
361         const char *dev_fmt, ...)
362 {
363         struct clk_lookup *cl;
364         va_list ap;
365
366         if (IS_ERR(clk))
367                 return PTR_ERR(clk);
368
369         va_start(ap, dev_fmt);
370         cl = vclkdev_alloc(clk, con_id, dev_fmt, ap);
371         va_end(ap);
372
373         if (!cl)
374                 return -ENOMEM;
375
376         clkdev_add(cl);
377
378         return 0;
379 }
380
381 /**
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
386  *
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().
391  */
392 int clk_register_clkdevs(struct clk *clk, struct clk_lookup *cl, size_t num)
393 {
394         unsigned i;
395
396         if (IS_ERR(clk))
397                 return PTR_ERR(clk);
398
399         for (i = 0; i < num; i++, cl++) {
400                 cl->clk = clk;
401                 clkdev_add(cl);
402         }
403
404         return 0;
405 }
406 EXPORT_SYMBOL(clk_register_clkdevs);