blob: 94dcad5190fc55fcf11649181410a00c59b7c700 [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
Shefali Jain0dc6e782017-11-27 13:06:27 +053030#if defined(CONFIG_OF)
Stephen Boyd73e0e492015-02-06 11:42:43 -080031static struct clk *__of_clk_get(struct device_node *np, int index,
32 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -050033{
34 struct of_phandle_args clkspec;
35 struct clk *clk;
36 int rc;
37
38 if (index < 0)
39 return ERR_PTR(-EINVAL);
40
41 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
42 &clkspec);
43 if (rc)
44 return ERR_PTR(rc);
45
Stephen Boyd306c3422015-02-05 15:39:11 -080046 clk = __of_clk_get_from_provider(&clkspec, dev_id, con_id);
Grant Likely766e6a42012-04-09 14:50:06 -050047 of_node_put(clkspec.np);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010048
49 return clk;
50}
51
52struct clk *of_clk_get(struct device_node *np, int index)
53{
Stephen Boyd73e0e492015-02-06 11:42:43 -080054 return __of_clk_get(np, index, np->full_name, NULL);
Grant Likely766e6a42012-04-09 14:50:06 -050055}
56EXPORT_SYMBOL(of_clk_get);
57
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010058static struct clk *__of_clk_get_by_name(struct device_node *np,
59 const char *dev_id,
60 const char *name)
Grant Likely766e6a42012-04-09 14:50:06 -050061{
62 struct clk *clk = ERR_PTR(-ENOENT);
63
64 /* Walk up the tree of devices looking for a clock that matches */
65 while (np) {
66 int index = 0;
67
68 /*
69 * For named clocks, first look up the name in the
70 * "clock-names" property. If it cannot be found, then
71 * index will be an error code, and of_clk_get() will fail.
72 */
73 if (name)
74 index = of_property_match_string(np, "clock-names", name);
Stephen Boyd73e0e492015-02-06 11:42:43 -080075 clk = __of_clk_get(np, index, dev_id, name);
Shefali Jain0dc6e782017-11-27 13:06:27 +053076 if (!IS_ERR(clk))
Grant Likely766e6a42012-04-09 14:50:06 -050077 break;
Shefali Jain0dc6e782017-11-27 13:06:27 +053078 else if (name && index >= 0)
Grant Likely766e6a42012-04-09 14:50:06 -050079 return clk;
Grant Likely766e6a42012-04-09 14:50:06 -050080
81 /*
82 * No matching clock found on this node. If the parent node
83 * has a "clock-ranges" property, then we can try one of its
84 * clocks.
85 */
86 np = np->parent;
87 if (np && !of_get_property(np, "clock-ranges", NULL))
88 break;
89 }
90
91 return clk;
92}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010093
94/**
95 * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
96 * @np: pointer to clock consumer node
97 * @name: name of consumer's clock input, or NULL for the first clock reference
98 *
99 * This function parses the clocks and clock-names properties,
100 * and uses them to look up the struct clk from the registered list of clock
101 * providers.
102 */
103struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
104{
105 if (!np)
106 return ERR_PTR(-ENOENT);
107
108 return __of_clk_get_by_name(np, np->full_name, name);
109}
Grant Likely766e6a42012-04-09 14:50:06 -0500110EXPORT_SYMBOL(of_clk_get_by_name);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100111
112#else /* defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) */
113
114static struct clk *__of_clk_get_by_name(struct device_node *np,
115 const char *dev_id,
116 const char *name)
117{
118 return ERR_PTR(-ENOENT);
119}
Grant Likely766e6a42012-04-09 14:50:06 -0500120#endif
121
Russell King409dc362009-01-24 10:14:37 +0000122/*
123 * Find the correct struct clk for the device and connection ID.
124 * We do slightly fuzzy matching here:
125 * An entry with a NULL ID is assumed to be a wildcard.
126 * If an entry has a device ID, it must match
127 * If an entry has a connection ID, it must match
128 * Then we take the most specific entry - with the following
Uwe Kleine-König659431f2010-01-18 16:02:48 +0100129 * order of precedence: dev+con > dev only > con only.
Russell King409dc362009-01-24 10:14:37 +0000130 */
Russell Kinge8bf8df2011-04-30 10:14:08 +0100131static struct clk_lookup *clk_find(const char *dev_id, const char *con_id)
Russell King0318e692008-11-09 16:32:46 +0000132{
Russell Kinge8bf8df2011-04-30 10:14:08 +0100133 struct clk_lookup *p, *cl = NULL;
viresh kumar67b50872012-04-19 04:23:25 +0100134 int match, best_found = 0, best_possible = 0;
135
136 if (dev_id)
137 best_possible += 2;
138 if (con_id)
139 best_possible += 1;
Russell King0318e692008-11-09 16:32:46 +0000140
141 list_for_each_entry(p, &clocks, node) {
Russell King0318e692008-11-09 16:32:46 +0000142 match = 0;
Russell King409dc362009-01-24 10:14:37 +0000143 if (p->dev_id) {
144 if (!dev_id || strcmp(p->dev_id, dev_id))
145 continue;
146 match += 2;
147 }
148 if (p->con_id) {
149 if (!con_id || strcmp(p->con_id, con_id))
150 continue;
151 match += 1;
152 }
Russell King0318e692008-11-09 16:32:46 +0000153
viresh kumar67b50872012-04-19 04:23:25 +0100154 if (match > best_found) {
Russell Kinge8bf8df2011-04-30 10:14:08 +0100155 cl = p;
viresh kumar67b50872012-04-19 04:23:25 +0100156 if (match != best_possible)
157 best_found = match;
viresh kumare4bf5be2010-03-09 11:54:30 +0100158 else
159 break;
Russell King0318e692008-11-09 16:32:46 +0000160 }
161 }
Russell Kinge8bf8df2011-04-30 10:14:08 +0100162 return cl;
Russell King0318e692008-11-09 16:32:46 +0000163}
164
Sascha Hauer05fd8e72009-03-07 12:55:49 +0100165struct clk *clk_get_sys(const char *dev_id, const char *con_id)
Russell King0318e692008-11-09 16:32:46 +0000166{
Russell Kinge8bf8df2011-04-30 10:14:08 +0100167 struct clk_lookup *cl;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100168 struct clk *clk = NULL;
Russell King0318e692008-11-09 16:32:46 +0000169
170 mutex_lock(&clocks_mutex);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100171
Russell Kinge8bf8df2011-04-30 10:14:08 +0100172 cl = clk_find(dev_id, con_id);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100173 if (!cl)
174 goto out;
175
Russell Kingd5622a92015-03-02 15:45:41 +0000176 clk = __clk_create_clk(cl->clk_hw, dev_id, con_id);
Stephen Boyd73e0e492015-02-06 11:42:43 -0800177 if (IS_ERR(clk))
178 goto out;
179
180 if (!__clk_get(clk)) {
181 __clk_free_clk(clk);
Russell Kinge8bf8df2011-04-30 10:14:08 +0100182 cl = NULL;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100183 goto out;
184 }
185
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100186out:
Russell King0318e692008-11-09 16:32:46 +0000187 mutex_unlock(&clocks_mutex);
188
Shefali Jain0dc6e782017-11-27 13:06:27 +0530189 return cl ? cl->clk : ERR_PTR(-ENOENT);
Russell King0318e692008-11-09 16:32:46 +0000190}
Sascha Hauer05fd8e72009-03-07 12:55:49 +0100191EXPORT_SYMBOL(clk_get_sys);
192
193struct clk *clk_get(struct device *dev, const char *con_id)
194{
195 const char *dev_id = dev ? dev_name(dev) : NULL;
Grant Likely766e6a42012-04-09 14:50:06 -0500196 struct clk *clk;
197
198 if (dev) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100199 clk = __of_clk_get_by_name(dev->of_node, dev_id, con_id);
200 if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
Jean-Francois Moinea34cd462013-11-25 19:47:04 +0100201 return clk;
Grant Likely766e6a42012-04-09 14:50:06 -0500202 }
Sascha Hauer05fd8e72009-03-07 12:55:49 +0100203
204 return clk_get_sys(dev_id, con_id);
205}
Russell King0318e692008-11-09 16:32:46 +0000206EXPORT_SYMBOL(clk_get);
207
208void clk_put(struct clk *clk)
209{
210 __clk_put(clk);
211}
212EXPORT_SYMBOL(clk_put);
213
Russell Kingd5622a92015-03-02 15:45:41 +0000214static void __clkdev_add(struct clk_lookup *cl)
Russell King0318e692008-11-09 16:32:46 +0000215{
216 mutex_lock(&clocks_mutex);
217 list_add_tail(&cl->node, &clocks);
218 mutex_unlock(&clocks_mutex);
219}
Russell Kingd5622a92015-03-02 15:45:41 +0000220
221void clkdev_add(struct clk_lookup *cl)
222{
223 if (!cl->clk_hw)
224 cl->clk_hw = __clk_get_hw(cl->clk);
225 __clkdev_add(cl);
226}
Russell King0318e692008-11-09 16:32:46 +0000227EXPORT_SYMBOL(clkdev_add);
228
Russell Kingfba3acd2015-03-10 14:34:00 +0000229void clkdev_add_table(struct clk_lookup *cl, size_t num)
Russell King0a0300d2010-01-12 12:28:00 +0000230{
231 mutex_lock(&clocks_mutex);
232 while (num--) {
Russell Kingd5622a92015-03-02 15:45:41 +0000233 cl->clk_hw = __clk_get_hw(cl->clk);
Russell King0a0300d2010-01-12 12:28:00 +0000234 list_add_tail(&cl->node, &clocks);
235 cl++;
236 }
237 mutex_unlock(&clocks_mutex);
238}
239
Russell King0318e692008-11-09 16:32:46 +0000240#define MAX_DEV_ID 20
241#define MAX_CON_ID 16
242
243struct clk_lookup_alloc {
244 struct clk_lookup cl;
245 char dev_id[MAX_DEV_ID];
246 char con_id[MAX_CON_ID];
247};
248
Fabian Frederickbd721ea2016-08-02 14:03:33 -0700249static struct clk_lookup * __ref
Russell Kingd5622a92015-03-02 15:45:41 +0000250vclkdev_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt,
Russell Kinge9d7f402012-05-02 09:30:32 +0100251 va_list ap)
Russell King0318e692008-11-09 16:32:46 +0000252{
253 struct clk_lookup_alloc *cla;
254
Jean-Christop PLAGNIOL-VILLARD6d803ba2010-11-17 10:04:33 +0100255 cla = __clkdev_alloc(sizeof(*cla));
Russell King0318e692008-11-09 16:32:46 +0000256 if (!cla)
257 return NULL;
258
Russell Kingd5622a92015-03-02 15:45:41 +0000259 cla->cl.clk_hw = hw;
Russell King0318e692008-11-09 16:32:46 +0000260 if (con_id) {
261 strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
262 cla->cl.con_id = cla->con_id;
263 }
264
265 if (dev_fmt) {
Russell King0318e692008-11-09 16:32:46 +0000266 vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
267 cla->cl.dev_id = cla->dev_id;
Russell King0318e692008-11-09 16:32:46 +0000268 }
269
270 return &cla->cl;
271}
Russell Kinge9d7f402012-05-02 09:30:32 +0100272
Russell King25689992015-03-02 15:40:29 +0000273static struct clk_lookup *
274vclkdev_create(struct clk_hw *hw, const char *con_id, const char *dev_fmt,
275 va_list ap)
276{
277 struct clk_lookup *cl;
278
279 cl = vclkdev_alloc(hw, con_id, dev_fmt, ap);
280 if (cl)
281 __clkdev_add(cl);
282
283 return cl;
284}
285
Fabian Frederickbd721ea2016-08-02 14:03:33 -0700286struct clk_lookup * __ref
Russell Kinge9d7f402012-05-02 09:30:32 +0100287clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...)
288{
289 struct clk_lookup *cl;
290 va_list ap;
291
292 va_start(ap, dev_fmt);
Russell Kingd5622a92015-03-02 15:45:41 +0000293 cl = vclkdev_alloc(__clk_get_hw(clk), con_id, dev_fmt, ap);
Russell Kinge9d7f402012-05-02 09:30:32 +0100294 va_end(ap);
295
296 return cl;
297}
Russell King0318e692008-11-09 16:32:46 +0000298EXPORT_SYMBOL(clkdev_alloc);
299
Stephen Boyde4f1b492016-02-08 14:59:49 -0800300struct clk_lookup *
301clkdev_hw_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt, ...)
302{
303 struct clk_lookup *cl;
304 va_list ap;
305
306 va_start(ap, dev_fmt);
307 cl = vclkdev_alloc(hw, con_id, dev_fmt, ap);
308 va_end(ap);
309
310 return cl;
311}
312EXPORT_SYMBOL(clkdev_hw_alloc);
313
Russell King25689992015-03-02 15:40:29 +0000314/**
315 * clkdev_create - allocate and add a clkdev lookup structure
316 * @clk: struct clk to associate with all clk_lookups
317 * @con_id: connection ID string on device
318 * @dev_fmt: format string describing device name
319 *
320 * Returns a clk_lookup structure, which can be later unregistered and
321 * freed.
322 */
323struct clk_lookup *clkdev_create(struct clk *clk, const char *con_id,
324 const char *dev_fmt, ...)
325{
326 struct clk_lookup *cl;
327 va_list ap;
328
329 va_start(ap, dev_fmt);
330 cl = vclkdev_create(__clk_get_hw(clk), con_id, dev_fmt, ap);
331 va_end(ap);
332
333 return cl;
334}
335EXPORT_SYMBOL_GPL(clkdev_create);
336
Stephen Boyde4f1b492016-02-08 14:59:49 -0800337/**
338 * clkdev_hw_create - allocate and add a clkdev lookup structure
339 * @hw: struct clk_hw to associate with all clk_lookups
340 * @con_id: connection ID string on device
341 * @dev_fmt: format string describing device name
342 *
343 * Returns a clk_lookup structure, which can be later unregistered and
344 * freed.
345 */
346struct clk_lookup *clkdev_hw_create(struct clk_hw *hw, const char *con_id,
347 const char *dev_fmt, ...)
348{
349 struct clk_lookup *cl;
350 va_list ap;
351
352 va_start(ap, dev_fmt);
353 cl = vclkdev_create(hw, con_id, dev_fmt, ap);
354 va_end(ap);
355
356 return cl;
357}
358EXPORT_SYMBOL_GPL(clkdev_hw_create);
359
Russell Kingb3d8d7e2015-03-09 10:43:04 +0000360int clk_add_alias(const char *alias, const char *alias_dev_name,
361 const char *con_id, struct device *dev)
Tony Lindgrenc0683032009-06-03 17:43:14 +0100362{
Russell Kingb3d8d7e2015-03-09 10:43:04 +0000363 struct clk *r = clk_get(dev, con_id);
Tony Lindgrenc0683032009-06-03 17:43:14 +0100364 struct clk_lookup *l;
365
366 if (IS_ERR(r))
367 return PTR_ERR(r);
368
Russell King625faa62015-10-20 11:49:44 +0100369 l = clkdev_create(r, alias, alias_dev_name ? "%s" : NULL,
370 alias_dev_name);
Tony Lindgrenc0683032009-06-03 17:43:14 +0100371 clk_put(r);
Russell King25689992015-03-02 15:40:29 +0000372
373 return l ? 0 : -ENODEV;
Tony Lindgrenc0683032009-06-03 17:43:14 +0100374}
375EXPORT_SYMBOL(clk_add_alias);
376
Russell King0318e692008-11-09 16:32:46 +0000377/*
378 * clkdev_drop - remove a clock dynamically allocated
379 */
380void clkdev_drop(struct clk_lookup *cl)
381{
382 mutex_lock(&clocks_mutex);
383 list_del(&cl->node);
384 mutex_unlock(&clocks_mutex);
385 kfree(cl);
386}
387EXPORT_SYMBOL(clkdev_drop);
Russell Kinge9d7f402012-05-02 09:30:32 +0100388
Kees Cook416dd132016-01-26 01:21:26 +0100389static struct clk_lookup *__clk_register_clkdev(struct clk_hw *hw,
390 const char *con_id,
391 const char *dev_id, ...)
392{
393 struct clk_lookup *cl;
394 va_list ap;
395
396 va_start(ap, dev_id);
397 cl = vclkdev_create(hw, con_id, dev_id, ap);
398 va_end(ap);
399
400 return cl;
401}
402
Russell Kinge9d7f402012-05-02 09:30:32 +0100403/**
404 * clk_register_clkdev - register one clock lookup for a struct clk
405 * @clk: struct clk to associate with all clk_lookups
406 * @con_id: connection ID string on device
Kees Cook416dd132016-01-26 01:21:26 +0100407 * @dev_id: string describing device name
Russell Kinge9d7f402012-05-02 09:30:32 +0100408 *
409 * con_id or dev_id may be NULL as a wildcard, just as in the rest of
410 * clkdev.
411 *
412 * To make things easier for mass registration, we detect error clks
413 * from a previous clk_register() call, and return the error code for
414 * those. This is to permit this function to be called immediately
415 * after clk_register().
416 */
417int clk_register_clkdev(struct clk *clk, const char *con_id,
Kees Cook416dd132016-01-26 01:21:26 +0100418 const char *dev_id)
Russell Kinge9d7f402012-05-02 09:30:32 +0100419{
420 struct clk_lookup *cl;
Russell Kinge9d7f402012-05-02 09:30:32 +0100421
422 if (IS_ERR(clk))
423 return PTR_ERR(clk);
424
Kees Cook416dd132016-01-26 01:21:26 +0100425 /*
426 * Since dev_id can be NULL, and NULL is handled specially, we must
427 * pass it as either a NULL format string, or with "%s".
428 */
429 if (dev_id)
430 cl = __clk_register_clkdev(__clk_get_hw(clk), con_id, "%s",
431 dev_id);
432 else
433 cl = __clk_register_clkdev(__clk_get_hw(clk), con_id, NULL);
Russell Kinge9d7f402012-05-02 09:30:32 +0100434
Russell King25689992015-03-02 15:40:29 +0000435 return cl ? 0 : -ENOMEM;
Russell Kinge9d7f402012-05-02 09:30:32 +0100436}
Tomeu Vizosoa251361a2015-01-23 12:03:32 +0100437EXPORT_SYMBOL(clk_register_clkdev);
Stephen Boyde4f1b492016-02-08 14:59:49 -0800438
439/**
440 * clk_hw_register_clkdev - register one clock lookup for a struct clk_hw
441 * @hw: struct clk_hw to associate with all clk_lookups
442 * @con_id: connection ID string on device
443 * @dev_id: format string describing device name
444 *
445 * con_id or dev_id may be NULL as a wildcard, just as in the rest of
446 * clkdev.
Geert Uytterhoevenb8ba5fa2016-11-22 12:33:11 +0100447 *
448 * To make things easier for mass registration, we detect error clk_hws
449 * from a previous clk_hw_register_*() call, and return the error code for
450 * those. This is to permit this function to be called immediately
451 * after clk_hw_register_*().
Stephen Boyde4f1b492016-02-08 14:59:49 -0800452 */
453int clk_hw_register_clkdev(struct clk_hw *hw, const char *con_id,
454 const char *dev_id)
455{
456 struct clk_lookup *cl;
457
Geert Uytterhoevenb8ba5fa2016-11-22 12:33:11 +0100458 if (IS_ERR(hw))
459 return PTR_ERR(hw);
460
Stephen Boyde4f1b492016-02-08 14:59:49 -0800461 /*
462 * Since dev_id can be NULL, and NULL is handled specially, we must
463 * pass it as either a NULL format string, or with "%s".
464 */
465 if (dev_id)
466 cl = __clk_register_clkdev(hw, con_id, "%s", dev_id);
467 else
468 cl = __clk_register_clkdev(hw, con_id, NULL);
469
470 return cl ? 0 : -ENOMEM;
471}
472EXPORT_SYMBOL(clk_hw_register_clkdev);