xref: /openbmc/linux/drivers/clk/clkdev.c (revision 6c71a0574249f5e5a45fe055ab5f837023d5eeca)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
26d803ba7SJean-Christop PLAGNIOL-VILLARD /*
36d803ba7SJean-Christop PLAGNIOL-VILLARD  * drivers/clk/clkdev.c
46d803ba7SJean-Christop PLAGNIOL-VILLARD  *
56d803ba7SJean-Christop PLAGNIOL-VILLARD  *  Copyright (C) 2008 Russell King.
66d803ba7SJean-Christop PLAGNIOL-VILLARD  *
76d803ba7SJean-Christop PLAGNIOL-VILLARD  * Helper for the clk API to assist looking up a struct clk.
86d803ba7SJean-Christop PLAGNIOL-VILLARD  */
96d803ba7SJean-Christop PLAGNIOL-VILLARD #include <linux/module.h>
106d803ba7SJean-Christop PLAGNIOL-VILLARD #include <linux/kernel.h>
116d803ba7SJean-Christop PLAGNIOL-VILLARD #include <linux/device.h>
126d803ba7SJean-Christop PLAGNIOL-VILLARD #include <linux/list.h>
136d803ba7SJean-Christop PLAGNIOL-VILLARD #include <linux/errno.h>
146d803ba7SJean-Christop PLAGNIOL-VILLARD #include <linux/err.h>
156d803ba7SJean-Christop PLAGNIOL-VILLARD #include <linux/string.h>
166d803ba7SJean-Christop PLAGNIOL-VILLARD #include <linux/mutex.h>
176d803ba7SJean-Christop PLAGNIOL-VILLARD #include <linux/clk.h>
186d803ba7SJean-Christop PLAGNIOL-VILLARD #include <linux/clkdev.h>
19035a61c3STomeu Vizoso #include <linux/clk-provider.h>
20766e6a4eSGrant Likely #include <linux/of.h>
216d803ba7SJean-Christop PLAGNIOL-VILLARD 
223a3d2b05SSylwester Nawrocki #include "clk.h"
233a3d2b05SSylwester Nawrocki 
246d803ba7SJean-Christop PLAGNIOL-VILLARD static LIST_HEAD(clocks);
256d803ba7SJean-Christop PLAGNIOL-VILLARD static DEFINE_MUTEX(clocks_mutex);
266d803ba7SJean-Christop PLAGNIOL-VILLARD 
276d803ba7SJean-Christop PLAGNIOL-VILLARD /*
286d803ba7SJean-Christop PLAGNIOL-VILLARD  * Find the correct struct clk for the device and connection ID.
296d803ba7SJean-Christop PLAGNIOL-VILLARD  * We do slightly fuzzy matching here:
306d803ba7SJean-Christop PLAGNIOL-VILLARD  *  An entry with a NULL ID is assumed to be a wildcard.
316d803ba7SJean-Christop PLAGNIOL-VILLARD  *  If an entry has a device ID, it must match
326d803ba7SJean-Christop PLAGNIOL-VILLARD  *  If an entry has a connection ID, it must match
336d803ba7SJean-Christop PLAGNIOL-VILLARD  * Then we take the most specific entry - with the following
346d803ba7SJean-Christop PLAGNIOL-VILLARD  * order of precedence: dev+con > dev only > con only.
356d803ba7SJean-Christop PLAGNIOL-VILLARD  */
clk_find(const char * dev_id,const char * con_id)36e8bf8df9SRussell King static struct clk_lookup *clk_find(const char *dev_id, const char *con_id)
376d803ba7SJean-Christop PLAGNIOL-VILLARD {
38e8bf8df9SRussell King 	struct clk_lookup *p, *cl = NULL;
3967b50871Sviresh kumar 	int match, best_found = 0, best_possible = 0;
4067b50871Sviresh kumar 
4167b50871Sviresh kumar 	if (dev_id)
4267b50871Sviresh kumar 		best_possible += 2;
4367b50871Sviresh kumar 	if (con_id)
4467b50871Sviresh kumar 		best_possible += 1;
456d803ba7SJean-Christop PLAGNIOL-VILLARD 
465a7efdacSStephen Boyd 	lockdep_assert_held(&clocks_mutex);
475a7efdacSStephen Boyd 
486d803ba7SJean-Christop PLAGNIOL-VILLARD 	list_for_each_entry(p, &clocks, node) {
496d803ba7SJean-Christop PLAGNIOL-VILLARD 		match = 0;
506d803ba7SJean-Christop PLAGNIOL-VILLARD 		if (p->dev_id) {
516d803ba7SJean-Christop PLAGNIOL-VILLARD 			if (!dev_id || strcmp(p->dev_id, dev_id))
526d803ba7SJean-Christop PLAGNIOL-VILLARD 				continue;
536d803ba7SJean-Christop PLAGNIOL-VILLARD 			match += 2;
546d803ba7SJean-Christop PLAGNIOL-VILLARD 		}
556d803ba7SJean-Christop PLAGNIOL-VILLARD 		if (p->con_id) {
566d803ba7SJean-Christop PLAGNIOL-VILLARD 			if (!con_id || strcmp(p->con_id, con_id))
576d803ba7SJean-Christop PLAGNIOL-VILLARD 				continue;
586d803ba7SJean-Christop PLAGNIOL-VILLARD 			match += 1;
596d803ba7SJean-Christop PLAGNIOL-VILLARD 		}
606d803ba7SJean-Christop PLAGNIOL-VILLARD 
6167b50871Sviresh kumar 		if (match > best_found) {
62e8bf8df9SRussell King 			cl = p;
6367b50871Sviresh kumar 			if (match != best_possible)
6467b50871Sviresh kumar 				best_found = match;
656d803ba7SJean-Christop PLAGNIOL-VILLARD 			else
666d803ba7SJean-Christop PLAGNIOL-VILLARD 				break;
676d803ba7SJean-Christop PLAGNIOL-VILLARD 		}
686d803ba7SJean-Christop PLAGNIOL-VILLARD 	}
69e8bf8df9SRussell King 	return cl;
706d803ba7SJean-Christop PLAGNIOL-VILLARD }
716d803ba7SJean-Christop PLAGNIOL-VILLARD 
clk_find_hw(const char * dev_id,const char * con_id)72dde4eff4SStephen Boyd struct clk_hw *clk_find_hw(const char *dev_id, const char *con_id)
73d1011cbaSStephen Boyd {
74d1011cbaSStephen Boyd 	struct clk_lookup *cl;
75d1011cbaSStephen Boyd 	struct clk_hw *hw = ERR_PTR(-ENOENT);
76d1011cbaSStephen Boyd 
77d1011cbaSStephen Boyd 	mutex_lock(&clocks_mutex);
78d1011cbaSStephen Boyd 	cl = clk_find(dev_id, con_id);
79d1011cbaSStephen Boyd 	if (cl)
80d1011cbaSStephen Boyd 		hw = cl->clk_hw;
81d1011cbaSStephen Boyd 	mutex_unlock(&clocks_mutex);
82d1011cbaSStephen Boyd 
83d1011cbaSStephen Boyd 	return hw;
84d1011cbaSStephen Boyd }
85d1011cbaSStephen Boyd 
__clk_get_sys(struct device * dev,const char * dev_id,const char * con_id)86efa85048SStephen Boyd static struct clk *__clk_get_sys(struct device *dev, const char *dev_id,
87efa85048SStephen Boyd 				 const char *con_id)
886d803ba7SJean-Christop PLAGNIOL-VILLARD {
89d1011cbaSStephen Boyd 	struct clk_hw *hw = clk_find_hw(dev_id, con_id);
906d803ba7SJean-Christop PLAGNIOL-VILLARD 
91d1011cbaSStephen Boyd 	return clk_hw_create_clk(dev, hw, dev_id, con_id);
926d803ba7SJean-Christop PLAGNIOL-VILLARD }
93efa85048SStephen Boyd 
clk_get_sys(const char * dev_id,const char * con_id)94efa85048SStephen Boyd struct clk *clk_get_sys(const char *dev_id, const char *con_id)
95efa85048SStephen Boyd {
96efa85048SStephen Boyd 	return __clk_get_sys(NULL, dev_id, con_id);
97efa85048SStephen Boyd }
986d803ba7SJean-Christop PLAGNIOL-VILLARD EXPORT_SYMBOL(clk_get_sys);
996d803ba7SJean-Christop PLAGNIOL-VILLARD 
clk_get(struct device * dev,const char * con_id)1006d803ba7SJean-Christop PLAGNIOL-VILLARD struct clk *clk_get(struct device *dev, const char *con_id)
1016d803ba7SJean-Christop PLAGNIOL-VILLARD {
1026d803ba7SJean-Christop PLAGNIOL-VILLARD 	const char *dev_id = dev ? dev_name(dev) : NULL;
1034472287aSStephen Boyd 	struct clk_hw *hw;
104766e6a4eSGrant Likely 
10553ccb22bSBartosz Golaszewski 	if (dev && dev->of_node) {
1064472287aSStephen Boyd 		hw = of_clk_get_hw(dev->of_node, 0, con_id);
1074472287aSStephen Boyd 		if (!IS_ERR(hw) || PTR_ERR(hw) == -EPROBE_DEFER)
108efa85048SStephen Boyd 			return clk_hw_create_clk(dev, hw, dev_id, con_id);
109766e6a4eSGrant Likely 	}
1106d803ba7SJean-Christop PLAGNIOL-VILLARD 
111efa85048SStephen Boyd 	return __clk_get_sys(dev, dev_id, con_id);
1126d803ba7SJean-Christop PLAGNIOL-VILLARD }
1136d803ba7SJean-Christop PLAGNIOL-VILLARD EXPORT_SYMBOL(clk_get);
1146d803ba7SJean-Christop PLAGNIOL-VILLARD 
clk_put(struct clk * clk)1156d803ba7SJean-Christop PLAGNIOL-VILLARD void clk_put(struct clk *clk)
1166d803ba7SJean-Christop PLAGNIOL-VILLARD {
1176d803ba7SJean-Christop PLAGNIOL-VILLARD 	__clk_put(clk);
1186d803ba7SJean-Christop PLAGNIOL-VILLARD }
1196d803ba7SJean-Christop PLAGNIOL-VILLARD EXPORT_SYMBOL(clk_put);
1206d803ba7SJean-Christop PLAGNIOL-VILLARD 
__clkdev_add(struct clk_lookup * cl)121d5622a9cSRussell King static void __clkdev_add(struct clk_lookup *cl)
1226d803ba7SJean-Christop PLAGNIOL-VILLARD {
1236d803ba7SJean-Christop PLAGNIOL-VILLARD 	mutex_lock(&clocks_mutex);
1246d803ba7SJean-Christop PLAGNIOL-VILLARD 	list_add_tail(&cl->node, &clocks);
1256d803ba7SJean-Christop PLAGNIOL-VILLARD 	mutex_unlock(&clocks_mutex);
1266d803ba7SJean-Christop PLAGNIOL-VILLARD }
127d5622a9cSRussell King 
clkdev_add(struct clk_lookup * cl)128d5622a9cSRussell King void clkdev_add(struct clk_lookup *cl)
129d5622a9cSRussell King {
130d5622a9cSRussell King 	if (!cl->clk_hw)
131d5622a9cSRussell King 		cl->clk_hw = __clk_get_hw(cl->clk);
132d5622a9cSRussell King 	__clkdev_add(cl);
133d5622a9cSRussell King }
1346d803ba7SJean-Christop PLAGNIOL-VILLARD EXPORT_SYMBOL(clkdev_add);
1356d803ba7SJean-Christop PLAGNIOL-VILLARD 
clkdev_add_table(struct clk_lookup * cl,size_t num)136fba3acd9SRussell King void clkdev_add_table(struct clk_lookup *cl, size_t num)
1376d803ba7SJean-Christop PLAGNIOL-VILLARD {
1386d803ba7SJean-Christop PLAGNIOL-VILLARD 	mutex_lock(&clocks_mutex);
1396d803ba7SJean-Christop PLAGNIOL-VILLARD 	while (num--) {
140d5622a9cSRussell King 		cl->clk_hw = __clk_get_hw(cl->clk);
1416d803ba7SJean-Christop PLAGNIOL-VILLARD 		list_add_tail(&cl->node, &clocks);
1426d803ba7SJean-Christop PLAGNIOL-VILLARD 		cl++;
1436d803ba7SJean-Christop PLAGNIOL-VILLARD 	}
1446d803ba7SJean-Christop PLAGNIOL-VILLARD 	mutex_unlock(&clocks_mutex);
1456d803ba7SJean-Christop PLAGNIOL-VILLARD }
1466d803ba7SJean-Christop PLAGNIOL-VILLARD 
147*6d0881a0SMichael J. Ruhl #define MAX_DEV_ID	24
1486d803ba7SJean-Christop PLAGNIOL-VILLARD #define MAX_CON_ID	16
1496d803ba7SJean-Christop PLAGNIOL-VILLARD 
1506d803ba7SJean-Christop PLAGNIOL-VILLARD struct clk_lookup_alloc {
1516d803ba7SJean-Christop PLAGNIOL-VILLARD 	struct clk_lookup cl;
1526d803ba7SJean-Christop PLAGNIOL-VILLARD 	char	dev_id[MAX_DEV_ID];
1536d803ba7SJean-Christop PLAGNIOL-VILLARD 	char	con_id[MAX_CON_ID];
1546d803ba7SJean-Christop PLAGNIOL-VILLARD };
1556d803ba7SJean-Christop PLAGNIOL-VILLARD 
156bd721ea7SFabian Frederick static struct clk_lookup * __ref
vclkdev_alloc(struct clk_hw * hw,const char * con_id,const char * dev_fmt,va_list ap)157d5622a9cSRussell King vclkdev_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt,
158e9d7f406SRussell King 	va_list ap)
1596d803ba7SJean-Christop PLAGNIOL-VILLARD {
1606d803ba7SJean-Christop PLAGNIOL-VILLARD 	struct clk_lookup_alloc *cla;
1616d803ba7SJean-Christop PLAGNIOL-VILLARD 
1620d4e3d00SStephen Boyd 	cla = kzalloc(sizeof(*cla), GFP_KERNEL);
1636d803ba7SJean-Christop PLAGNIOL-VILLARD 	if (!cla)
1646d803ba7SJean-Christop PLAGNIOL-VILLARD 		return NULL;
1656d803ba7SJean-Christop PLAGNIOL-VILLARD 
166d5622a9cSRussell King 	cla->cl.clk_hw = hw;
1676d803ba7SJean-Christop PLAGNIOL-VILLARD 	if (con_id) {
168c19edff6SWolfram Sang 		strscpy(cla->con_id, con_id, sizeof(cla->con_id));
1696d803ba7SJean-Christop PLAGNIOL-VILLARD 		cla->cl.con_id = cla->con_id;
1706d803ba7SJean-Christop PLAGNIOL-VILLARD 	}
1716d803ba7SJean-Christop PLAGNIOL-VILLARD 
1726d803ba7SJean-Christop PLAGNIOL-VILLARD 	if (dev_fmt) {
1736d803ba7SJean-Christop PLAGNIOL-VILLARD 		vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
1746d803ba7SJean-Christop PLAGNIOL-VILLARD 		cla->cl.dev_id = cla->dev_id;
1756d803ba7SJean-Christop PLAGNIOL-VILLARD 	}
1766d803ba7SJean-Christop PLAGNIOL-VILLARD 
1776d803ba7SJean-Christop PLAGNIOL-VILLARD 	return &cla->cl;
1786d803ba7SJean-Christop PLAGNIOL-VILLARD }
179e9d7f406SRussell King 
18025689998SRussell King static struct clk_lookup *
vclkdev_create(struct clk_hw * hw,const char * con_id,const char * dev_fmt,va_list ap)18125689998SRussell King vclkdev_create(struct clk_hw *hw, const char *con_id, const char *dev_fmt,
18225689998SRussell King 	va_list ap)
18325689998SRussell King {
18425689998SRussell King 	struct clk_lookup *cl;
18525689998SRussell King 
18625689998SRussell King 	cl = vclkdev_alloc(hw, con_id, dev_fmt, ap);
18725689998SRussell King 	if (cl)
18825689998SRussell King 		__clkdev_add(cl);
18925689998SRussell King 
19025689998SRussell King 	return cl;
19125689998SRussell King }
19225689998SRussell King 
19325689998SRussell King /**
19425689998SRussell King  * clkdev_create - allocate and add a clkdev lookup structure
19525689998SRussell King  * @clk: struct clk to associate with all clk_lookups
19625689998SRussell King  * @con_id: connection ID string on device
19725689998SRussell King  * @dev_fmt: format string describing device name
19825689998SRussell King  *
19925689998SRussell King  * Returns a clk_lookup structure, which can be later unregistered and
20025689998SRussell King  * freed.
20125689998SRussell King  */
clkdev_create(struct clk * clk,const char * con_id,const char * dev_fmt,...)20225689998SRussell King struct clk_lookup *clkdev_create(struct clk *clk, const char *con_id,
20325689998SRussell King 	const char *dev_fmt, ...)
20425689998SRussell King {
20525689998SRussell King 	struct clk_lookup *cl;
20625689998SRussell King 	va_list ap;
20725689998SRussell King 
20825689998SRussell King 	va_start(ap, dev_fmt);
20925689998SRussell King 	cl = vclkdev_create(__clk_get_hw(clk), con_id, dev_fmt, ap);
21025689998SRussell King 	va_end(ap);
21125689998SRussell King 
21225689998SRussell King 	return cl;
21325689998SRussell King }
21425689998SRussell King EXPORT_SYMBOL_GPL(clkdev_create);
21525689998SRussell King 
216e4f1b49bSStephen Boyd /**
217e4f1b49bSStephen Boyd  * clkdev_hw_create - allocate and add a clkdev lookup structure
218e4f1b49bSStephen Boyd  * @hw: struct clk_hw to associate with all clk_lookups
219e4f1b49bSStephen Boyd  * @con_id: connection ID string on device
220e4f1b49bSStephen Boyd  * @dev_fmt: format string describing device name
221e4f1b49bSStephen Boyd  *
222e4f1b49bSStephen Boyd  * Returns a clk_lookup structure, which can be later unregistered and
223e4f1b49bSStephen Boyd  * freed.
224e4f1b49bSStephen Boyd  */
clkdev_hw_create(struct clk_hw * hw,const char * con_id,const char * dev_fmt,...)225e4f1b49bSStephen Boyd struct clk_lookup *clkdev_hw_create(struct clk_hw *hw, const char *con_id,
226e4f1b49bSStephen Boyd 	const char *dev_fmt, ...)
227e4f1b49bSStephen Boyd {
228e4f1b49bSStephen Boyd 	struct clk_lookup *cl;
229e4f1b49bSStephen Boyd 	va_list ap;
230e4f1b49bSStephen Boyd 
231e4f1b49bSStephen Boyd 	va_start(ap, dev_fmt);
232e4f1b49bSStephen Boyd 	cl = vclkdev_create(hw, con_id, dev_fmt, ap);
233e4f1b49bSStephen Boyd 	va_end(ap);
234e4f1b49bSStephen Boyd 
235e4f1b49bSStephen Boyd 	return cl;
236e4f1b49bSStephen Boyd }
237e4f1b49bSStephen Boyd EXPORT_SYMBOL_GPL(clkdev_hw_create);
238e4f1b49bSStephen Boyd 
clk_add_alias(const char * alias,const char * alias_dev_name,const char * con_id,struct device * dev)239b3d8d7e8SRussell King int clk_add_alias(const char *alias, const char *alias_dev_name,
240b3d8d7e8SRussell King 	const char *con_id, struct device *dev)
2416d803ba7SJean-Christop PLAGNIOL-VILLARD {
242b3d8d7e8SRussell King 	struct clk *r = clk_get(dev, con_id);
2436d803ba7SJean-Christop PLAGNIOL-VILLARD 	struct clk_lookup *l;
2446d803ba7SJean-Christop PLAGNIOL-VILLARD 
2456d803ba7SJean-Christop PLAGNIOL-VILLARD 	if (IS_ERR(r))
2466d803ba7SJean-Christop PLAGNIOL-VILLARD 		return PTR_ERR(r);
2476d803ba7SJean-Christop PLAGNIOL-VILLARD 
248625faa6aSRussell King 	l = clkdev_create(r, alias, alias_dev_name ? "%s" : NULL,
249625faa6aSRussell King 			  alias_dev_name);
2506d803ba7SJean-Christop PLAGNIOL-VILLARD 	clk_put(r);
25125689998SRussell King 
25225689998SRussell King 	return l ? 0 : -ENODEV;
2536d803ba7SJean-Christop PLAGNIOL-VILLARD }
2546d803ba7SJean-Christop PLAGNIOL-VILLARD EXPORT_SYMBOL(clk_add_alias);
2556d803ba7SJean-Christop PLAGNIOL-VILLARD 
2566d803ba7SJean-Christop PLAGNIOL-VILLARD /*
2576d803ba7SJean-Christop PLAGNIOL-VILLARD  * clkdev_drop - remove a clock dynamically allocated
2586d803ba7SJean-Christop PLAGNIOL-VILLARD  */
clkdev_drop(struct clk_lookup * cl)2596d803ba7SJean-Christop PLAGNIOL-VILLARD void clkdev_drop(struct clk_lookup *cl)
2606d803ba7SJean-Christop PLAGNIOL-VILLARD {
2616d803ba7SJean-Christop PLAGNIOL-VILLARD 	mutex_lock(&clocks_mutex);
2626d803ba7SJean-Christop PLAGNIOL-VILLARD 	list_del(&cl->node);
2636d803ba7SJean-Christop PLAGNIOL-VILLARD 	mutex_unlock(&clocks_mutex);
2646d803ba7SJean-Christop PLAGNIOL-VILLARD 	kfree(cl);
2656d803ba7SJean-Christop PLAGNIOL-VILLARD }
2666d803ba7SJean-Christop PLAGNIOL-VILLARD EXPORT_SYMBOL(clkdev_drop);
267e9d7f406SRussell King 
__clk_register_clkdev(struct clk_hw * hw,const char * con_id,const char * dev_id,...)268416dd13aSKees Cook static struct clk_lookup *__clk_register_clkdev(struct clk_hw *hw,
269416dd13aSKees Cook 						const char *con_id,
270416dd13aSKees Cook 						const char *dev_id, ...)
271416dd13aSKees Cook {
272416dd13aSKees Cook 	struct clk_lookup *cl;
273416dd13aSKees Cook 	va_list ap;
274416dd13aSKees Cook 
275416dd13aSKees Cook 	va_start(ap, dev_id);
276416dd13aSKees Cook 	cl = vclkdev_create(hw, con_id, dev_id, ap);
277416dd13aSKees Cook 	va_end(ap);
278416dd13aSKees Cook 
279416dd13aSKees Cook 	return cl;
280416dd13aSKees Cook }
281416dd13aSKees Cook 
do_clk_register_clkdev(struct clk_hw * hw,struct clk_lookup ** cl,const char * con_id,const char * dev_id)2823eee6c7dSMatti Vaittinen static int do_clk_register_clkdev(struct clk_hw *hw,
2833eee6c7dSMatti Vaittinen 	struct clk_lookup **cl, const char *con_id, const char *dev_id)
2843eee6c7dSMatti Vaittinen {
2853eee6c7dSMatti Vaittinen 	if (IS_ERR(hw))
2863eee6c7dSMatti Vaittinen 		return PTR_ERR(hw);
2873eee6c7dSMatti Vaittinen 	/*
2883eee6c7dSMatti Vaittinen 	 * Since dev_id can be NULL, and NULL is handled specially, we must
2893eee6c7dSMatti Vaittinen 	 * pass it as either a NULL format string, or with "%s".
2903eee6c7dSMatti Vaittinen 	 */
2913eee6c7dSMatti Vaittinen 	if (dev_id)
2923eee6c7dSMatti Vaittinen 		*cl = __clk_register_clkdev(hw, con_id, "%s", dev_id);
2933eee6c7dSMatti Vaittinen 	else
2943eee6c7dSMatti Vaittinen 		*cl = __clk_register_clkdev(hw, con_id, NULL);
2953eee6c7dSMatti Vaittinen 
2963eee6c7dSMatti Vaittinen 	return *cl ? 0 : -ENOMEM;
2973eee6c7dSMatti Vaittinen }
2983eee6c7dSMatti Vaittinen 
299e9d7f406SRussell King /**
300e9d7f406SRussell King  * clk_register_clkdev - register one clock lookup for a struct clk
301e9d7f406SRussell King  * @clk: struct clk to associate with all clk_lookups
302e9d7f406SRussell King  * @con_id: connection ID string on device
303416dd13aSKees Cook  * @dev_id: string describing device name
304e9d7f406SRussell King  *
305e9d7f406SRussell King  * con_id or dev_id may be NULL as a wildcard, just as in the rest of
306e9d7f406SRussell King  * clkdev.
307e9d7f406SRussell King  *
308e9d7f406SRussell King  * To make things easier for mass registration, we detect error clks
309e9d7f406SRussell King  * from a previous clk_register() call, and return the error code for
310e9d7f406SRussell King  * those.  This is to permit this function to be called immediately
311e9d7f406SRussell King  * after clk_register().
312e9d7f406SRussell King  */
clk_register_clkdev(struct clk * clk,const char * con_id,const char * dev_id)313e9d7f406SRussell King int clk_register_clkdev(struct clk *clk, const char *con_id,
314416dd13aSKees Cook 	const char *dev_id)
315e9d7f406SRussell King {
316e9d7f406SRussell King 	struct clk_lookup *cl;
317e9d7f406SRussell King 
318e9d7f406SRussell King 	if (IS_ERR(clk))
319e9d7f406SRussell King 		return PTR_ERR(clk);
320e9d7f406SRussell King 
3213eee6c7dSMatti Vaittinen 	return do_clk_register_clkdev(__clk_get_hw(clk), &cl, con_id,
322416dd13aSKees Cook 					      dev_id);
323e9d7f406SRussell King }
324a251361aSTomeu Vizoso EXPORT_SYMBOL(clk_register_clkdev);
325e4f1b49bSStephen Boyd 
326e4f1b49bSStephen Boyd /**
327e4f1b49bSStephen Boyd  * clk_hw_register_clkdev - register one clock lookup for a struct clk_hw
328e4f1b49bSStephen Boyd  * @hw: struct clk_hw to associate with all clk_lookups
329e4f1b49bSStephen Boyd  * @con_id: connection ID string on device
330e4f1b49bSStephen Boyd  * @dev_id: format string describing device name
331e4f1b49bSStephen Boyd  *
332e4f1b49bSStephen Boyd  * con_id or dev_id may be NULL as a wildcard, just as in the rest of
333e4f1b49bSStephen Boyd  * clkdev.
3349388093dSGeert Uytterhoeven  *
3359388093dSGeert Uytterhoeven  * To make things easier for mass registration, we detect error clk_hws
3369388093dSGeert Uytterhoeven  * from a previous clk_hw_register_*() call, and return the error code for
3379388093dSGeert Uytterhoeven  * those.  This is to permit this function to be called immediately
3389388093dSGeert Uytterhoeven  * after clk_hw_register_*().
339e4f1b49bSStephen Boyd  */
clk_hw_register_clkdev(struct clk_hw * hw,const char * con_id,const char * dev_id)340e4f1b49bSStephen Boyd int clk_hw_register_clkdev(struct clk_hw *hw, const char *con_id,
341e4f1b49bSStephen Boyd 	const char *dev_id)
342e4f1b49bSStephen Boyd {
343e4f1b49bSStephen Boyd 	struct clk_lookup *cl;
344e4f1b49bSStephen Boyd 
3453eee6c7dSMatti Vaittinen 	return do_clk_register_clkdev(hw, &cl, con_id, dev_id);
346e4f1b49bSStephen Boyd }
347e4f1b49bSStephen Boyd EXPORT_SYMBOL(clk_hw_register_clkdev);
3483eee6c7dSMatti Vaittinen 
devm_clkdev_release(void * res)349d61876a2SAndy Shevchenko static void devm_clkdev_release(void *res)
3503eee6c7dSMatti Vaittinen {
351d61876a2SAndy Shevchenko 	clkdev_drop(res);
3523eee6c7dSMatti Vaittinen }
3533eee6c7dSMatti Vaittinen 
3543eee6c7dSMatti Vaittinen /**
3553eee6c7dSMatti Vaittinen  * devm_clk_hw_register_clkdev - managed clk lookup registration for clk_hw
3563eee6c7dSMatti Vaittinen  * @dev: device this lookup is bound
3573eee6c7dSMatti Vaittinen  * @hw: struct clk_hw to associate with all clk_lookups
3583eee6c7dSMatti Vaittinen  * @con_id: connection ID string on device
3593eee6c7dSMatti Vaittinen  * @dev_id: format string describing device name
3603eee6c7dSMatti Vaittinen  *
3613eee6c7dSMatti Vaittinen  * con_id or dev_id may be NULL as a wildcard, just as in the rest of
3623eee6c7dSMatti Vaittinen  * clkdev.
3633eee6c7dSMatti Vaittinen  *
3643eee6c7dSMatti Vaittinen  * To make things easier for mass registration, we detect error clk_hws
3653eee6c7dSMatti Vaittinen  * from a previous clk_hw_register_*() call, and return the error code for
3663eee6c7dSMatti Vaittinen  * those.  This is to permit this function to be called immediately
3673eee6c7dSMatti Vaittinen  * after clk_hw_register_*().
3683eee6c7dSMatti Vaittinen  */
devm_clk_hw_register_clkdev(struct device * dev,struct clk_hw * hw,const char * con_id,const char * dev_id)3693eee6c7dSMatti Vaittinen int devm_clk_hw_register_clkdev(struct device *dev, struct clk_hw *hw,
3703eee6c7dSMatti Vaittinen 				const char *con_id, const char *dev_id)
3713eee6c7dSMatti Vaittinen {
372d61876a2SAndy Shevchenko 	struct clk_lookup *cl;
373d61876a2SAndy Shevchenko 	int rval;
3743eee6c7dSMatti Vaittinen 
375d61876a2SAndy Shevchenko 	rval = do_clk_register_clkdev(hw, &cl, con_id, dev_id);
376d61876a2SAndy Shevchenko 	if (rval)
3773eee6c7dSMatti Vaittinen 		return rval;
378d61876a2SAndy Shevchenko 
379d61876a2SAndy Shevchenko 	return devm_add_action_or_reset(dev, devm_clkdev_release, cl);
3803eee6c7dSMatti Vaittinen }
3813eee6c7dSMatti Vaittinen EXPORT_SYMBOL(devm_clk_hw_register_clkdev);
382