blob: 6e787cc9e5b90ded5c9fd9f613301c6c735133b3 [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
Russell King409dc362009-01-24 10:14:37 +000030/*
31 * Find the correct struct clk for the device and connection ID.
32 * We do slightly fuzzy matching here:
33 * An entry with a NULL ID is assumed to be a wildcard.
34 * If an entry has a device ID, it must match
35 * If an entry has a connection ID, it must match
36 * Then we take the most specific entry - with the following
Uwe Kleine-König659431f2010-01-18 16:02:48 +010037 * order of precedence: dev+con > dev only > con only.
Russell King409dc362009-01-24 10:14:37 +000038 */
Russell Kinge8bf8df2011-04-30 10:14:08 +010039static struct clk_lookup *clk_find(const char *dev_id, const char *con_id)
Russell King0318e692008-11-09 16:32:46 +000040{
Russell Kinge8bf8df2011-04-30 10:14:08 +010041 struct clk_lookup *p, *cl = NULL;
viresh kumar67b50872012-04-19 04:23:25 +010042 int match, best_found = 0, best_possible = 0;
43
44 if (dev_id)
45 best_possible += 2;
46 if (con_id)
47 best_possible += 1;
Russell King0318e692008-11-09 16:32:46 +000048
Stephen Boyd5a7efda2019-04-12 11:31:42 -070049 lockdep_assert_held(&clocks_mutex);
50
Russell King0318e692008-11-09 16:32:46 +000051 list_for_each_entry(p, &clocks, node) {
Russell King0318e692008-11-09 16:32:46 +000052 match = 0;
Russell King409dc362009-01-24 10:14:37 +000053 if (p->dev_id) {
54 if (!dev_id || strcmp(p->dev_id, dev_id))
55 continue;
56 match += 2;
57 }
58 if (p->con_id) {
59 if (!con_id || strcmp(p->con_id, con_id))
60 continue;
61 match += 1;
62 }
Russell King0318e692008-11-09 16:32:46 +000063
viresh kumar67b50872012-04-19 04:23:25 +010064 if (match > best_found) {
Russell Kinge8bf8df2011-04-30 10:14:08 +010065 cl = p;
viresh kumar67b50872012-04-19 04:23:25 +010066 if (match != best_possible)
67 best_found = match;
viresh kumare4bf5be2010-03-09 11:54:30 +010068 else
69 break;
Russell King0318e692008-11-09 16:32:46 +000070 }
71 }
Russell Kinge8bf8df2011-04-30 10:14:08 +010072 return cl;
Russell King0318e692008-11-09 16:32:46 +000073}
74
Stephen Boydefa85042018-12-11 08:34:16 -080075static struct clk *__clk_get_sys(struct device *dev, const char *dev_id,
76 const char *con_id)
Russell King0318e692008-11-09 16:32:46 +000077{
Russell Kinge8bf8df2011-04-30 10:14:08 +010078 struct clk_lookup *cl;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010079 struct clk *clk = NULL;
Russell King0318e692008-11-09 16:32:46 +000080
81 mutex_lock(&clocks_mutex);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010082
Russell Kinge8bf8df2011-04-30 10:14:08 +010083 cl = clk_find(dev_id, con_id);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010084 if (!cl)
85 goto out;
86
Stephen Boydefa85042018-12-11 08:34:16 -080087 clk = clk_hw_create_clk(dev, cl->clk_hw, dev_id, con_id);
Stephen Boyd73e0e492015-02-06 11:42:43 -080088 if (IS_ERR(clk))
Russell Kinge8bf8df2011-04-30 10:14:08 +010089 cl = NULL;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010090out:
Russell King0318e692008-11-09 16:32:46 +000091 mutex_unlock(&clocks_mutex);
92
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010093 return cl ? clk : ERR_PTR(-ENOENT);
Russell King0318e692008-11-09 16:32:46 +000094}
Stephen Boydefa85042018-12-11 08:34:16 -080095
96struct clk *clk_get_sys(const char *dev_id, const char *con_id)
97{
98 return __clk_get_sys(NULL, dev_id, con_id);
99}
Sascha Hauer05fd8e732009-03-07 12:55:49 +0100100EXPORT_SYMBOL(clk_get_sys);
101
102struct clk *clk_get(struct device *dev, const char *con_id)
103{
104 const char *dev_id = dev ? dev_name(dev) : NULL;
Stephen Boyd44722872018-12-19 10:59:55 -0800105 struct clk_hw *hw;
Grant Likely766e6a42012-04-09 14:50:06 -0500106
Bartosz Golaszewski53ccb222018-06-28 15:42:20 +0100107 if (dev && dev->of_node) {
Stephen Boyd44722872018-12-19 10:59:55 -0800108 hw = of_clk_get_hw(dev->of_node, 0, con_id);
109 if (!IS_ERR(hw) || PTR_ERR(hw) == -EPROBE_DEFER)
Stephen Boydefa85042018-12-11 08:34:16 -0800110 return clk_hw_create_clk(dev, hw, dev_id, con_id);
Grant Likely766e6a42012-04-09 14:50:06 -0500111 }
Sascha Hauer05fd8e732009-03-07 12:55:49 +0100112
Stephen Boydefa85042018-12-11 08:34:16 -0800113 return __clk_get_sys(dev, dev_id, con_id);
Sascha Hauer05fd8e732009-03-07 12:55:49 +0100114}
Russell King0318e692008-11-09 16:32:46 +0000115EXPORT_SYMBOL(clk_get);
116
117void clk_put(struct clk *clk)
118{
119 __clk_put(clk);
120}
121EXPORT_SYMBOL(clk_put);
122
Russell Kingd5622a92015-03-02 15:45:41 +0000123static void __clkdev_add(struct clk_lookup *cl)
Russell King0318e692008-11-09 16:32:46 +0000124{
125 mutex_lock(&clocks_mutex);
126 list_add_tail(&cl->node, &clocks);
127 mutex_unlock(&clocks_mutex);
128}
Russell Kingd5622a92015-03-02 15:45:41 +0000129
130void clkdev_add(struct clk_lookup *cl)
131{
132 if (!cl->clk_hw)
133 cl->clk_hw = __clk_get_hw(cl->clk);
134 __clkdev_add(cl);
135}
Russell King0318e692008-11-09 16:32:46 +0000136EXPORT_SYMBOL(clkdev_add);
137
Russell Kingfba3acd2015-03-10 14:34:00 +0000138void clkdev_add_table(struct clk_lookup *cl, size_t num)
Russell King0a0300d2010-01-12 12:28:00 +0000139{
140 mutex_lock(&clocks_mutex);
141 while (num--) {
Russell Kingd5622a92015-03-02 15:45:41 +0000142 cl->clk_hw = __clk_get_hw(cl->clk);
Russell King0a0300d2010-01-12 12:28:00 +0000143 list_add_tail(&cl->node, &clocks);
144 cl++;
145 }
146 mutex_unlock(&clocks_mutex);
147}
148
Russell King0318e692008-11-09 16:32:46 +0000149#define MAX_DEV_ID 20
150#define MAX_CON_ID 16
151
152struct clk_lookup_alloc {
153 struct clk_lookup cl;
154 char dev_id[MAX_DEV_ID];
155 char con_id[MAX_CON_ID];
156};
157
Fabian Frederickbd721ea2016-08-02 14:03:33 -0700158static struct clk_lookup * __ref
Russell Kingd5622a92015-03-02 15:45:41 +0000159vclkdev_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt,
Russell Kinge9d7f402012-05-02 09:30:32 +0100160 va_list ap)
Russell King0318e692008-11-09 16:32:46 +0000161{
162 struct clk_lookup_alloc *cla;
163
Stephen Boyd0d4e3d002018-01-02 15:47:07 -0800164 cla = kzalloc(sizeof(*cla), GFP_KERNEL);
Russell King0318e692008-11-09 16:32:46 +0000165 if (!cla)
166 return NULL;
167
Russell Kingd5622a92015-03-02 15:45:41 +0000168 cla->cl.clk_hw = hw;
Russell King0318e692008-11-09 16:32:46 +0000169 if (con_id) {
170 strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
171 cla->cl.con_id = cla->con_id;
172 }
173
174 if (dev_fmt) {
Russell King0318e692008-11-09 16:32:46 +0000175 vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
176 cla->cl.dev_id = cla->dev_id;
Russell King0318e692008-11-09 16:32:46 +0000177 }
178
179 return &cla->cl;
180}
Russell Kinge9d7f402012-05-02 09:30:32 +0100181
Russell King25689992015-03-02 15:40:29 +0000182static struct clk_lookup *
183vclkdev_create(struct clk_hw *hw, const char *con_id, const char *dev_fmt,
184 va_list ap)
185{
186 struct clk_lookup *cl;
187
188 cl = vclkdev_alloc(hw, con_id, dev_fmt, ap);
189 if (cl)
190 __clkdev_add(cl);
191
192 return cl;
193}
194
Fabian Frederickbd721ea2016-08-02 14:03:33 -0700195struct clk_lookup * __ref
Russell Kinge9d7f402012-05-02 09:30:32 +0100196clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...)
197{
198 struct clk_lookup *cl;
199 va_list ap;
200
201 va_start(ap, dev_fmt);
Russell Kingd5622a92015-03-02 15:45:41 +0000202 cl = vclkdev_alloc(__clk_get_hw(clk), con_id, dev_fmt, ap);
Russell Kinge9d7f402012-05-02 09:30:32 +0100203 va_end(ap);
204
205 return cl;
206}
Russell King0318e692008-11-09 16:32:46 +0000207EXPORT_SYMBOL(clkdev_alloc);
208
Stephen Boyde4f1b492016-02-08 14:59:49 -0800209struct clk_lookup *
210clkdev_hw_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt, ...)
211{
212 struct clk_lookup *cl;
213 va_list ap;
214
215 va_start(ap, dev_fmt);
216 cl = vclkdev_alloc(hw, con_id, dev_fmt, ap);
217 va_end(ap);
218
219 return cl;
220}
221EXPORT_SYMBOL(clkdev_hw_alloc);
222
Russell King25689992015-03-02 15:40:29 +0000223/**
224 * clkdev_create - allocate and add a clkdev lookup structure
225 * @clk: struct clk to associate with all clk_lookups
226 * @con_id: connection ID string on device
227 * @dev_fmt: format string describing device name
228 *
229 * Returns a clk_lookup structure, which can be later unregistered and
230 * freed.
231 */
232struct clk_lookup *clkdev_create(struct clk *clk, const char *con_id,
233 const char *dev_fmt, ...)
234{
235 struct clk_lookup *cl;
236 va_list ap;
237
238 va_start(ap, dev_fmt);
239 cl = vclkdev_create(__clk_get_hw(clk), con_id, dev_fmt, ap);
240 va_end(ap);
241
242 return cl;
243}
244EXPORT_SYMBOL_GPL(clkdev_create);
245
Stephen Boyde4f1b492016-02-08 14:59:49 -0800246/**
247 * clkdev_hw_create - allocate and add a clkdev lookup structure
248 * @hw: struct clk_hw to associate with all clk_lookups
249 * @con_id: connection ID string on device
250 * @dev_fmt: format string describing device name
251 *
252 * Returns a clk_lookup structure, which can be later unregistered and
253 * freed.
254 */
255struct clk_lookup *clkdev_hw_create(struct clk_hw *hw, const char *con_id,
256 const char *dev_fmt, ...)
257{
258 struct clk_lookup *cl;
259 va_list ap;
260
261 va_start(ap, dev_fmt);
262 cl = vclkdev_create(hw, con_id, dev_fmt, ap);
263 va_end(ap);
264
265 return cl;
266}
267EXPORT_SYMBOL_GPL(clkdev_hw_create);
268
Russell Kingb3d8d7e2015-03-09 10:43:04 +0000269int clk_add_alias(const char *alias, const char *alias_dev_name,
270 const char *con_id, struct device *dev)
Tony Lindgrenc0683032009-06-03 17:43:14 +0100271{
Russell Kingb3d8d7e2015-03-09 10:43:04 +0000272 struct clk *r = clk_get(dev, con_id);
Tony Lindgrenc0683032009-06-03 17:43:14 +0100273 struct clk_lookup *l;
274
275 if (IS_ERR(r))
276 return PTR_ERR(r);
277
Russell King625faa62015-10-20 11:49:44 +0100278 l = clkdev_create(r, alias, alias_dev_name ? "%s" : NULL,
279 alias_dev_name);
Tony Lindgrenc0683032009-06-03 17:43:14 +0100280 clk_put(r);
Russell King25689992015-03-02 15:40:29 +0000281
282 return l ? 0 : -ENODEV;
Tony Lindgrenc0683032009-06-03 17:43:14 +0100283}
284EXPORT_SYMBOL(clk_add_alias);
285
Russell King0318e692008-11-09 16:32:46 +0000286/*
287 * clkdev_drop - remove a clock dynamically allocated
288 */
289void clkdev_drop(struct clk_lookup *cl)
290{
291 mutex_lock(&clocks_mutex);
292 list_del(&cl->node);
293 mutex_unlock(&clocks_mutex);
294 kfree(cl);
295}
296EXPORT_SYMBOL(clkdev_drop);
Russell Kinge9d7f402012-05-02 09:30:32 +0100297
Kees Cook416dd132016-01-26 01:21:26 +0100298static struct clk_lookup *__clk_register_clkdev(struct clk_hw *hw,
299 const char *con_id,
300 const char *dev_id, ...)
301{
302 struct clk_lookup *cl;
303 va_list ap;
304
305 va_start(ap, dev_id);
306 cl = vclkdev_create(hw, con_id, dev_id, ap);
307 va_end(ap);
308
309 return cl;
310}
311
Matti Vaittinen3eee6c72018-12-07 13:09:39 +0200312static int do_clk_register_clkdev(struct clk_hw *hw,
313 struct clk_lookup **cl, const char *con_id, const char *dev_id)
314{
315 if (IS_ERR(hw))
316 return PTR_ERR(hw);
317 /*
318 * Since dev_id can be NULL, and NULL is handled specially, we must
319 * pass it as either a NULL format string, or with "%s".
320 */
321 if (dev_id)
322 *cl = __clk_register_clkdev(hw, con_id, "%s", dev_id);
323 else
324 *cl = __clk_register_clkdev(hw, con_id, NULL);
325
326 return *cl ? 0 : -ENOMEM;
327}
328
Russell Kinge9d7f402012-05-02 09:30:32 +0100329/**
330 * clk_register_clkdev - register one clock lookup for a struct clk
331 * @clk: struct clk to associate with all clk_lookups
332 * @con_id: connection ID string on device
Kees Cook416dd132016-01-26 01:21:26 +0100333 * @dev_id: string describing device name
Russell Kinge9d7f402012-05-02 09:30:32 +0100334 *
335 * con_id or dev_id may be NULL as a wildcard, just as in the rest of
336 * clkdev.
337 *
338 * To make things easier for mass registration, we detect error clks
339 * from a previous clk_register() call, and return the error code for
340 * those. This is to permit this function to be called immediately
341 * after clk_register().
342 */
343int clk_register_clkdev(struct clk *clk, const char *con_id,
Kees Cook416dd132016-01-26 01:21:26 +0100344 const char *dev_id)
Russell Kinge9d7f402012-05-02 09:30:32 +0100345{
346 struct clk_lookup *cl;
Russell Kinge9d7f402012-05-02 09:30:32 +0100347
348 if (IS_ERR(clk))
349 return PTR_ERR(clk);
350
Matti Vaittinen3eee6c72018-12-07 13:09:39 +0200351 return do_clk_register_clkdev(__clk_get_hw(clk), &cl, con_id,
352 dev_id);
Russell Kinge9d7f402012-05-02 09:30:32 +0100353}
Tomeu Vizosoa251361a2015-01-23 12:03:32 +0100354EXPORT_SYMBOL(clk_register_clkdev);
Stephen Boyde4f1b492016-02-08 14:59:49 -0800355
356/**
357 * clk_hw_register_clkdev - register one clock lookup for a struct clk_hw
358 * @hw: struct clk_hw to associate with all clk_lookups
359 * @con_id: connection ID string on device
360 * @dev_id: format string describing device name
361 *
362 * con_id or dev_id may be NULL as a wildcard, just as in the rest of
363 * clkdev.
Geert Uytterhoeven93880932016-11-22 12:33:11 +0100364 *
365 * To make things easier for mass registration, we detect error clk_hws
366 * from a previous clk_hw_register_*() call, and return the error code for
367 * those. This is to permit this function to be called immediately
368 * after clk_hw_register_*().
Stephen Boyde4f1b492016-02-08 14:59:49 -0800369 */
370int clk_hw_register_clkdev(struct clk_hw *hw, const char *con_id,
371 const char *dev_id)
372{
373 struct clk_lookup *cl;
374
Matti Vaittinen3eee6c72018-12-07 13:09:39 +0200375 return do_clk_register_clkdev(hw, &cl, con_id, dev_id);
Stephen Boyde4f1b492016-02-08 14:59:49 -0800376}
377EXPORT_SYMBOL(clk_hw_register_clkdev);
Matti Vaittinen3eee6c72018-12-07 13:09:39 +0200378
379static void devm_clkdev_release(struct device *dev, void *res)
380{
381 clkdev_drop(*(struct clk_lookup **)res);
382}
383
384static int devm_clk_match_clkdev(struct device *dev, void *res, void *data)
385{
386 struct clk_lookup **l = res;
387
388 return *l == data;
389}
390
391/**
392 * devm_clk_release_clkdev - Resource managed clkdev lookup release
393 * @dev: device this lookup is bound
394 * @con_id: connection ID string on device
395 * @dev_id: format string describing device name
396 *
397 * Drop the clkdev lookup created with devm_clk_hw_register_clkdev.
398 * Normally this function will not need to be called and the resource
399 * management code will ensure that the resource is freed.
400 */
401void devm_clk_release_clkdev(struct device *dev, const char *con_id,
402 const char *dev_id)
403{
404 struct clk_lookup *cl;
405 int rval;
406
Stephen Boyd5a7efda2019-04-12 11:31:42 -0700407 mutex_lock(&clocks_mutex);
Matti Vaittinen3eee6c72018-12-07 13:09:39 +0200408 cl = clk_find(dev_id, con_id);
Stephen Boyd5a7efda2019-04-12 11:31:42 -0700409 mutex_unlock(&clocks_mutex);
410
Matti Vaittinen3eee6c72018-12-07 13:09:39 +0200411 WARN_ON(!cl);
412 rval = devres_release(dev, devm_clkdev_release,
413 devm_clk_match_clkdev, cl);
414 WARN_ON(rval);
415}
416EXPORT_SYMBOL(devm_clk_release_clkdev);
417
418/**
419 * devm_clk_hw_register_clkdev - managed clk lookup registration for clk_hw
420 * @dev: device this lookup is bound
421 * @hw: struct clk_hw to associate with all clk_lookups
422 * @con_id: connection ID string on device
423 * @dev_id: format string describing device name
424 *
425 * con_id or dev_id may be NULL as a wildcard, just as in the rest of
426 * clkdev.
427 *
428 * To make things easier for mass registration, we detect error clk_hws
429 * from a previous clk_hw_register_*() call, and return the error code for
430 * those. This is to permit this function to be called immediately
431 * after clk_hw_register_*().
432 */
433int devm_clk_hw_register_clkdev(struct device *dev, struct clk_hw *hw,
434 const char *con_id, const char *dev_id)
435{
436 int rval = -ENOMEM;
437 struct clk_lookup **cl;
438
439 cl = devres_alloc(devm_clkdev_release, sizeof(*cl), GFP_KERNEL);
440 if (cl) {
441 rval = do_clk_register_clkdev(hw, cl, con_id, dev_id);
442 if (!rval)
443 devres_add(dev, cl);
444 else
445 devres_free(cl);
446 }
447 return rval;
448}
449EXPORT_SYMBOL(devm_clk_hw_register_clkdev);