Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Dong Aisheng and committed by
Stephen Boyd
616e45df cfdc0411

+92 -1
+51
drivers/clk/clk-bulk.c
··· 17 17 */ 18 18 19 19 #include <linux/clk.h> 20 + #include <linux/clk-provider.h> 20 21 #include <linux/device.h> 21 22 #include <linux/export.h> 22 23 #include <linux/of.h> 24 + #include <linux/slab.h> 23 25 24 26 static int __must_check of_clk_bulk_get(struct device_node *np, int num_clks, 25 27 struct clk_bulk_data *clks) ··· 49 47 clk_bulk_put(i, clks); 50 48 51 49 return ret; 50 + } 51 + 52 + static int __must_check of_clk_bulk_get_all(struct device_node *np, 53 + struct clk_bulk_data **clks) 54 + { 55 + struct clk_bulk_data *clk_bulk; 56 + int num_clks; 57 + int ret; 58 + 59 + num_clks = of_clk_get_parent_count(np); 60 + if (!num_clks) 61 + return 0; 62 + 63 + clk_bulk = kmalloc_array(num_clks, sizeof(*clk_bulk), GFP_KERNEL); 64 + if (!clk_bulk) 65 + return -ENOMEM; 66 + 67 + ret = of_clk_bulk_get(np, num_clks, clk_bulk); 68 + if (ret) { 69 + kfree(clk_bulk); 70 + return ret; 71 + } 72 + 73 + *clks = clk_bulk; 74 + 75 + return num_clks; 52 76 } 53 77 54 78 void clk_bulk_put(int num_clks, struct clk_bulk_data *clks) ··· 115 87 return ret; 116 88 } 117 89 EXPORT_SYMBOL(clk_bulk_get); 90 + 91 + void clk_bulk_put_all(int num_clks, struct clk_bulk_data *clks) 92 + { 93 + if (IS_ERR_OR_NULL(clks)) 94 + return; 95 + 96 + clk_bulk_put(num_clks, clks); 97 + 98 + kfree(clks); 99 + } 100 + EXPORT_SYMBOL(clk_bulk_put_all); 101 + 102 + int __must_check clk_bulk_get_all(struct device *dev, 103 + struct clk_bulk_data **clks) 104 + { 105 + struct device_node *np = dev_of_node(dev); 106 + 107 + if (!np) 108 + return 0; 109 + 110 + return of_clk_bulk_get_all(np, clks); 111 + } 112 + EXPORT_SYMBOL(clk_bulk_get_all); 118 113 119 114 #ifdef CONFIG_HAVE_CLK_PREPARE 120 115
+41 -1
include/linux/clk.h
··· 312 312 */ 313 313 int __must_check clk_bulk_get(struct device *dev, int num_clks, 314 314 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); 316 335 /** 317 336 * devm_clk_bulk_get - managed get multiple clk consumers 318 337 * @dev: device for clock "consumer" ··· 507 488 void clk_bulk_put(int num_clks, struct clk_bulk_data *clks); 508 489 509 490 /** 491 + * clk_bulk_put_all - "free" all the clock source 492 + * @num_clks: the number of clk_bulk_data 493 + * @clks: the clk_bulk_data table of consumer 494 + * 495 + * Note: drivers must ensure that all clk_bulk_enable calls made on this 496 + * clock source are balanced by clk_bulk_disable calls prior to calling 497 + * this function. 498 + * 499 + * clk_bulk_put_all should not be called from within interrupt context. 500 + */ 501 + void clk_bulk_put_all(int num_clks, struct clk_bulk_data *clks); 502 + 503 + /** 510 504 * devm_clk_put - "free" a managed clock source 511 505 * @dev: device used to acquire the clock 512 506 * @clk: clock source acquired with devm_clk_get() ··· 674 642 return 0; 675 643 } 676 644 645 + static inline int __must_check clk_bulk_get_all(struct device *dev, 646 + struct clk_bulk_data **clks) 647 + { 648 + return 0; 649 + } 650 + 677 651 static inline struct clk *devm_clk_get(struct device *dev, const char *id) 678 652 { 679 653 return NULL; ··· 700 662 static inline void clk_put(struct clk *clk) {} 701 663 702 664 static inline void clk_bulk_put(int num_clks, struct clk_bulk_data *clks) {} 665 + 666 + static inline void clk_bulk_put_all(int num_clks, struct clk_bulk_data *clks) {} 703 667 704 668 static inline void devm_clk_put(struct device *dev, struct clk *clk) {} 705 669