blob: 043fd3633373982f0408de1618370c6d63d5887b [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
Stephen Boyd73e0e492015-02-06 11:42:43 -080032static struct clk *__of_clk_get_by_clkspec(struct of_phandle_args *clkspec,
33 const char *dev_id, const char *con_id)
34{
35 struct clk *clk;
36
37 if (!clkspec)
38 return ERR_PTR(-EINVAL);
39
40 of_clk_lock();
41 clk = __of_clk_get_from_provider(clkspec, dev_id, con_id);
42 of_clk_unlock();
43 return clk;
44}
45
Sylwester Nawrocki7f05e282014-05-19 19:22:50 +020046/**
47 * of_clk_get_by_clkspec() - Lookup a clock form a clock provider
48 * @clkspec: pointer to a clock specifier data structure
49 *
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.
53 */
54struct clk *of_clk_get_by_clkspec(struct of_phandle_args *clkspec)
55{
Stephen Boyd73e0e492015-02-06 11:42:43 -080056 return __of_clk_get_by_clkspec(clkspec, NULL, __func__);
Sylwester Nawrocki7f05e282014-05-19 19:22:50 +020057}
58
Stephen Boyd73e0e492015-02-06 11:42:43 -080059static struct clk *__of_clk_get(struct device_node *np, int index,
60 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -050061{
62 struct of_phandle_args clkspec;
63 struct clk *clk;
64 int rc;
65
66 if (index < 0)
67 return ERR_PTR(-EINVAL);
68
69 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
70 &clkspec);
71 if (rc)
72 return ERR_PTR(rc);
73
Stephen Boyd73e0e492015-02-06 11:42:43 -080074 clk = __of_clk_get_by_clkspec(&clkspec, dev_id, con_id);
Grant Likely766e6a42012-04-09 14:50:06 -050075 of_node_put(clkspec.np);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010076
77 return clk;
78}
79
80struct clk *of_clk_get(struct device_node *np, int index)
81{
Stephen Boyd73e0e492015-02-06 11:42:43 -080082 return __of_clk_get(np, index, np->full_name, NULL);
Grant Likely766e6a42012-04-09 14:50:06 -050083}
84EXPORT_SYMBOL(of_clk_get);
85
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010086static struct clk *__of_clk_get_by_name(struct device_node *np,
87 const char *dev_id,
88 const char *name)
Grant Likely766e6a42012-04-09 14:50:06 -050089{
90 struct clk *clk = ERR_PTR(-ENOENT);
91
92 /* Walk up the tree of devices looking for a clock that matches */
93 while (np) {
94 int index = 0;
95
96 /*
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.
100 */
101 if (name)
102 index = of_property_match_string(np, "clock-names", name);
Stephen Boyd73e0e492015-02-06 11:42:43 -0800103 clk = __of_clk_get(np, index, dev_id, name);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100104 if (!IS_ERR(clk)) {
Grant Likely766e6a42012-04-09 14:50:06 -0500105 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -0800106 } else if (name && index >= 0) {
Stephen Boydd8e53c32014-06-13 16:36:31 -0700107 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);
Grant Likely766e6a42012-04-09 14:50:06 -0500110 return clk;
111 }
112
113 /*
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.
117 */
118 np = np->parent;
119 if (np && !of_get_property(np, "clock-ranges", NULL))
120 break;
121 }
122
123 return clk;
124}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100125
126/**
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
130 *
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.
134 */
135struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
136{
137 if (!np)
138 return ERR_PTR(-ENOENT);
139
140 return __of_clk_get_by_name(np, np->full_name, name);
141}
Grant Likely766e6a42012-04-09 14:50:06 -0500142EXPORT_SYMBOL(of_clk_get_by_name);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100143
144#else /* defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) */
145
146static struct clk *__of_clk_get_by_name(struct device_node *np,
147 const char *dev_id,
148 const char *name)
149{
150 return ERR_PTR(-ENOENT);
151}
Grant Likely766e6a42012-04-09 14:50:06 -0500152#endif
153
Russell King409dc362009-01-24 10:14:37 +0000154/*
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
Uwe Kleine-König659431f2010-01-18 16:02:48 +0100161 * order of precedence: dev+con > dev only > con only.
Russell King409dc362009-01-24 10:14:37 +0000162 */
Russell Kinge8bf8df2011-04-30 10:14:08 +0100163static struct clk_lookup *clk_find(const char *dev_id, const char *con_id)
Russell King0318e692008-11-09 16:32:46 +0000164{
Russell Kinge8bf8df2011-04-30 10:14:08 +0100165 struct clk_lookup *p, *cl = NULL;
viresh kumar67b50872012-04-19 04:23:25 +0100166 int match, best_found = 0, best_possible = 0;
167
168 if (dev_id)
169 best_possible += 2;
170 if (con_id)
171 best_possible += 1;
Russell King0318e692008-11-09 16:32:46 +0000172
173 list_for_each_entry(p, &clocks, node) {
Russell King0318e692008-11-09 16:32:46 +0000174 match = 0;
Russell King409dc362009-01-24 10:14:37 +0000175 if (p->dev_id) {
176 if (!dev_id || strcmp(p->dev_id, dev_id))
177 continue;
178 match += 2;
179 }
180 if (p->con_id) {
181 if (!con_id || strcmp(p->con_id, con_id))
182 continue;
183 match += 1;
184 }
Russell King0318e692008-11-09 16:32:46 +0000185
viresh kumar67b50872012-04-19 04:23:25 +0100186 if (match > best_found) {
Russell Kinge8bf8df2011-04-30 10:14:08 +0100187 cl = p;
viresh kumar67b50872012-04-19 04:23:25 +0100188 if (match != best_possible)
189 best_found = match;
viresh kumare4bf5be2010-03-09 11:54:30 +0100190 else
191 break;
Russell King0318e692008-11-09 16:32:46 +0000192 }
193 }
Russell Kinge8bf8df2011-04-30 10:14:08 +0100194 return cl;
Russell King0318e692008-11-09 16:32:46 +0000195}
196
Sascha Hauer05fd8e72009-03-07 12:55:49 +0100197struct clk *clk_get_sys(const char *dev_id, const char *con_id)
Russell King0318e692008-11-09 16:32:46 +0000198{
Russell Kinge8bf8df2011-04-30 10:14:08 +0100199 struct clk_lookup *cl;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100200 struct clk *clk = NULL;
Russell King0318e692008-11-09 16:32:46 +0000201
202 mutex_lock(&clocks_mutex);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100203
Russell Kinge8bf8df2011-04-30 10:14:08 +0100204 cl = clk_find(dev_id, con_id);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100205 if (!cl)
206 goto out;
207
Stephen Boyd73e0e492015-02-06 11:42:43 -0800208 clk = __clk_create_clk(__clk_get_hw(cl->clk), dev_id, con_id);
209 if (IS_ERR(clk))
210 goto out;
211
212 if (!__clk_get(clk)) {
213 __clk_free_clk(clk);
Russell Kinge8bf8df2011-04-30 10:14:08 +0100214 cl = NULL;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100215 goto out;
216 }
217
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100218out:
Russell King0318e692008-11-09 16:32:46 +0000219 mutex_unlock(&clocks_mutex);
220
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100221 return cl ? clk : ERR_PTR(-ENOENT);
Russell King0318e692008-11-09 16:32:46 +0000222}
Sascha Hauer05fd8e72009-03-07 12:55:49 +0100223EXPORT_SYMBOL(clk_get_sys);
224
225struct clk *clk_get(struct device *dev, const char *con_id)
226{
227 const char *dev_id = dev ? dev_name(dev) : NULL;
Grant Likely766e6a42012-04-09 14:50:06 -0500228 struct clk *clk;
229
230 if (dev) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100231 clk = __of_clk_get_by_name(dev->of_node, dev_id, con_id);
232 if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
Jean-Francois Moinea34cd462013-11-25 19:47:04 +0100233 return clk;
Grant Likely766e6a42012-04-09 14:50:06 -0500234 }
Sascha Hauer05fd8e72009-03-07 12:55:49 +0100235
236 return clk_get_sys(dev_id, con_id);
237}
Russell King0318e692008-11-09 16:32:46 +0000238EXPORT_SYMBOL(clk_get);
239
240void clk_put(struct clk *clk)
241{
242 __clk_put(clk);
243}
244EXPORT_SYMBOL(clk_put);
245
246void clkdev_add(struct clk_lookup *cl)
247{
248 mutex_lock(&clocks_mutex);
249 list_add_tail(&cl->node, &clocks);
250 mutex_unlock(&clocks_mutex);
251}
252EXPORT_SYMBOL(clkdev_add);
253
Russell King0a0300d2010-01-12 12:28:00 +0000254void __init clkdev_add_table(struct clk_lookup *cl, size_t num)
255{
256 mutex_lock(&clocks_mutex);
257 while (num--) {
258 list_add_tail(&cl->node, &clocks);
259 cl++;
260 }
261 mutex_unlock(&clocks_mutex);
262}
263
Russell King0318e692008-11-09 16:32:46 +0000264#define MAX_DEV_ID 20
265#define MAX_CON_ID 16
266
267struct clk_lookup_alloc {
268 struct clk_lookup cl;
269 char dev_id[MAX_DEV_ID];
270 char con_id[MAX_CON_ID];
271};
272
Russell Kinge9d7f402012-05-02 09:30:32 +0100273static struct clk_lookup * __init_refok
274vclkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt,
275 va_list ap)
Russell King0318e692008-11-09 16:32:46 +0000276{
277 struct clk_lookup_alloc *cla;
278
Jean-Christop PLAGNIOL-VILLARD6d803ba2010-11-17 10:04:33 +0100279 cla = __clkdev_alloc(sizeof(*cla));
Russell King0318e692008-11-09 16:32:46 +0000280 if (!cla)
281 return NULL;
282
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;
287 }
288
289 if (dev_fmt) {
Russell King0318e692008-11-09 16:32:46 +0000290 vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
291 cla->cl.dev_id = cla->dev_id;
Russell King0318e692008-11-09 16:32:46 +0000292 }
293
294 return &cla->cl;
295}
Russell Kinge9d7f402012-05-02 09:30:32 +0100296
297struct clk_lookup * __init_refok
298clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...)
299{
300 struct clk_lookup *cl;
301 va_list ap;
302
303 va_start(ap, dev_fmt);
304 cl = vclkdev_alloc(clk, con_id, dev_fmt, ap);
305 va_end(ap);
306
307 return cl;
308}
Russell King0318e692008-11-09 16:32:46 +0000309EXPORT_SYMBOL(clkdev_alloc);
310
Tony Lindgrenc0683032009-06-03 17:43:14 +0100311int clk_add_alias(const char *alias, const char *alias_dev_name, char *id,
312 struct device *dev)
313{
314 struct clk *r = clk_get(dev, id);
315 struct clk_lookup *l;
316
317 if (IS_ERR(r))
318 return PTR_ERR(r);
319
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;
326}
327EXPORT_SYMBOL(clk_add_alias);
328
Russell King0318e692008-11-09 16:32:46 +0000329/*
330 * clkdev_drop - remove a clock dynamically allocated
331 */
332void clkdev_drop(struct clk_lookup *cl)
333{
334 mutex_lock(&clocks_mutex);
335 list_del(&cl->node);
336 mutex_unlock(&clocks_mutex);
337 kfree(cl);
338}
339EXPORT_SYMBOL(clkdev_drop);
Russell Kinge9d7f402012-05-02 09:30:32 +0100340
341/**
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
346 *
347 * con_id or dev_id may be NULL as a wildcard, just as in the rest of
348 * clkdev.
349 *
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().
354 */
355int clk_register_clkdev(struct clk *clk, const char *con_id,
356 const char *dev_fmt, ...)
357{
358 struct clk_lookup *cl;
359 va_list ap;
360
361 if (IS_ERR(clk))
362 return PTR_ERR(clk);
363
364 va_start(ap, dev_fmt);
365 cl = vclkdev_alloc(clk, con_id, dev_fmt, ap);
366 va_end(ap);
367
368 if (!cl)
369 return -ENOMEM;
370
371 clkdev_add(cl);
372
373 return 0;
374}
Tomeu Vizosoa251361a2015-01-23 12:03:32 +0100375EXPORT_SYMBOL(clk_register_clkdev);
Russell Kinge9d7f402012-05-02 09:30:32 +0100376
377/**
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
382 *
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().
387 */
388int clk_register_clkdevs(struct clk *clk, struct clk_lookup *cl, size_t num)
389{
390 unsigned i;
391
392 if (IS_ERR(clk))
393 return PTR_ERR(clk);
394
395 for (i = 0; i < num; i++, cl++) {
396 cl->clk = clk;
397 clkdev_add(cl);
398 }
399
400 return 0;
401}
402EXPORT_SYMBOL(clk_register_clkdevs);