blob: 5ebb2119c0b9b6cba1ed4cc14389cb327a3e7e81 [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)
Stephen Boyd44722872018-12-19 10:59:55 -080031static struct clk_hw *of_clk_get_hw(struct device_node *np,
32 int index, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -050033{
Stephen Boyd44722872018-12-19 10:59:55 -080034 int ret;
35 struct clk_hw *hw;
Grant Likely766e6a42012-04-09 14:50:06 -050036 struct of_phandle_args clkspec;
Grant Likely766e6a42012-04-09 14:50:06 -050037
Stephen Boyd44722872018-12-19 10:59:55 -080038 ret = of_parse_clkspec(np, index, con_id, &clkspec);
39 if (ret)
40 return ERR_PTR(ret);
Grant Likely766e6a42012-04-09 14:50:06 -050041
Stephen Boyd44722872018-12-19 10:59:55 -080042 hw = of_clk_get_hw_from_clkspec(&clkspec);
Grant Likely766e6a42012-04-09 14:50:06 -050043 of_node_put(clkspec.np);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010044
Stephen Boyd44722872018-12-19 10:59:55 -080045 return hw;
46}
47
48static struct clk *__of_clk_get(struct device_node *np,
49 int index, const char *dev_id,
50 const char *con_id)
51{
52 struct clk_hw *hw = of_clk_get_hw(np, index, con_id);
53
54 return clk_hw_create_clk(hw, dev_id, con_id);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010055}
56
57struct clk *of_clk_get(struct device_node *np, int index)
58{
Stephen Boyd73e0e492015-02-06 11:42:43 -080059 return __of_clk_get(np, index, np->full_name, NULL);
Grant Likely766e6a42012-04-09 14:50:06 -050060}
61EXPORT_SYMBOL(of_clk_get);
62
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010063/**
64 * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
65 * @np: pointer to clock consumer node
66 * @name: name of consumer's clock input, or NULL for the first clock reference
67 *
68 * This function parses the clocks and clock-names properties,
69 * and uses them to look up the struct clk from the registered list of clock
70 * providers.
71 */
72struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
73{
74 if (!np)
75 return ERR_PTR(-ENOENT);
76
Stephen Boyd44722872018-12-19 10:59:55 -080077 return __of_clk_get(np, -1, np->full_name, name);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010078}
Grant Likely766e6a42012-04-09 14:50:06 -050079EXPORT_SYMBOL(of_clk_get_by_name);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010080
81#else /* defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) */
82
Stephen Boyd44722872018-12-19 10:59:55 -080083static struct clk_hw *of_clk_get_hw(struct device_node *np,
84 int index, const char *con_id)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010085{
86 return ERR_PTR(-ENOENT);
87}
Grant Likely766e6a42012-04-09 14:50:06 -050088#endif
89
Russell King409dc362009-01-24 10:14:37 +000090/*
91 * Find the correct struct clk for the device and connection ID.
92 * We do slightly fuzzy matching here:
93 * An entry with a NULL ID is assumed to be a wildcard.
94 * If an entry has a device ID, it must match
95 * If an entry has a connection ID, it must match
96 * Then we take the most specific entry - with the following
Uwe Kleine-König659431f2010-01-18 16:02:48 +010097 * order of precedence: dev+con > dev only > con only.
Russell King409dc362009-01-24 10:14:37 +000098 */
Russell Kinge8bf8df2011-04-30 10:14:08 +010099static struct clk_lookup *clk_find(const char *dev_id, const char *con_id)
Russell King0318e692008-11-09 16:32:46 +0000100{
Russell Kinge8bf8df2011-04-30 10:14:08 +0100101 struct clk_lookup *p, *cl = NULL;
viresh kumar67b50872012-04-19 04:23:25 +0100102 int match, best_found = 0, best_possible = 0;
103
104 if (dev_id)
105 best_possible += 2;
106 if (con_id)
107 best_possible += 1;
Russell King0318e692008-11-09 16:32:46 +0000108
109 list_for_each_entry(p, &clocks, node) {
Russell King0318e692008-11-09 16:32:46 +0000110 match = 0;
Russell King409dc362009-01-24 10:14:37 +0000111 if (p->dev_id) {
112 if (!dev_id || strcmp(p->dev_id, dev_id))
113 continue;
114 match += 2;
115 }
116 if (p->con_id) {
117 if (!con_id || strcmp(p->con_id, con_id))
118 continue;
119 match += 1;
120 }
Russell King0318e692008-11-09 16:32:46 +0000121
viresh kumar67b50872012-04-19 04:23:25 +0100122 if (match > best_found) {
Russell Kinge8bf8df2011-04-30 10:14:08 +0100123 cl = p;
viresh kumar67b50872012-04-19 04:23:25 +0100124 if (match != best_possible)
125 best_found = match;
viresh kumare4bf5be2010-03-09 11:54:30 +0100126 else
127 break;
Russell King0318e692008-11-09 16:32:46 +0000128 }
129 }
Russell Kinge8bf8df2011-04-30 10:14:08 +0100130 return cl;
Russell King0318e692008-11-09 16:32:46 +0000131}
132
Sascha Hauer05fd8e732009-03-07 12:55:49 +0100133struct clk *clk_get_sys(const char *dev_id, const char *con_id)
Russell King0318e692008-11-09 16:32:46 +0000134{
Russell Kinge8bf8df2011-04-30 10:14:08 +0100135 struct clk_lookup *cl;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100136 struct clk *clk = NULL;
Russell King0318e692008-11-09 16:32:46 +0000137
138 mutex_lock(&clocks_mutex);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100139
Russell Kinge8bf8df2011-04-30 10:14:08 +0100140 cl = clk_find(dev_id, con_id);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100141 if (!cl)
142 goto out;
143
Stephen Boyd1df40462018-12-11 08:32:04 -0800144 clk = clk_hw_create_clk(cl->clk_hw, dev_id, con_id);
Stephen Boyd73e0e492015-02-06 11:42:43 -0800145 if (IS_ERR(clk))
Russell Kinge8bf8df2011-04-30 10:14:08 +0100146 cl = NULL;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100147out:
Russell King0318e692008-11-09 16:32:46 +0000148 mutex_unlock(&clocks_mutex);
149
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100150 return cl ? clk : ERR_PTR(-ENOENT);
Russell King0318e692008-11-09 16:32:46 +0000151}
Sascha Hauer05fd8e732009-03-07 12:55:49 +0100152EXPORT_SYMBOL(clk_get_sys);
153
154struct clk *clk_get(struct device *dev, const char *con_id)
155{
156 const char *dev_id = dev ? dev_name(dev) : NULL;
Stephen Boyd44722872018-12-19 10:59:55 -0800157 struct clk_hw *hw;
Grant Likely766e6a42012-04-09 14:50:06 -0500158
Bartosz Golaszewski53ccb222018-06-28 15:42:20 +0100159 if (dev && dev->of_node) {
Stephen Boyd44722872018-12-19 10:59:55 -0800160 hw = of_clk_get_hw(dev->of_node, 0, con_id);
161 if (!IS_ERR(hw) || PTR_ERR(hw) == -EPROBE_DEFER)
162 return clk_hw_create_clk(hw, dev_id, con_id);
Grant Likely766e6a42012-04-09 14:50:06 -0500163 }
Sascha Hauer05fd8e732009-03-07 12:55:49 +0100164
165 return clk_get_sys(dev_id, con_id);
166}
Russell King0318e692008-11-09 16:32:46 +0000167EXPORT_SYMBOL(clk_get);
168
169void clk_put(struct clk *clk)
170{
171 __clk_put(clk);
172}
173EXPORT_SYMBOL(clk_put);
174
Russell Kingd5622a92015-03-02 15:45:41 +0000175static void __clkdev_add(struct clk_lookup *cl)
Russell King0318e692008-11-09 16:32:46 +0000176{
177 mutex_lock(&clocks_mutex);
178 list_add_tail(&cl->node, &clocks);
179 mutex_unlock(&clocks_mutex);
180}
Russell Kingd5622a92015-03-02 15:45:41 +0000181
182void clkdev_add(struct clk_lookup *cl)
183{
184 if (!cl->clk_hw)
185 cl->clk_hw = __clk_get_hw(cl->clk);
186 __clkdev_add(cl);
187}
Russell King0318e692008-11-09 16:32:46 +0000188EXPORT_SYMBOL(clkdev_add);
189
Russell Kingfba3acd2015-03-10 14:34:00 +0000190void clkdev_add_table(struct clk_lookup *cl, size_t num)
Russell King0a0300d2010-01-12 12:28:00 +0000191{
192 mutex_lock(&clocks_mutex);
193 while (num--) {
Russell Kingd5622a92015-03-02 15:45:41 +0000194 cl->clk_hw = __clk_get_hw(cl->clk);
Russell King0a0300d2010-01-12 12:28:00 +0000195 list_add_tail(&cl->node, &clocks);
196 cl++;
197 }
198 mutex_unlock(&clocks_mutex);
199}
200
Russell King0318e692008-11-09 16:32:46 +0000201#define MAX_DEV_ID 20
202#define MAX_CON_ID 16
203
204struct clk_lookup_alloc {
205 struct clk_lookup cl;
206 char dev_id[MAX_DEV_ID];
207 char con_id[MAX_CON_ID];
208};
209
Fabian Frederickbd721ea2016-08-02 14:03:33 -0700210static struct clk_lookup * __ref
Russell Kingd5622a92015-03-02 15:45:41 +0000211vclkdev_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt,
Russell Kinge9d7f402012-05-02 09:30:32 +0100212 va_list ap)
Russell King0318e692008-11-09 16:32:46 +0000213{
214 struct clk_lookup_alloc *cla;
215
Stephen Boyd0d4e3d002018-01-02 15:47:07 -0800216 cla = kzalloc(sizeof(*cla), GFP_KERNEL);
Russell King0318e692008-11-09 16:32:46 +0000217 if (!cla)
218 return NULL;
219
Russell Kingd5622a92015-03-02 15:45:41 +0000220 cla->cl.clk_hw = hw;
Russell King0318e692008-11-09 16:32:46 +0000221 if (con_id) {
222 strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
223 cla->cl.con_id = cla->con_id;
224 }
225
226 if (dev_fmt) {
Russell King0318e692008-11-09 16:32:46 +0000227 vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
228 cla->cl.dev_id = cla->dev_id;
Russell King0318e692008-11-09 16:32:46 +0000229 }
230
231 return &cla->cl;
232}
Russell Kinge9d7f402012-05-02 09:30:32 +0100233
Russell King25689992015-03-02 15:40:29 +0000234static struct clk_lookup *
235vclkdev_create(struct clk_hw *hw, const char *con_id, const char *dev_fmt,
236 va_list ap)
237{
238 struct clk_lookup *cl;
239
240 cl = vclkdev_alloc(hw, con_id, dev_fmt, ap);
241 if (cl)
242 __clkdev_add(cl);
243
244 return cl;
245}
246
Fabian Frederickbd721ea2016-08-02 14:03:33 -0700247struct clk_lookup * __ref
Russell Kinge9d7f402012-05-02 09:30:32 +0100248clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...)
249{
250 struct clk_lookup *cl;
251 va_list ap;
252
253 va_start(ap, dev_fmt);
Russell Kingd5622a92015-03-02 15:45:41 +0000254 cl = vclkdev_alloc(__clk_get_hw(clk), con_id, dev_fmt, ap);
Russell Kinge9d7f402012-05-02 09:30:32 +0100255 va_end(ap);
256
257 return cl;
258}
Russell King0318e692008-11-09 16:32:46 +0000259EXPORT_SYMBOL(clkdev_alloc);
260
Stephen Boyde4f1b492016-02-08 14:59:49 -0800261struct clk_lookup *
262clkdev_hw_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt, ...)
263{
264 struct clk_lookup *cl;
265 va_list ap;
266
267 va_start(ap, dev_fmt);
268 cl = vclkdev_alloc(hw, con_id, dev_fmt, ap);
269 va_end(ap);
270
271 return cl;
272}
273EXPORT_SYMBOL(clkdev_hw_alloc);
274
Russell King25689992015-03-02 15:40:29 +0000275/**
276 * clkdev_create - allocate and add a clkdev lookup structure
277 * @clk: struct clk to associate with all clk_lookups
278 * @con_id: connection ID string on device
279 * @dev_fmt: format string describing device name
280 *
281 * Returns a clk_lookup structure, which can be later unregistered and
282 * freed.
283 */
284struct clk_lookup *clkdev_create(struct clk *clk, const char *con_id,
285 const char *dev_fmt, ...)
286{
287 struct clk_lookup *cl;
288 va_list ap;
289
290 va_start(ap, dev_fmt);
291 cl = vclkdev_create(__clk_get_hw(clk), con_id, dev_fmt, ap);
292 va_end(ap);
293
294 return cl;
295}
296EXPORT_SYMBOL_GPL(clkdev_create);
297
Stephen Boyde4f1b492016-02-08 14:59:49 -0800298/**
299 * clkdev_hw_create - allocate and add a clkdev lookup structure
300 * @hw: struct clk_hw to associate with all clk_lookups
301 * @con_id: connection ID string on device
302 * @dev_fmt: format string describing device name
303 *
304 * Returns a clk_lookup structure, which can be later unregistered and
305 * freed.
306 */
307struct clk_lookup *clkdev_hw_create(struct clk_hw *hw, const char *con_id,
308 const char *dev_fmt, ...)
309{
310 struct clk_lookup *cl;
311 va_list ap;
312
313 va_start(ap, dev_fmt);
314 cl = vclkdev_create(hw, con_id, dev_fmt, ap);
315 va_end(ap);
316
317 return cl;
318}
319EXPORT_SYMBOL_GPL(clkdev_hw_create);
320
Russell Kingb3d8d7e2015-03-09 10:43:04 +0000321int clk_add_alias(const char *alias, const char *alias_dev_name,
322 const char *con_id, struct device *dev)
Tony Lindgrenc0683032009-06-03 17:43:14 +0100323{
Russell Kingb3d8d7e2015-03-09 10:43:04 +0000324 struct clk *r = clk_get(dev, con_id);
Tony Lindgrenc0683032009-06-03 17:43:14 +0100325 struct clk_lookup *l;
326
327 if (IS_ERR(r))
328 return PTR_ERR(r);
329
Russell King625faa62015-10-20 11:49:44 +0100330 l = clkdev_create(r, alias, alias_dev_name ? "%s" : NULL,
331 alias_dev_name);
Tony Lindgrenc0683032009-06-03 17:43:14 +0100332 clk_put(r);
Russell King25689992015-03-02 15:40:29 +0000333
334 return l ? 0 : -ENODEV;
Tony Lindgrenc0683032009-06-03 17:43:14 +0100335}
336EXPORT_SYMBOL(clk_add_alias);
337
Russell King0318e692008-11-09 16:32:46 +0000338/*
339 * clkdev_drop - remove a clock dynamically allocated
340 */
341void clkdev_drop(struct clk_lookup *cl)
342{
343 mutex_lock(&clocks_mutex);
344 list_del(&cl->node);
345 mutex_unlock(&clocks_mutex);
346 kfree(cl);
347}
348EXPORT_SYMBOL(clkdev_drop);
Russell Kinge9d7f402012-05-02 09:30:32 +0100349
Kees Cook416dd132016-01-26 01:21:26 +0100350static struct clk_lookup *__clk_register_clkdev(struct clk_hw *hw,
351 const char *con_id,
352 const char *dev_id, ...)
353{
354 struct clk_lookup *cl;
355 va_list ap;
356
357 va_start(ap, dev_id);
358 cl = vclkdev_create(hw, con_id, dev_id, ap);
359 va_end(ap);
360
361 return cl;
362}
363
Russell Kinge9d7f402012-05-02 09:30:32 +0100364/**
365 * clk_register_clkdev - register one clock lookup for a struct clk
366 * @clk: struct clk to associate with all clk_lookups
367 * @con_id: connection ID string on device
Kees Cook416dd132016-01-26 01:21:26 +0100368 * @dev_id: string describing device name
Russell Kinge9d7f402012-05-02 09:30:32 +0100369 *
370 * con_id or dev_id may be NULL as a wildcard, just as in the rest of
371 * clkdev.
372 *
373 * To make things easier for mass registration, we detect error clks
374 * from a previous clk_register() call, and return the error code for
375 * those. This is to permit this function to be called immediately
376 * after clk_register().
377 */
378int clk_register_clkdev(struct clk *clk, const char *con_id,
Kees Cook416dd132016-01-26 01:21:26 +0100379 const char *dev_id)
Russell Kinge9d7f402012-05-02 09:30:32 +0100380{
381 struct clk_lookup *cl;
Russell Kinge9d7f402012-05-02 09:30:32 +0100382
383 if (IS_ERR(clk))
384 return PTR_ERR(clk);
385
Kees Cook416dd132016-01-26 01:21:26 +0100386 /*
387 * Since dev_id can be NULL, and NULL is handled specially, we must
388 * pass it as either a NULL format string, or with "%s".
389 */
390 if (dev_id)
391 cl = __clk_register_clkdev(__clk_get_hw(clk), con_id, "%s",
392 dev_id);
393 else
394 cl = __clk_register_clkdev(__clk_get_hw(clk), con_id, NULL);
Russell Kinge9d7f402012-05-02 09:30:32 +0100395
Russell King25689992015-03-02 15:40:29 +0000396 return cl ? 0 : -ENOMEM;
Russell Kinge9d7f402012-05-02 09:30:32 +0100397}
Tomeu Vizosoa251361a2015-01-23 12:03:32 +0100398EXPORT_SYMBOL(clk_register_clkdev);
Stephen Boyde4f1b492016-02-08 14:59:49 -0800399
400/**
401 * clk_hw_register_clkdev - register one clock lookup for a struct clk_hw
402 * @hw: struct clk_hw to associate with all clk_lookups
403 * @con_id: connection ID string on device
404 * @dev_id: format string describing device name
405 *
406 * con_id or dev_id may be NULL as a wildcard, just as in the rest of
407 * clkdev.
Geert Uytterhoeven93880932016-11-22 12:33:11 +0100408 *
409 * To make things easier for mass registration, we detect error clk_hws
410 * from a previous clk_hw_register_*() call, and return the error code for
411 * those. This is to permit this function to be called immediately
412 * after clk_hw_register_*().
Stephen Boyde4f1b492016-02-08 14:59:49 -0800413 */
414int clk_hw_register_clkdev(struct clk_hw *hw, const char *con_id,
415 const char *dev_id)
416{
417 struct clk_lookup *cl;
418
Geert Uytterhoeven93880932016-11-22 12:33:11 +0100419 if (IS_ERR(hw))
420 return PTR_ERR(hw);
421
Stephen Boyde4f1b492016-02-08 14:59:49 -0800422 /*
423 * Since dev_id can be NULL, and NULL is handled specially, we must
424 * pass it as either a NULL format string, or with "%s".
425 */
426 if (dev_id)
427 cl = __clk_register_clkdev(hw, con_id, "%s", dev_id);
428 else
429 cl = __clk_register_clkdev(hw, con_id, NULL);
430
431 return cl ? 0 : -ENOMEM;
432}
433EXPORT_SYMBOL(clk_hw_register_clkdev);