blob: bdeaffc950ae9812ee96602aa8839f19fb1b0b9f [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 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
Grant Likely766e6a42012-04-09 14:50:06 -050038 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
39 &clkspec);
40 if (rc)
41 return ERR_PTR(rc);
42
Stephen Boyd306c3422015-02-05 15:39:11 -080043 clk = __of_clk_get_from_provider(&clkspec, dev_id, con_id);
Grant Likely766e6a42012-04-09 14:50:06 -050044 of_node_put(clkspec.np);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010045
46 return clk;
47}
48
49struct clk *of_clk_get(struct device_node *np, int index)
50{
Stephen Boyd73e0e492015-02-06 11:42:43 -080051 return __of_clk_get(np, index, np->full_name, NULL);
Grant Likely766e6a42012-04-09 14:50:06 -050052}
53EXPORT_SYMBOL(of_clk_get);
54
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010055static struct clk *__of_clk_get_by_name(struct device_node *np,
56 const char *dev_id,
57 const char *name)
Grant Likely766e6a42012-04-09 14:50:06 -050058{
59 struct clk *clk = ERR_PTR(-ENOENT);
60
61 /* Walk up the tree of devices looking for a clock that matches */
62 while (np) {
63 int index = 0;
64
65 /*
66 * For named clocks, first look up the name in the
67 * "clock-names" property. If it cannot be found, then
68 * index will be an error code, and of_clk_get() will fail.
69 */
70 if (name)
71 index = of_property_match_string(np, "clock-names", name);
Stephen Boyd73e0e492015-02-06 11:42:43 -080072 clk = __of_clk_get(np, index, dev_id, name);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010073 if (!IS_ERR(clk)) {
Grant Likely766e6a42012-04-09 14:50:06 -050074 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -080075 } else if (name && index >= 0) {
Stephen Boydd8e53c32014-06-13 16:36:31 -070076 if (PTR_ERR(clk) != -EPROBE_DEFER)
Rob Herring16673932017-07-18 16:42:52 -050077 pr_err("ERROR: could not get clock %pOF:%s(%i)\n",
78 np, name ? name : "", index);
Grant Likely766e6a42012-04-09 14:50:06 -050079 return clk;
80 }
81
82 /*
83 * No matching clock found on this node. If the parent node
84 * has a "clock-ranges" property, then we can try one of its
85 * clocks.
86 */
87 np = np->parent;
88 if (np && !of_get_property(np, "clock-ranges", NULL))
89 break;
90 }
91
92 return clk;
93}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010094
95/**
96 * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
97 * @np: pointer to clock consumer node
98 * @name: name of consumer's clock input, or NULL for the first clock reference
99 *
100 * This function parses the clocks and clock-names properties,
101 * and uses them to look up the struct clk from the registered list of clock
102 * providers.
103 */
104struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
105{
106 if (!np)
107 return ERR_PTR(-ENOENT);
108
109 return __of_clk_get_by_name(np, np->full_name, name);
110}
Grant Likely766e6a42012-04-09 14:50:06 -0500111EXPORT_SYMBOL(of_clk_get_by_name);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100112
113#else /* defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) */
114
115static struct clk *__of_clk_get_by_name(struct device_node *np,
116 const char *dev_id,
117 const char *name)
118{
119 return ERR_PTR(-ENOENT);
120}
Grant Likely766e6a42012-04-09 14:50:06 -0500121#endif
122
Russell King409dc362009-01-24 10:14:37 +0000123/*
124 * Find the correct struct clk for the device and connection ID.
125 * We do slightly fuzzy matching here:
126 * An entry with a NULL ID is assumed to be a wildcard.
127 * If an entry has a device ID, it must match
128 * If an entry has a connection ID, it must match
129 * Then we take the most specific entry - with the following
Uwe Kleine-König659431f2010-01-18 16:02:48 +0100130 * order of precedence: dev+con > dev only > con only.
Russell King409dc362009-01-24 10:14:37 +0000131 */
Russell Kinge8bf8df2011-04-30 10:14:08 +0100132static struct clk_lookup *clk_find(const char *dev_id, const char *con_id)
Russell King0318e692008-11-09 16:32:46 +0000133{
Russell Kinge8bf8df2011-04-30 10:14:08 +0100134 struct clk_lookup *p, *cl = NULL;
viresh kumar67b50872012-04-19 04:23:25 +0100135 int match, best_found = 0, best_possible = 0;
136
137 if (dev_id)
138 best_possible += 2;
139 if (con_id)
140 best_possible += 1;
Russell King0318e692008-11-09 16:32:46 +0000141
142 list_for_each_entry(p, &clocks, node) {
Russell King0318e692008-11-09 16:32:46 +0000143 match = 0;
Russell King409dc362009-01-24 10:14:37 +0000144 if (p->dev_id) {
145 if (!dev_id || strcmp(p->dev_id, dev_id))
146 continue;
147 match += 2;
148 }
149 if (p->con_id) {
150 if (!con_id || strcmp(p->con_id, con_id))
151 continue;
152 match += 1;
153 }
Russell King0318e692008-11-09 16:32:46 +0000154
viresh kumar67b50872012-04-19 04:23:25 +0100155 if (match > best_found) {
Russell Kinge8bf8df2011-04-30 10:14:08 +0100156 cl = p;
viresh kumar67b50872012-04-19 04:23:25 +0100157 if (match != best_possible)
158 best_found = match;
viresh kumare4bf5be2010-03-09 11:54:30 +0100159 else
160 break;
Russell King0318e692008-11-09 16:32:46 +0000161 }
162 }
Russell Kinge8bf8df2011-04-30 10:14:08 +0100163 return cl;
Russell King0318e692008-11-09 16:32:46 +0000164}
165
Sascha Hauer05fd8e732009-03-07 12:55:49 +0100166struct clk *clk_get_sys(const char *dev_id, const char *con_id)
Russell King0318e692008-11-09 16:32:46 +0000167{
Russell Kinge8bf8df2011-04-30 10:14:08 +0100168 struct clk_lookup *cl;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100169 struct clk *clk = NULL;
Russell King0318e692008-11-09 16:32:46 +0000170
171 mutex_lock(&clocks_mutex);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100172
Russell Kinge8bf8df2011-04-30 10:14:08 +0100173 cl = clk_find(dev_id, con_id);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100174 if (!cl)
175 goto out;
176
Stephen Boyd1df40462018-12-11 08:32:04 -0800177 clk = clk_hw_create_clk(cl->clk_hw, dev_id, con_id);
Stephen Boyd73e0e492015-02-06 11:42:43 -0800178 if (IS_ERR(clk))
Russell Kinge8bf8df2011-04-30 10:14:08 +0100179 cl = NULL;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100180out:
Russell King0318e692008-11-09 16:32:46 +0000181 mutex_unlock(&clocks_mutex);
182
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100183 return cl ? clk : ERR_PTR(-ENOENT);
Russell King0318e692008-11-09 16:32:46 +0000184}
Sascha Hauer05fd8e732009-03-07 12:55:49 +0100185EXPORT_SYMBOL(clk_get_sys);
186
187struct clk *clk_get(struct device *dev, const char *con_id)
188{
189 const char *dev_id = dev ? dev_name(dev) : NULL;
Grant Likely766e6a42012-04-09 14:50:06 -0500190 struct clk *clk;
191
Bartosz Golaszewski53ccb222018-06-28 15:42:20 +0100192 if (dev && dev->of_node) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100193 clk = __of_clk_get_by_name(dev->of_node, dev_id, con_id);
194 if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
Jean-Francois Moinea34cd462013-11-25 19:47:04 +0100195 return clk;
Grant Likely766e6a42012-04-09 14:50:06 -0500196 }
Sascha Hauer05fd8e732009-03-07 12:55:49 +0100197
198 return clk_get_sys(dev_id, con_id);
199}
Russell King0318e692008-11-09 16:32:46 +0000200EXPORT_SYMBOL(clk_get);
201
202void clk_put(struct clk *clk)
203{
204 __clk_put(clk);
205}
206EXPORT_SYMBOL(clk_put);
207
Russell Kingd5622a92015-03-02 15:45:41 +0000208static void __clkdev_add(struct clk_lookup *cl)
Russell King0318e692008-11-09 16:32:46 +0000209{
210 mutex_lock(&clocks_mutex);
211 list_add_tail(&cl->node, &clocks);
212 mutex_unlock(&clocks_mutex);
213}
Russell Kingd5622a92015-03-02 15:45:41 +0000214
215void clkdev_add(struct clk_lookup *cl)
216{
217 if (!cl->clk_hw)
218 cl->clk_hw = __clk_get_hw(cl->clk);
219 __clkdev_add(cl);
220}
Russell King0318e692008-11-09 16:32:46 +0000221EXPORT_SYMBOL(clkdev_add);
222
Russell Kingfba3acd2015-03-10 14:34:00 +0000223void clkdev_add_table(struct clk_lookup *cl, size_t num)
Russell King0a0300d2010-01-12 12:28:00 +0000224{
225 mutex_lock(&clocks_mutex);
226 while (num--) {
Russell Kingd5622a92015-03-02 15:45:41 +0000227 cl->clk_hw = __clk_get_hw(cl->clk);
Russell King0a0300d2010-01-12 12:28:00 +0000228 list_add_tail(&cl->node, &clocks);
229 cl++;
230 }
231 mutex_unlock(&clocks_mutex);
232}
233
Russell King0318e692008-11-09 16:32:46 +0000234#define MAX_DEV_ID 20
235#define MAX_CON_ID 16
236
237struct clk_lookup_alloc {
238 struct clk_lookup cl;
239 char dev_id[MAX_DEV_ID];
240 char con_id[MAX_CON_ID];
241};
242
Fabian Frederickbd721ea2016-08-02 14:03:33 -0700243static struct clk_lookup * __ref
Russell Kingd5622a92015-03-02 15:45:41 +0000244vclkdev_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt,
Russell Kinge9d7f402012-05-02 09:30:32 +0100245 va_list ap)
Russell King0318e692008-11-09 16:32:46 +0000246{
247 struct clk_lookup_alloc *cla;
248
Stephen Boyd0d4e3d002018-01-02 15:47:07 -0800249 cla = kzalloc(sizeof(*cla), GFP_KERNEL);
Russell King0318e692008-11-09 16:32:46 +0000250 if (!cla)
251 return NULL;
252
Russell Kingd5622a92015-03-02 15:45:41 +0000253 cla->cl.clk_hw = hw;
Russell King0318e692008-11-09 16:32:46 +0000254 if (con_id) {
255 strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
256 cla->cl.con_id = cla->con_id;
257 }
258
259 if (dev_fmt) {
Russell King0318e692008-11-09 16:32:46 +0000260 vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
261 cla->cl.dev_id = cla->dev_id;
Russell King0318e692008-11-09 16:32:46 +0000262 }
263
264 return &cla->cl;
265}
Russell Kinge9d7f402012-05-02 09:30:32 +0100266
Russell King25689992015-03-02 15:40:29 +0000267static struct clk_lookup *
268vclkdev_create(struct clk_hw *hw, const char *con_id, const char *dev_fmt,
269 va_list ap)
270{
271 struct clk_lookup *cl;
272
273 cl = vclkdev_alloc(hw, con_id, dev_fmt, ap);
274 if (cl)
275 __clkdev_add(cl);
276
277 return cl;
278}
279
Fabian Frederickbd721ea2016-08-02 14:03:33 -0700280struct clk_lookup * __ref
Russell Kinge9d7f402012-05-02 09:30:32 +0100281clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...)
282{
283 struct clk_lookup *cl;
284 va_list ap;
285
286 va_start(ap, dev_fmt);
Russell Kingd5622a92015-03-02 15:45:41 +0000287 cl = vclkdev_alloc(__clk_get_hw(clk), con_id, dev_fmt, ap);
Russell Kinge9d7f402012-05-02 09:30:32 +0100288 va_end(ap);
289
290 return cl;
291}
Russell King0318e692008-11-09 16:32:46 +0000292EXPORT_SYMBOL(clkdev_alloc);
293
Stephen Boyde4f1b492016-02-08 14:59:49 -0800294struct clk_lookup *
295clkdev_hw_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt, ...)
296{
297 struct clk_lookup *cl;
298 va_list ap;
299
300 va_start(ap, dev_fmt);
301 cl = vclkdev_alloc(hw, con_id, dev_fmt, ap);
302 va_end(ap);
303
304 return cl;
305}
306EXPORT_SYMBOL(clkdev_hw_alloc);
307
Russell King25689992015-03-02 15:40:29 +0000308/**
309 * clkdev_create - allocate and add a clkdev lookup structure
310 * @clk: struct clk to associate with all clk_lookups
311 * @con_id: connection ID string on device
312 * @dev_fmt: format string describing device name
313 *
314 * Returns a clk_lookup structure, which can be later unregistered and
315 * freed.
316 */
317struct clk_lookup *clkdev_create(struct clk *clk, const char *con_id,
318 const char *dev_fmt, ...)
319{
320 struct clk_lookup *cl;
321 va_list ap;
322
323 va_start(ap, dev_fmt);
324 cl = vclkdev_create(__clk_get_hw(clk), con_id, dev_fmt, ap);
325 va_end(ap);
326
327 return cl;
328}
329EXPORT_SYMBOL_GPL(clkdev_create);
330
Stephen Boyde4f1b492016-02-08 14:59:49 -0800331/**
332 * clkdev_hw_create - allocate and add a clkdev lookup structure
333 * @hw: struct clk_hw to associate with all clk_lookups
334 * @con_id: connection ID string on device
335 * @dev_fmt: format string describing device name
336 *
337 * Returns a clk_lookup structure, which can be later unregistered and
338 * freed.
339 */
340struct clk_lookup *clkdev_hw_create(struct clk_hw *hw, const char *con_id,
341 const char *dev_fmt, ...)
342{
343 struct clk_lookup *cl;
344 va_list ap;
345
346 va_start(ap, dev_fmt);
347 cl = vclkdev_create(hw, con_id, dev_fmt, ap);
348 va_end(ap);
349
350 return cl;
351}
352EXPORT_SYMBOL_GPL(clkdev_hw_create);
353
Russell Kingb3d8d7e2015-03-09 10:43:04 +0000354int clk_add_alias(const char *alias, const char *alias_dev_name,
355 const char *con_id, struct device *dev)
Tony Lindgrenc0683032009-06-03 17:43:14 +0100356{
Russell Kingb3d8d7e2015-03-09 10:43:04 +0000357 struct clk *r = clk_get(dev, con_id);
Tony Lindgrenc0683032009-06-03 17:43:14 +0100358 struct clk_lookup *l;
359
360 if (IS_ERR(r))
361 return PTR_ERR(r);
362
Russell King625faa62015-10-20 11:49:44 +0100363 l = clkdev_create(r, alias, alias_dev_name ? "%s" : NULL,
364 alias_dev_name);
Tony Lindgrenc0683032009-06-03 17:43:14 +0100365 clk_put(r);
Russell King25689992015-03-02 15:40:29 +0000366
367 return l ? 0 : -ENODEV;
Tony Lindgrenc0683032009-06-03 17:43:14 +0100368}
369EXPORT_SYMBOL(clk_add_alias);
370
Russell King0318e692008-11-09 16:32:46 +0000371/*
372 * clkdev_drop - remove a clock dynamically allocated
373 */
374void clkdev_drop(struct clk_lookup *cl)
375{
376 mutex_lock(&clocks_mutex);
377 list_del(&cl->node);
378 mutex_unlock(&clocks_mutex);
379 kfree(cl);
380}
381EXPORT_SYMBOL(clkdev_drop);
Russell Kinge9d7f402012-05-02 09:30:32 +0100382
Kees Cook416dd132016-01-26 01:21:26 +0100383static struct clk_lookup *__clk_register_clkdev(struct clk_hw *hw,
384 const char *con_id,
385 const char *dev_id, ...)
386{
387 struct clk_lookup *cl;
388 va_list ap;
389
390 va_start(ap, dev_id);
391 cl = vclkdev_create(hw, con_id, dev_id, ap);
392 va_end(ap);
393
394 return cl;
395}
396
Russell Kinge9d7f402012-05-02 09:30:32 +0100397/**
398 * clk_register_clkdev - register one clock lookup for a struct clk
399 * @clk: struct clk to associate with all clk_lookups
400 * @con_id: connection ID string on device
Kees Cook416dd132016-01-26 01:21:26 +0100401 * @dev_id: string describing device name
Russell Kinge9d7f402012-05-02 09:30:32 +0100402 *
403 * con_id or dev_id may be NULL as a wildcard, just as in the rest of
404 * clkdev.
405 *
406 * To make things easier for mass registration, we detect error clks
407 * from a previous clk_register() call, and return the error code for
408 * those. This is to permit this function to be called immediately
409 * after clk_register().
410 */
411int clk_register_clkdev(struct clk *clk, const char *con_id,
Kees Cook416dd132016-01-26 01:21:26 +0100412 const char *dev_id)
Russell Kinge9d7f402012-05-02 09:30:32 +0100413{
414 struct clk_lookup *cl;
Russell Kinge9d7f402012-05-02 09:30:32 +0100415
416 if (IS_ERR(clk))
417 return PTR_ERR(clk);
418
Kees Cook416dd132016-01-26 01:21:26 +0100419 /*
420 * Since dev_id can be NULL, and NULL is handled specially, we must
421 * pass it as either a NULL format string, or with "%s".
422 */
423 if (dev_id)
424 cl = __clk_register_clkdev(__clk_get_hw(clk), con_id, "%s",
425 dev_id);
426 else
427 cl = __clk_register_clkdev(__clk_get_hw(clk), con_id, NULL);
Russell Kinge9d7f402012-05-02 09:30:32 +0100428
Russell King25689992015-03-02 15:40:29 +0000429 return cl ? 0 : -ENOMEM;
Russell Kinge9d7f402012-05-02 09:30:32 +0100430}
Tomeu Vizosoa251361a2015-01-23 12:03:32 +0100431EXPORT_SYMBOL(clk_register_clkdev);
Stephen Boyde4f1b492016-02-08 14:59:49 -0800432
433/**
434 * clk_hw_register_clkdev - register one clock lookup for a struct clk_hw
435 * @hw: struct clk_hw to associate with all clk_lookups
436 * @con_id: connection ID string on device
437 * @dev_id: format string describing device name
438 *
439 * con_id or dev_id may be NULL as a wildcard, just as in the rest of
440 * clkdev.
Geert Uytterhoeven93880932016-11-22 12:33:11 +0100441 *
442 * To make things easier for mass registration, we detect error clk_hws
443 * from a previous clk_hw_register_*() call, and return the error code for
444 * those. This is to permit this function to be called immediately
445 * after clk_hw_register_*().
Stephen Boyde4f1b492016-02-08 14:59:49 -0800446 */
447int clk_hw_register_clkdev(struct clk_hw *hw, const char *con_id,
448 const char *dev_id)
449{
450 struct clk_lookup *cl;
451
Geert Uytterhoeven93880932016-11-22 12:33:11 +0100452 if (IS_ERR(hw))
453 return PTR_ERR(hw);
454
Stephen Boyde4f1b492016-02-08 14:59:49 -0800455 /*
456 * Since dev_id can be NULL, and NULL is handled specially, we must
457 * pass it as either a NULL format string, or with "%s".
458 */
459 if (dev_id)
460 cl = __clk_register_clkdev(hw, con_id, "%s", dev_id);
461 else
462 cl = __clk_register_clkdev(hw, con_id, NULL);
463
464 return cl ? 0 : -ENOMEM;
465}
466EXPORT_SYMBOL(clk_hw_register_clkdev);