blob: 29a1ab7af4b827d04e9d629985c4ef3863c3d2c1 [file] [log] [blame]
Russell King0318e692008-11-09 16:32:46 +00001/*
Jean-Christop PLAGNIOL-VILLARD6d803ba2010-11-17 10:04:33 +01002 * drivers/clk/clkdev.c
Russell King0318e692008-11-09 16:32:46 +00003 *
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>
Hartley Sweetenc0c60c42009-08-04 23:38:06 +010020#include <linux/clk.h>
Jean-Christop PLAGNIOL-VILLARD6d803ba2010-11-17 10:04:33 +010021#include <linux/clkdev.h>
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010022#include <linux/clk-provider.h>
Grant Likely766e6a42012-04-09 14:50:06 -050023#include <linux/of.h>
Russell King0318e692008-11-09 16:32:46 +000024
Sylwester Nawrocki3a3d2b02013-08-23 17:03:44 +020025#include "clk.h"
26
Russell King0318e692008-11-09 16:32:46 +000027static LIST_HEAD(clocks);
28static DEFINE_MUTEX(clocks_mutex);
29
Rob Herring137f8a72012-07-18 11:52:23 +080030#if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK)
Sylwester Nawrocki7f05e282014-05-19 19:22:50 +020031
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 */
40struct 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
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010057static struct clk *__of_clk_get(struct device_node *np, int index)
Grant Likely766e6a42012-04-09 14:50:06 -050058{
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
Sylwester Nawrocki7f05e282014-05-19 19:22:50 +020071 clk = of_clk_get_by_clkspec(&clkspec);
Grant Likely766e6a42012-04-09 14:50:06 -050072 of_node_put(clkspec.np);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010073
74 return clk;
75}
76
77struct 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
Grant Likely766e6a42012-04-09 14:50:06 -050084 return clk;
85}
86EXPORT_SYMBOL(of_clk_get);
87
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010088static struct clk *__of_clk_get_by_name(struct device_node *np,
89 const char *dev_id,
90 const char *name)
Grant Likely766e6a42012-04-09 14:50:06 -050091{
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);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100105 clk = __of_clk_get(np, index);
106 if (!IS_ERR(clk)) {
107 clk = __clk_create_clk(__clk_get_hw(clk), dev_id, name);
Grant Likely766e6a42012-04-09 14:50:06 -0500108 break;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100109 }
Grant Likely766e6a42012-04-09 14:50:06 -0500110 else if (name && index >= 0) {
Stephen Boydd8e53c32014-06-13 16:36:31 -0700111 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);
Grant Likely766e6a42012-04-09 14:50:06 -0500114 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}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100129
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 */
139struct 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}
Grant Likely766e6a42012-04-09 14:50:06 -0500146EXPORT_SYMBOL(of_clk_get_by_name);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100147
148#else /* defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) */
149
150static 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}
Grant Likely766e6a42012-04-09 14:50:06 -0500156#endif
157
Russell King409dc362009-01-24 10:14:37 +0000158/*
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
Uwe Kleine-König659431f2010-01-18 16:02:48 +0100165 * order of precedence: dev+con > dev only > con only.
Russell King409dc362009-01-24 10:14:37 +0000166 */
Russell Kinge8bf8df2011-04-30 10:14:08 +0100167static struct clk_lookup *clk_find(const char *dev_id, const char *con_id)
Russell King0318e692008-11-09 16:32:46 +0000168{
Russell Kinge8bf8df2011-04-30 10:14:08 +0100169 struct clk_lookup *p, *cl = NULL;
viresh kumar67b50872012-04-19 04:23:25 +0100170 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;
Russell King0318e692008-11-09 16:32:46 +0000176
177 list_for_each_entry(p, &clocks, node) {
Russell King0318e692008-11-09 16:32:46 +0000178 match = 0;
Russell King409dc362009-01-24 10:14:37 +0000179 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 }
Russell King0318e692008-11-09 16:32:46 +0000189
viresh kumar67b50872012-04-19 04:23:25 +0100190 if (match > best_found) {
Russell Kinge8bf8df2011-04-30 10:14:08 +0100191 cl = p;
viresh kumar67b50872012-04-19 04:23:25 +0100192 if (match != best_possible)
193 best_found = match;
viresh kumare4bf5be2010-03-09 11:54:30 +0100194 else
195 break;
Russell King0318e692008-11-09 16:32:46 +0000196 }
197 }
Russell Kinge8bf8df2011-04-30 10:14:08 +0100198 return cl;
Russell King0318e692008-11-09 16:32:46 +0000199}
200
Sascha Hauer05fd8e732009-03-07 12:55:49 +0100201struct clk *clk_get_sys(const char *dev_id, const char *con_id)
Russell King0318e692008-11-09 16:32:46 +0000202{
Russell Kinge8bf8df2011-04-30 10:14:08 +0100203 struct clk_lookup *cl;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100204 struct clk *clk = NULL;
Russell King0318e692008-11-09 16:32:46 +0000205
206 mutex_lock(&clocks_mutex);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100207
Russell Kinge8bf8df2011-04-30 10:14:08 +0100208 cl = clk_find(dev_id, con_id);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100209 if (!cl)
210 goto out;
211
212 if (!__clk_get(cl->clk)) {
Russell Kinge8bf8df2011-04-30 10:14:08 +0100213 cl = NULL;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100214 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
223out:
Russell King0318e692008-11-09 16:32:46 +0000224 mutex_unlock(&clocks_mutex);
225
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100226 return cl ? clk : ERR_PTR(-ENOENT);
Russell King0318e692008-11-09 16:32:46 +0000227}
Sascha Hauer05fd8e732009-03-07 12:55:49 +0100228EXPORT_SYMBOL(clk_get_sys);
229
230struct clk *clk_get(struct device *dev, const char *con_id)
231{
232 const char *dev_id = dev ? dev_name(dev) : NULL;
Grant Likely766e6a42012-04-09 14:50:06 -0500233 struct clk *clk;
234
235 if (dev) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100236 clk = __of_clk_get_by_name(dev->of_node, dev_id, con_id);
237 if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
Jean-Francois Moinea34cd462013-11-25 19:47:04 +0100238 return clk;
Grant Likely766e6a42012-04-09 14:50:06 -0500239 }
Sascha Hauer05fd8e732009-03-07 12:55:49 +0100240
241 return clk_get_sys(dev_id, con_id);
242}
Russell King0318e692008-11-09 16:32:46 +0000243EXPORT_SYMBOL(clk_get);
244
245void clk_put(struct clk *clk)
246{
247 __clk_put(clk);
248}
249EXPORT_SYMBOL(clk_put);
250
251void 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}
257EXPORT_SYMBOL(clkdev_add);
258
Russell King0a0300d2010-01-12 12:28:00 +0000259void __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
Russell King0318e692008-11-09 16:32:46 +0000269#define MAX_DEV_ID 20
270#define MAX_CON_ID 16
271
272struct clk_lookup_alloc {
273 struct clk_lookup cl;
274 char dev_id[MAX_DEV_ID];
275 char con_id[MAX_CON_ID];
276};
277
Russell Kinge9d7f402012-05-02 09:30:32 +0100278static struct clk_lookup * __init_refok
279vclkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt,
280 va_list ap)
Russell King0318e692008-11-09 16:32:46 +0000281{
282 struct clk_lookup_alloc *cla;
283
Jean-Christop PLAGNIOL-VILLARD6d803ba2010-11-17 10:04:33 +0100284 cla = __clkdev_alloc(sizeof(*cla));
Russell King0318e692008-11-09 16:32:46 +0000285 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) {
Russell King0318e692008-11-09 16:32:46 +0000295 vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
296 cla->cl.dev_id = cla->dev_id;
Russell King0318e692008-11-09 16:32:46 +0000297 }
298
299 return &cla->cl;
300}
Russell Kinge9d7f402012-05-02 09:30:32 +0100301
302struct clk_lookup * __init_refok
303clkdev_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}
Russell King0318e692008-11-09 16:32:46 +0000314EXPORT_SYMBOL(clkdev_alloc);
315
Tony Lindgrenc0683032009-06-03 17:43:14 +0100316int 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}
332EXPORT_SYMBOL(clk_add_alias);
333
Russell King0318e692008-11-09 16:32:46 +0000334/*
335 * clkdev_drop - remove a clock dynamically allocated
336 */
337void 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}
344EXPORT_SYMBOL(clkdev_drop);
Russell Kinge9d7f402012-05-02 09:30:32 +0100345
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 */
360int 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}
Tomeu Vizosoa251361a2015-01-23 12:03:32 +0100380EXPORT_SYMBOL(clk_register_clkdev);
Russell Kinge9d7f402012-05-02 09:30:32 +0100381
382/**
383 * clk_register_clkdevs - register a set of clk_lookup for a struct clk
384 * @clk: struct clk to associate with all clk_lookups
385 * @cl: array of clk_lookup structures with con_id and dev_id pre-initialized
386 * @num: number of clk_lookup structures to register
387 *
388 * To make things easier for mass registration, we detect error clks
389 * from a previous clk_register() call, and return the error code for
390 * those. This is to permit this function to be called immediately
391 * after clk_register().
392 */
393int clk_register_clkdevs(struct clk *clk, struct clk_lookup *cl, size_t num)
394{
395 unsigned i;
396
397 if (IS_ERR(clk))
398 return PTR_ERR(clk);
399
400 for (i = 0; i < num; i++, cl++) {
401 cl->clk = clk;
402 clkdev_add(cl);
403 }
404
405 return 0;
406}
407EXPORT_SYMBOL(clk_register_clkdevs);