Skip to content

Commit 616e45d

Browse files
aisheng86bebarino
authored andcommitted
clk: add new APIs to operate on all available clocks
This patch introduces of_clk_bulk_get_all and clk_bulk_x_all APIs to users who just want to handle all available clocks from device tree without need to know the detailed clock information likes clock numbers and names. This is useful in writing some generic drivers to handle clock part. Cc: Stephen Boyd <sboyd@codeaurora.org> Cc: Masahiro Yamada <yamada.masahiro@socionext.com> Tested-by: Thor Thayer <thor.thayer@linux.intel.com> Signed-off-by: Dong Aisheng <aisheng.dong@nxp.com> Signed-off-by: Stephen Boyd <sboyd@kernel.org>
1 parent cfdc041 commit 616e45d

2 files changed

Lines changed: 92 additions & 1 deletion

File tree

‎drivers/clk/clk-bulk.c‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
*/
1818

1919
#include <linux/clk.h>
20+
#include <linux/clk-provider.h>
2021
#include <linux/device.h>
2122
#include <linux/export.h>
2223
#include <linux/of.h>
24+
#include <linux/slab.h>
2325

2426
static int __must_check of_clk_bulk_get(struct device_node *np, int num_clks,
2527
struct clk_bulk_data *clks)
@@ -49,6 +51,32 @@ static int __must_check of_clk_bulk_get(struct device_node *np, int num_clks,
4951
return ret;
5052
}
5153

54+
static int __must_check of_clk_bulk_get_all(struct device_node *np,
55+
struct clk_bulk_data **clks)
56+
{
57+
struct clk_bulk_data *clk_bulk;
58+
int num_clks;
59+
int ret;
60+
61+
num_clks = of_clk_get_parent_count(np);
62+
if (!num_clks)
63+
return 0;
64+
65+
clk_bulk = kmalloc_array(num_clks, sizeof(*clk_bulk), GFP_KERNEL);
66+
if (!clk_bulk)
67+
return -ENOMEM;
68+
69+
ret = of_clk_bulk_get(np, num_clks, clk_bulk);
70+
if (ret) {
71+
kfree(clk_bulk);
72+
return ret;
73+
}
74+
75+
*clks = clk_bulk;
76+
77+
return num_clks;
78+
}
79+
5280
void clk_bulk_put(int num_clks, struct clk_bulk_data *clks)
5381
{
5482
while (--num_clks >= 0) {
@@ -88,6 +116,29 @@ int __must_check clk_bulk_get(struct device *dev, int num_clks,
88116
}
89117
EXPORT_SYMBOL(clk_bulk_get);
90118

119+
void clk_bulk_put_all(int num_clks, struct clk_bulk_data *clks)
120+
{
121+
if (IS_ERR_OR_NULL(clks))
122+
return;
123+
124+
clk_bulk_put(num_clks, clks);
125+
126+
kfree(clks);
127+
}
128+
EXPORT_SYMBOL(clk_bulk_put_all);
129+
130+
int __must_check clk_bulk_get_all(struct device *dev,
131+
struct clk_bulk_data **clks)
132+
{
133+
struct device_node *np = dev_of_node(dev);
134+
135+
if (!np)
136+
return 0;
137+
138+
return of_clk_bulk_get_all(np, clks);
139+
}
140+
EXPORT_SYMBOL(clk_bulk_get_all);
141+
91142
#ifdef CONFIG_HAVE_CLK_PREPARE
92143

93144
/**

‎include/linux/clk.h‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,26 @@ struct clk *clk_get(struct device *dev, const char *id);
312312
*/
313313
int __must_check clk_bulk_get(struct device *dev, int num_clks,
314314
struct clk_bulk_data *clks);
315-
315+
/**
316+
* clk_bulk_get_all - lookup and obtain all available references to clock
317+
* producer.
318+
* @dev: device for clock "consumer"
319+
* @clks: pointer to the clk_bulk_data table of consumer
320+
*
321+
* This helper function allows drivers to get all clk consumers in one
322+
* operation. If any of the clk cannot be acquired then any clks
323+
* that were obtained will be freed before returning to the caller.
324+
*
325+
* Returns a positive value for the number of clocks obtained while the
326+
* clock references are stored in the clk_bulk_data table in @clks field.
327+
* Returns 0 if there're none and a negative value if something failed.
328+
*
329+
* Drivers must assume that the clock source is not enabled.
330+
*
331+
* clk_bulk_get should not be called from within interrupt context.
332+
*/
333+
int __must_check clk_bulk_get_all(struct device *dev,
334+
struct clk_bulk_data **clks);
316335
/**
317336
* devm_clk_bulk_get - managed get multiple clk consumers
318337
* @dev: device for clock "consumer"
@@ -487,6 +506,19 @@ void clk_put(struct clk *clk);
487506
*/
488507
void clk_bulk_put(int num_clks, struct clk_bulk_data *clks);
489508

509+
/**
510+
* clk_bulk_put_all - "free" all the clock source
511+
* @num_clks: the number of clk_bulk_data
512+
* @clks: the clk_bulk_data table of consumer
513+
*
514+
* Note: drivers must ensure that all clk_bulk_enable calls made on this
515+
* clock source are balanced by clk_bulk_disable calls prior to calling
516+
* this function.
517+
*
518+
* clk_bulk_put_all should not be called from within interrupt context.
519+
*/
520+
void clk_bulk_put_all(int num_clks, struct clk_bulk_data *clks);
521+
490522
/**
491523
* devm_clk_put - "free" a managed clock source
492524
* @dev: device used to acquire the clock
@@ -642,6 +674,12 @@ static inline int __must_check clk_bulk_get(struct device *dev, int num_clks,
642674
return 0;
643675
}
644676

677+
static inline int __must_check clk_bulk_get_all(struct device *dev,
678+
struct clk_bulk_data **clks)
679+
{
680+
return 0;
681+
}
682+
645683
static inline struct clk *devm_clk_get(struct device *dev, const char *id)
646684
{
647685
return NULL;
@@ -663,6 +701,8 @@ static inline void clk_put(struct clk *clk) {}
663701

664702
static inline void clk_bulk_put(int num_clks, struct clk_bulk_data *clks) {}
665703

704+
static inline void clk_bulk_put_all(int num_clks, struct clk_bulk_data *clks) {}
705+
666706
static inline void devm_clk_put(struct device *dev, struct clk *clk) {}
667707

668708

0 commit comments

Comments
 (0)