1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */ 2f8ce2547SRussell King /* 3f8ce2547SRussell King * linux/include/linux/clk.h 4f8ce2547SRussell King * 5f8ce2547SRussell King * Copyright (C) 2004 ARM Limited. 6f8ce2547SRussell King * Written by Deep Blue Solutions Limited. 7b2476490SMike Turquette * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org> 8f8ce2547SRussell King */ 9686f8c5dSTodd Poynor #ifndef __LINUX_CLK_H 10686f8c5dSTodd Poynor #define __LINUX_CLK_H 11f8ce2547SRussell King 129f1612d3SShawn Guo #include <linux/err.h> 1340d3e0f4SRussell King #include <linux/kernel.h> 14b2476490SMike Turquette #include <linux/notifier.h> 1540d3e0f4SRussell King 16f8ce2547SRussell King struct device; 17f8ce2547SRussell King struct clk; 1871a2f115SKuninori Morimoto struct device_node; 1971a2f115SKuninori Morimoto struct of_phandle_args; 20f8ce2547SRussell King 21b2476490SMike Turquette /** 22b2476490SMike Turquette * DOC: clk notifier callback types 23b2476490SMike Turquette * 24b2476490SMike Turquette * PRE_RATE_CHANGE - called immediately before the clk rate is changed, 25b2476490SMike Turquette * to indicate that the rate change will proceed. Drivers must 26b2476490SMike Turquette * immediately terminate any operations that will be affected by the 27fb72a059SSoren Brinkmann * rate change. Callbacks may either return NOTIFY_DONE, NOTIFY_OK, 28fb72a059SSoren Brinkmann * NOTIFY_STOP or NOTIFY_BAD. 29b2476490SMike Turquette * 30b2476490SMike Turquette * ABORT_RATE_CHANGE: called if the rate change failed for some reason 31b2476490SMike Turquette * after PRE_RATE_CHANGE. In this case, all registered notifiers on 32b2476490SMike Turquette * the clk will be called with ABORT_RATE_CHANGE. Callbacks must 33fb72a059SSoren Brinkmann * always return NOTIFY_DONE or NOTIFY_OK. 34b2476490SMike Turquette * 35b2476490SMike Turquette * POST_RATE_CHANGE - called after the clk rate change has successfully 36fb72a059SSoren Brinkmann * completed. Callbacks must always return NOTIFY_DONE or NOTIFY_OK. 37b2476490SMike Turquette * 38b2476490SMike Turquette */ 39b2476490SMike Turquette #define PRE_RATE_CHANGE BIT(0) 40b2476490SMike Turquette #define POST_RATE_CHANGE BIT(1) 41b2476490SMike Turquette #define ABORT_RATE_CHANGE BIT(2) 42b2476490SMike Turquette 43b2476490SMike Turquette /** 44b2476490SMike Turquette * struct clk_notifier - associate a clk with a notifier 45b2476490SMike Turquette * @clk: struct clk * to associate the notifier with 46b2476490SMike Turquette * @notifier_head: a blocking_notifier_head for this clk 47b2476490SMike Turquette * @node: linked list pointers 48b2476490SMike Turquette * 49b2476490SMike Turquette * A list of struct clk_notifier is maintained by the notifier code. 50b2476490SMike Turquette * An entry is created whenever code registers the first notifier on a 51b2476490SMike Turquette * particular @clk. Future notifiers on that @clk are added to the 52b2476490SMike Turquette * @notifier_head. 53b2476490SMike Turquette */ 54b2476490SMike Turquette struct clk_notifier { 55b2476490SMike Turquette struct clk *clk; 56b2476490SMike Turquette struct srcu_notifier_head notifier_head; 57b2476490SMike Turquette struct list_head node; 58b2476490SMike Turquette }; 59b2476490SMike Turquette 60b2476490SMike Turquette /** 61b2476490SMike Turquette * struct clk_notifier_data - rate data to pass to the notifier callback 62b2476490SMike Turquette * @clk: struct clk * being changed 63b2476490SMike Turquette * @old_rate: previous rate of this clk 64b2476490SMike Turquette * @new_rate: new rate of this clk 65b2476490SMike Turquette * 66b2476490SMike Turquette * For a pre-notifier, old_rate is the clk's rate before this rate 67b2476490SMike Turquette * change, and new_rate is what the rate will be in the future. For a 68b2476490SMike Turquette * post-notifier, old_rate and new_rate are both set to the clk's 69b2476490SMike Turquette * current rate (this was done to optimize the implementation). 70b2476490SMike Turquette */ 71b2476490SMike Turquette struct clk_notifier_data { 72b2476490SMike Turquette struct clk *clk; 73b2476490SMike Turquette unsigned long old_rate; 74b2476490SMike Turquette unsigned long new_rate; 75b2476490SMike Turquette }; 76b2476490SMike Turquette 77266e4e9dSDong Aisheng /** 78266e4e9dSDong Aisheng * struct clk_bulk_data - Data used for bulk clk operations. 79266e4e9dSDong Aisheng * 80266e4e9dSDong Aisheng * @id: clock consumer ID 81266e4e9dSDong Aisheng * @clk: struct clk * to store the associated clock 82266e4e9dSDong Aisheng * 83266e4e9dSDong Aisheng * The CLK APIs provide a series of clk_bulk_() API calls as 84266e4e9dSDong Aisheng * a convenience to consumers which require multiple clks. This 85266e4e9dSDong Aisheng * structure is used to manage data for these calls. 86266e4e9dSDong Aisheng */ 87266e4e9dSDong Aisheng struct clk_bulk_data { 88266e4e9dSDong Aisheng const char *id; 89266e4e9dSDong Aisheng struct clk *clk; 90266e4e9dSDong Aisheng }; 91266e4e9dSDong Aisheng 92e81b87d2SKrzysztof Kozlowski #ifdef CONFIG_COMMON_CLK 93e81b87d2SKrzysztof Kozlowski 9486bcfa2eSMike Turquette /** 95b90f3726SRandy Dunlap * clk_notifier_register - register a clock rate-change notifier callback 9686bcfa2eSMike Turquette * @clk: clock whose rate we are interested in 9786bcfa2eSMike Turquette * @nb: notifier block with callback function pointer 9886bcfa2eSMike Turquette * 9986bcfa2eSMike Turquette * ProTip: debugging across notifier chains can be frustrating. Make sure that 10086bcfa2eSMike Turquette * your notifier callback function prints a nice big warning in case of 10186bcfa2eSMike Turquette * failure. 10286bcfa2eSMike Turquette */ 103b2476490SMike Turquette int clk_notifier_register(struct clk *clk, struct notifier_block *nb); 104b2476490SMike Turquette 10586bcfa2eSMike Turquette /** 106b90f3726SRandy Dunlap * clk_notifier_unregister - unregister a clock rate-change notifier callback 10786bcfa2eSMike Turquette * @clk: clock whose rate we are no longer interested in 10886bcfa2eSMike Turquette * @nb: notifier block which will be unregistered 10986bcfa2eSMike Turquette */ 110b2476490SMike Turquette int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb); 111b2476490SMike Turquette 1125279fc40SBoris BREZILLON /** 1136d30d50dSJerome Brunet * devm_clk_notifier_register - register a managed rate-change notifier callback 1146d30d50dSJerome Brunet * @dev: device for clock "consumer" 1156d30d50dSJerome Brunet * @clk: clock whose rate we are interested in 1166d30d50dSJerome Brunet * @nb: notifier block with callback function pointer 1176d30d50dSJerome Brunet * 1186d30d50dSJerome Brunet * Returns 0 on success, -EERROR otherwise 1196d30d50dSJerome Brunet */ 120e6fb7aeeSJerome Brunet int devm_clk_notifier_register(struct device *dev, struct clk *clk, 121e6fb7aeeSJerome Brunet struct notifier_block *nb); 1226d30d50dSJerome Brunet 1236d30d50dSJerome Brunet /** 1245279fc40SBoris BREZILLON * clk_get_accuracy - obtain the clock accuracy in ppb (parts per billion) 1255279fc40SBoris BREZILLON * for a clock source. 1265279fc40SBoris BREZILLON * @clk: clock source 1275279fc40SBoris BREZILLON * 1285279fc40SBoris BREZILLON * This gets the clock source accuracy expressed in ppb. 1295279fc40SBoris BREZILLON * A perfect clock returns 0. 1305279fc40SBoris BREZILLON */ 1315279fc40SBoris BREZILLON long clk_get_accuracy(struct clk *clk); 1325279fc40SBoris BREZILLON 133e59c5371SMike Turquette /** 134e59c5371SMike Turquette * clk_set_phase - adjust the phase shift of a clock signal 135e59c5371SMike Turquette * @clk: clock signal source 136e59c5371SMike Turquette * @degrees: number of degrees the signal is shifted 137e59c5371SMike Turquette * 138e59c5371SMike Turquette * Shifts the phase of a clock signal by the specified degrees. Returns 0 on 139e59c5371SMike Turquette * success, -EERROR otherwise. 140e59c5371SMike Turquette */ 141e59c5371SMike Turquette int clk_set_phase(struct clk *clk, int degrees); 142e59c5371SMike Turquette 143e59c5371SMike Turquette /** 144e59c5371SMike Turquette * clk_get_phase - return the phase shift of a clock signal 145e59c5371SMike Turquette * @clk: clock signal source 146e59c5371SMike Turquette * 147e59c5371SMike Turquette * Returns the phase shift of a clock node in degrees, otherwise returns 148e59c5371SMike Turquette * -EERROR. 149e59c5371SMike Turquette */ 150e59c5371SMike Turquette int clk_get_phase(struct clk *clk); 151e59c5371SMike Turquette 1523d3801efSMichael Turquette /** 1539fba738aSJerome Brunet * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal 1549fba738aSJerome Brunet * @clk: clock signal source 1559fba738aSJerome Brunet * @num: numerator of the duty cycle ratio to be applied 1569fba738aSJerome Brunet * @den: denominator of the duty cycle ratio to be applied 1579fba738aSJerome Brunet * 1589fba738aSJerome Brunet * Adjust the duty cycle of a clock signal by the specified ratio. Returns 0 on 1599fba738aSJerome Brunet * success, -EERROR otherwise. 1609fba738aSJerome Brunet */ 1619fba738aSJerome Brunet int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den); 1629fba738aSJerome Brunet 1639fba738aSJerome Brunet /** 1649d1c94a6SMauro Carvalho Chehab * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal 1659fba738aSJerome Brunet * @clk: clock signal source 1669fba738aSJerome Brunet * @scale: scaling factor to be applied to represent the ratio as an integer 1679fba738aSJerome Brunet * 1689fba738aSJerome Brunet * Returns the duty cycle ratio multiplied by the scale provided, otherwise 1699fba738aSJerome Brunet * returns -EERROR. 1709fba738aSJerome Brunet */ 1719fba738aSJerome Brunet int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale); 1729fba738aSJerome Brunet 1739fba738aSJerome Brunet /** 1743d3801efSMichael Turquette * clk_is_match - check if two clk's point to the same hardware clock 1753d3801efSMichael Turquette * @p: clk compared against q 1763d3801efSMichael Turquette * @q: clk compared against p 1773d3801efSMichael Turquette * 1783d3801efSMichael Turquette * Returns true if the two struct clk pointers both point to the same hardware 1790e056eb5Smchehab@s-opensource.com * clock node. Put differently, returns true if @p and @q 1800e056eb5Smchehab@s-opensource.com * share the same &struct clk_core object. 1813d3801efSMichael Turquette * 1823d3801efSMichael Turquette * Returns false otherwise. Note that two NULL clks are treated as matching. 1833d3801efSMichael Turquette */ 1843d3801efSMichael Turquette bool clk_is_match(const struct clk *p, const struct clk *q); 1853d3801efSMichael Turquette 1865279fc40SBoris BREZILLON #else 1875279fc40SBoris BREZILLON 188e81b87d2SKrzysztof Kozlowski static inline int clk_notifier_register(struct clk *clk, 189e81b87d2SKrzysztof Kozlowski struct notifier_block *nb) 190e81b87d2SKrzysztof Kozlowski { 191e81b87d2SKrzysztof Kozlowski return -ENOTSUPP; 192e81b87d2SKrzysztof Kozlowski } 193e81b87d2SKrzysztof Kozlowski 194e81b87d2SKrzysztof Kozlowski static inline int clk_notifier_unregister(struct clk *clk, 195e81b87d2SKrzysztof Kozlowski struct notifier_block *nb) 196e81b87d2SKrzysztof Kozlowski { 197e81b87d2SKrzysztof Kozlowski return -ENOTSUPP; 198e81b87d2SKrzysztof Kozlowski } 199e81b87d2SKrzysztof Kozlowski 200e6fb7aeeSJerome Brunet static inline int devm_clk_notifier_register(struct device *dev, 201e6fb7aeeSJerome Brunet struct clk *clk, 202e6fb7aeeSJerome Brunet struct notifier_block *nb) 203e6fb7aeeSJerome Brunet { 204e6fb7aeeSJerome Brunet return -ENOTSUPP; 205e6fb7aeeSJerome Brunet } 206e6fb7aeeSJerome Brunet 2075279fc40SBoris BREZILLON static inline long clk_get_accuracy(struct clk *clk) 2085279fc40SBoris BREZILLON { 2095279fc40SBoris BREZILLON return -ENOTSUPP; 2105279fc40SBoris BREZILLON } 2115279fc40SBoris BREZILLON 212e59c5371SMike Turquette static inline long clk_set_phase(struct clk *clk, int phase) 213e59c5371SMike Turquette { 214e59c5371SMike Turquette return -ENOTSUPP; 215e59c5371SMike Turquette } 216e59c5371SMike Turquette 217e59c5371SMike Turquette static inline long clk_get_phase(struct clk *clk) 218e59c5371SMike Turquette { 219e59c5371SMike Turquette return -ENOTSUPP; 220e59c5371SMike Turquette } 221e59c5371SMike Turquette 2229fba738aSJerome Brunet static inline int clk_set_duty_cycle(struct clk *clk, unsigned int num, 2239fba738aSJerome Brunet unsigned int den) 2249fba738aSJerome Brunet { 2259fba738aSJerome Brunet return -ENOTSUPP; 2269fba738aSJerome Brunet } 2279fba738aSJerome Brunet 2289fba738aSJerome Brunet static inline unsigned int clk_get_scaled_duty_cycle(struct clk *clk, 2299fba738aSJerome Brunet unsigned int scale) 2309fba738aSJerome Brunet { 2319fba738aSJerome Brunet return 0; 2329fba738aSJerome Brunet } 2339fba738aSJerome Brunet 2343d3801efSMichael Turquette static inline bool clk_is_match(const struct clk *p, const struct clk *q) 2353d3801efSMichael Turquette { 2363d3801efSMichael Turquette return p == q; 2373d3801efSMichael Turquette } 2383d3801efSMichael Turquette 2397e87aed9SMark Brown #endif 240b2476490SMike Turquette 2410bfa0820SNicolas Pitre #ifdef CONFIG_HAVE_CLK_PREPARE 242f8ce2547SRussell King /** 24393abe8e4SViresh Kumar * clk_prepare - prepare a clock source 24493abe8e4SViresh Kumar * @clk: clock source 24593abe8e4SViresh Kumar * 24693abe8e4SViresh Kumar * This prepares the clock source for use. 24793abe8e4SViresh Kumar * 24893abe8e4SViresh Kumar * Must not be called from within atomic context. 24993abe8e4SViresh Kumar */ 25093abe8e4SViresh Kumar int clk_prepare(struct clk *clk); 251266e4e9dSDong Aisheng int __must_check clk_bulk_prepare(int num_clks, 252266e4e9dSDong Aisheng const struct clk_bulk_data *clks); 2530bfa0820SNicolas Pitre 2540bfa0820SNicolas Pitre /** 2550bfa0820SNicolas Pitre * clk_is_enabled_when_prepared - indicate if preparing a clock also enables it. 2560bfa0820SNicolas Pitre * @clk: clock source 2570bfa0820SNicolas Pitre * 2580bfa0820SNicolas Pitre * Returns true if clk_prepare() implicitly enables the clock, effectively 2590bfa0820SNicolas Pitre * making clk_enable()/clk_disable() no-ops, false otherwise. 2600bfa0820SNicolas Pitre * 2610bfa0820SNicolas Pitre * This is of interest mainly to the power management code where actually 2620bfa0820SNicolas Pitre * disabling the clock also requires unpreparing it to have any material 2630bfa0820SNicolas Pitre * effect. 2640bfa0820SNicolas Pitre * 2650bfa0820SNicolas Pitre * Regardless of the value returned here, the caller must always invoke 2660bfa0820SNicolas Pitre * clk_enable() or clk_prepare_enable() and counterparts for usage counts 2670bfa0820SNicolas Pitre * to be right. 2680bfa0820SNicolas Pitre */ 2690bfa0820SNicolas Pitre bool clk_is_enabled_when_prepared(struct clk *clk); 27093abe8e4SViresh Kumar #else 27193abe8e4SViresh Kumar static inline int clk_prepare(struct clk *clk) 27293abe8e4SViresh Kumar { 27393abe8e4SViresh Kumar might_sleep(); 27493abe8e4SViresh Kumar return 0; 27593abe8e4SViresh Kumar } 276266e4e9dSDong Aisheng 277570aaec7SAndrey Smirnov static inline int __must_check 278570aaec7SAndrey Smirnov clk_bulk_prepare(int num_clks, const struct clk_bulk_data *clks) 279266e4e9dSDong Aisheng { 280266e4e9dSDong Aisheng might_sleep(); 281266e4e9dSDong Aisheng return 0; 282266e4e9dSDong Aisheng } 2830bfa0820SNicolas Pitre 2840bfa0820SNicolas Pitre static inline bool clk_is_enabled_when_prepared(struct clk *clk) 2850bfa0820SNicolas Pitre { 2860bfa0820SNicolas Pitre return false; 2870bfa0820SNicolas Pitre } 28893abe8e4SViresh Kumar #endif 28993abe8e4SViresh Kumar 29093abe8e4SViresh Kumar /** 29193abe8e4SViresh Kumar * clk_unprepare - undo preparation of a clock source 29293abe8e4SViresh Kumar * @clk: clock source 29393abe8e4SViresh Kumar * 29493abe8e4SViresh Kumar * This undoes a previously prepared clock. The caller must balance 29593abe8e4SViresh Kumar * the number of prepare and unprepare calls. 29693abe8e4SViresh Kumar * 29793abe8e4SViresh Kumar * Must not be called from within atomic context. 29893abe8e4SViresh Kumar */ 29993abe8e4SViresh Kumar #ifdef CONFIG_HAVE_CLK_PREPARE 30093abe8e4SViresh Kumar void clk_unprepare(struct clk *clk); 301266e4e9dSDong Aisheng void clk_bulk_unprepare(int num_clks, const struct clk_bulk_data *clks); 30293abe8e4SViresh Kumar #else 30393abe8e4SViresh Kumar static inline void clk_unprepare(struct clk *clk) 30493abe8e4SViresh Kumar { 30593abe8e4SViresh Kumar might_sleep(); 30693abe8e4SViresh Kumar } 307570aaec7SAndrey Smirnov static inline void clk_bulk_unprepare(int num_clks, 308570aaec7SAndrey Smirnov const struct clk_bulk_data *clks) 309266e4e9dSDong Aisheng { 310266e4e9dSDong Aisheng might_sleep(); 311266e4e9dSDong Aisheng } 31293abe8e4SViresh Kumar #endif 31393abe8e4SViresh Kumar 31493abe8e4SViresh Kumar #ifdef CONFIG_HAVE_CLK 31593abe8e4SViresh Kumar /** 316f8ce2547SRussell King * clk_get - lookup and obtain a reference to a clock producer. 317f8ce2547SRussell King * @dev: device for clock "consumer" 318a58b3a4aSJan-Simon Möller * @id: clock consumer ID 319f8ce2547SRussell King * 320f8ce2547SRussell King * Returns a struct clk corresponding to the clock producer, or 321f8ce2547SRussell King * valid IS_ERR() condition containing errno. The implementation 322f8ce2547SRussell King * uses @dev and @id to determine the clock consumer, and thereby 323f8ce2547SRussell King * the clock producer. (IOW, @id may be identical strings, but 324f8ce2547SRussell King * clk_get may return different clock producers depending on @dev.) 325f8ce2547SRussell King * 326f8ce2547SRussell King * Drivers must assume that the clock source is not enabled. 327f7ad160bSAlex Raimondi * 328f7ad160bSAlex Raimondi * clk_get should not be called from within interrupt context. 329f8ce2547SRussell King */ 330f8ce2547SRussell King struct clk *clk_get(struct device *dev, const char *id); 331f8ce2547SRussell King 332f8ce2547SRussell King /** 333266e4e9dSDong Aisheng * clk_bulk_get - lookup and obtain a number of references to clock producer. 334266e4e9dSDong Aisheng * @dev: device for clock "consumer" 335266e4e9dSDong Aisheng * @num_clks: the number of clk_bulk_data 336266e4e9dSDong Aisheng * @clks: the clk_bulk_data table of consumer 337266e4e9dSDong Aisheng * 338266e4e9dSDong Aisheng * This helper function allows drivers to get several clk consumers in one 339266e4e9dSDong Aisheng * operation. If any of the clk cannot be acquired then any clks 340266e4e9dSDong Aisheng * that were obtained will be freed before returning to the caller. 341266e4e9dSDong Aisheng * 342266e4e9dSDong Aisheng * Returns 0 if all clocks specified in clk_bulk_data table are obtained 343266e4e9dSDong Aisheng * successfully, or valid IS_ERR() condition containing errno. 344266e4e9dSDong Aisheng * The implementation uses @dev and @clk_bulk_data.id to determine the 345266e4e9dSDong Aisheng * clock consumer, and thereby the clock producer. 346266e4e9dSDong Aisheng * The clock returned is stored in each @clk_bulk_data.clk field. 347266e4e9dSDong Aisheng * 348266e4e9dSDong Aisheng * Drivers must assume that the clock source is not enabled. 349266e4e9dSDong Aisheng * 350266e4e9dSDong Aisheng * clk_bulk_get should not be called from within interrupt context. 351266e4e9dSDong Aisheng */ 352266e4e9dSDong Aisheng int __must_check clk_bulk_get(struct device *dev, int num_clks, 353266e4e9dSDong Aisheng struct clk_bulk_data *clks); 354616e45dfSDong Aisheng /** 355616e45dfSDong Aisheng * clk_bulk_get_all - lookup and obtain all available references to clock 356616e45dfSDong Aisheng * producer. 357616e45dfSDong Aisheng * @dev: device for clock "consumer" 358616e45dfSDong Aisheng * @clks: pointer to the clk_bulk_data table of consumer 359616e45dfSDong Aisheng * 360616e45dfSDong Aisheng * This helper function allows drivers to get all clk consumers in one 361616e45dfSDong Aisheng * operation. If any of the clk cannot be acquired then any clks 362616e45dfSDong Aisheng * that were obtained will be freed before returning to the caller. 363616e45dfSDong Aisheng * 364616e45dfSDong Aisheng * Returns a positive value for the number of clocks obtained while the 365616e45dfSDong Aisheng * clock references are stored in the clk_bulk_data table in @clks field. 366616e45dfSDong Aisheng * Returns 0 if there're none and a negative value if something failed. 367616e45dfSDong Aisheng * 368616e45dfSDong Aisheng * Drivers must assume that the clock source is not enabled. 369616e45dfSDong Aisheng * 370616e45dfSDong Aisheng * clk_bulk_get should not be called from within interrupt context. 371616e45dfSDong Aisheng */ 372616e45dfSDong Aisheng int __must_check clk_bulk_get_all(struct device *dev, 373616e45dfSDong Aisheng struct clk_bulk_data **clks); 3742f25528eSSylwester Nawrocki 3752f25528eSSylwester Nawrocki /** 3762f25528eSSylwester Nawrocki * clk_bulk_get_optional - lookup and obtain a number of references to clock producer 3772f25528eSSylwester Nawrocki * @dev: device for clock "consumer" 3782f25528eSSylwester Nawrocki * @num_clks: the number of clk_bulk_data 3792f25528eSSylwester Nawrocki * @clks: the clk_bulk_data table of consumer 3802f25528eSSylwester Nawrocki * 3812f25528eSSylwester Nawrocki * Behaves the same as clk_bulk_get() except where there is no clock producer. 3822f25528eSSylwester Nawrocki * In this case, instead of returning -ENOENT, the function returns 0 and 3832f25528eSSylwester Nawrocki * NULL for a clk for which a clock producer could not be determined. 3842f25528eSSylwester Nawrocki */ 3852f25528eSSylwester Nawrocki int __must_check clk_bulk_get_optional(struct device *dev, int num_clks, 3862f25528eSSylwester Nawrocki struct clk_bulk_data *clks); 387266e4e9dSDong Aisheng /** 388618aee02SDong Aisheng * devm_clk_bulk_get - managed get multiple clk consumers 389618aee02SDong Aisheng * @dev: device for clock "consumer" 390618aee02SDong Aisheng * @num_clks: the number of clk_bulk_data 391618aee02SDong Aisheng * @clks: the clk_bulk_data table of consumer 392618aee02SDong Aisheng * 393618aee02SDong Aisheng * Return 0 on success, an errno on failure. 394618aee02SDong Aisheng * 395618aee02SDong Aisheng * This helper function allows drivers to get several clk 396618aee02SDong Aisheng * consumers in one operation with management, the clks will 397618aee02SDong Aisheng * automatically be freed when the device is unbound. 398618aee02SDong Aisheng */ 399618aee02SDong Aisheng int __must_check devm_clk_bulk_get(struct device *dev, int num_clks, 400618aee02SDong Aisheng struct clk_bulk_data *clks); 401f08c2e28SDong Aisheng /** 4029bd5ef0bSSylwester Nawrocki * devm_clk_bulk_get_optional - managed get multiple optional consumer clocks 4039bd5ef0bSSylwester Nawrocki * @dev: device for clock "consumer" 4046ee82ef0SSylwester Nawrocki * @num_clks: the number of clk_bulk_data 4059bd5ef0bSSylwester Nawrocki * @clks: pointer to the clk_bulk_data table of consumer 4069bd5ef0bSSylwester Nawrocki * 4079bd5ef0bSSylwester Nawrocki * Behaves the same as devm_clk_bulk_get() except where there is no clock 4089bd5ef0bSSylwester Nawrocki * producer. In this case, instead of returning -ENOENT, the function returns 4099bd5ef0bSSylwester Nawrocki * NULL for given clk. It is assumed all clocks in clk_bulk_data are optional. 4109bd5ef0bSSylwester Nawrocki * 4119bd5ef0bSSylwester Nawrocki * Returns 0 if all clocks specified in clk_bulk_data table are obtained 4129bd5ef0bSSylwester Nawrocki * successfully or for any clk there was no clk provider available, otherwise 4139bd5ef0bSSylwester Nawrocki * returns valid IS_ERR() condition containing errno. 4149bd5ef0bSSylwester Nawrocki * The implementation uses @dev and @clk_bulk_data.id to determine the 4159bd5ef0bSSylwester Nawrocki * clock consumer, and thereby the clock producer. 4169bd5ef0bSSylwester Nawrocki * The clock returned is stored in each @clk_bulk_data.clk field. 4179bd5ef0bSSylwester Nawrocki * 4189bd5ef0bSSylwester Nawrocki * Drivers must assume that the clock source is not enabled. 4199bd5ef0bSSylwester Nawrocki * 4209bd5ef0bSSylwester Nawrocki * clk_bulk_get should not be called from within interrupt context. 4219bd5ef0bSSylwester Nawrocki */ 4229bd5ef0bSSylwester Nawrocki int __must_check devm_clk_bulk_get_optional(struct device *dev, int num_clks, 4239bd5ef0bSSylwester Nawrocki struct clk_bulk_data *clks); 4249bd5ef0bSSylwester Nawrocki /** 425f08c2e28SDong Aisheng * devm_clk_bulk_get_all - managed get multiple clk consumers 426f08c2e28SDong Aisheng * @dev: device for clock "consumer" 427f08c2e28SDong Aisheng * @clks: pointer to the clk_bulk_data table of consumer 428f08c2e28SDong Aisheng * 429f08c2e28SDong Aisheng * Returns a positive value for the number of clocks obtained while the 430f08c2e28SDong Aisheng * clock references are stored in the clk_bulk_data table in @clks field. 431f08c2e28SDong Aisheng * Returns 0 if there're none and a negative value if something failed. 432f08c2e28SDong Aisheng * 433f08c2e28SDong Aisheng * This helper function allows drivers to get several clk 434f08c2e28SDong Aisheng * consumers in one operation with management, the clks will 435f08c2e28SDong Aisheng * automatically be freed when the device is unbound. 436f08c2e28SDong Aisheng */ 437f08c2e28SDong Aisheng 438f08c2e28SDong Aisheng int __must_check devm_clk_bulk_get_all(struct device *dev, 439f08c2e28SDong Aisheng struct clk_bulk_data **clks); 440618aee02SDong Aisheng 441618aee02SDong Aisheng /** 442a8a97db9SMark Brown * devm_clk_get - lookup and obtain a managed reference to a clock producer. 443a8a97db9SMark Brown * @dev: device for clock "consumer" 444a58b3a4aSJan-Simon Möller * @id: clock consumer ID 445a8a97db9SMark Brown * 446a8a97db9SMark Brown * Returns a struct clk corresponding to the clock producer, or 447a8a97db9SMark Brown * valid IS_ERR() condition containing errno. The implementation 448a8a97db9SMark Brown * uses @dev and @id to determine the clock consumer, and thereby 449a8a97db9SMark Brown * the clock producer. (IOW, @id may be identical strings, but 450a8a97db9SMark Brown * clk_get may return different clock producers depending on @dev.) 451a8a97db9SMark Brown * 452a8a97db9SMark Brown * Drivers must assume that the clock source is not enabled. 453a8a97db9SMark Brown * 454a8a97db9SMark Brown * devm_clk_get should not be called from within interrupt context. 455a8a97db9SMark Brown * 456a8a97db9SMark Brown * The clock will automatically be freed when the device is unbound 457a8a97db9SMark Brown * from the bus. 458a8a97db9SMark Brown */ 459a8a97db9SMark Brown struct clk *devm_clk_get(struct device *dev, const char *id); 460a8a97db9SMark Brown 461a8a97db9SMark Brown /** 46260b8f0ddSPhil Edworthy * devm_clk_get_optional - lookup and obtain a managed reference to an optional 46360b8f0ddSPhil Edworthy * clock producer. 46460b8f0ddSPhil Edworthy * @dev: device for clock "consumer" 46560b8f0ddSPhil Edworthy * @id: clock consumer ID 46660b8f0ddSPhil Edworthy * 46760b8f0ddSPhil Edworthy * Behaves the same as devm_clk_get() except where there is no clock producer. 46860b8f0ddSPhil Edworthy * In this case, instead of returning -ENOENT, the function returns NULL. 46960b8f0ddSPhil Edworthy */ 47060b8f0ddSPhil Edworthy struct clk *devm_clk_get_optional(struct device *dev, const char *id); 47160b8f0ddSPhil Edworthy 47260b8f0ddSPhil Edworthy /** 47371a2f115SKuninori Morimoto * devm_get_clk_from_child - lookup and obtain a managed reference to a 47471a2f115SKuninori Morimoto * clock producer from child node. 47571a2f115SKuninori Morimoto * @dev: device for clock "consumer" 47671a2f115SKuninori Morimoto * @np: pointer to clock consumer node 47771a2f115SKuninori Morimoto * @con_id: clock consumer ID 47871a2f115SKuninori Morimoto * 47971a2f115SKuninori Morimoto * This function parses the clocks, and uses them to look up the 48071a2f115SKuninori Morimoto * struct clk from the registered list of clock providers by using 48171a2f115SKuninori Morimoto * @np and @con_id 48271a2f115SKuninori Morimoto * 48371a2f115SKuninori Morimoto * The clock will automatically be freed when the device is unbound 48471a2f115SKuninori Morimoto * from the bus. 48571a2f115SKuninori Morimoto */ 48671a2f115SKuninori Morimoto struct clk *devm_get_clk_from_child(struct device *dev, 48771a2f115SKuninori Morimoto struct device_node *np, const char *con_id); 48855e9b8b7SJerome Brunet /** 48955e9b8b7SJerome Brunet * clk_rate_exclusive_get - get exclusivity over the rate control of a 49055e9b8b7SJerome Brunet * producer 49155e9b8b7SJerome Brunet * @clk: clock source 49255e9b8b7SJerome Brunet * 49355e9b8b7SJerome Brunet * This function allows drivers to get exclusive control over the rate of a 49455e9b8b7SJerome Brunet * provider. It prevents any other consumer to execute, even indirectly, 49555e9b8b7SJerome Brunet * opereation which could alter the rate of the provider or cause glitches 49655e9b8b7SJerome Brunet * 49755e9b8b7SJerome Brunet * If exlusivity is claimed more than once on clock, even by the same driver, 49855e9b8b7SJerome Brunet * the rate effectively gets locked as exclusivity can't be preempted. 49955e9b8b7SJerome Brunet * 50055e9b8b7SJerome Brunet * Must not be called from within atomic context. 50155e9b8b7SJerome Brunet * 50255e9b8b7SJerome Brunet * Returns success (0) or negative errno. 50355e9b8b7SJerome Brunet */ 50455e9b8b7SJerome Brunet int clk_rate_exclusive_get(struct clk *clk); 50555e9b8b7SJerome Brunet 50655e9b8b7SJerome Brunet /** 50755e9b8b7SJerome Brunet * clk_rate_exclusive_put - release exclusivity over the rate control of a 50855e9b8b7SJerome Brunet * producer 50955e9b8b7SJerome Brunet * @clk: clock source 51055e9b8b7SJerome Brunet * 51155e9b8b7SJerome Brunet * This function allows drivers to release the exclusivity it previously got 51255e9b8b7SJerome Brunet * from clk_rate_exclusive_get() 51355e9b8b7SJerome Brunet * 51455e9b8b7SJerome Brunet * The caller must balance the number of clk_rate_exclusive_get() and 51555e9b8b7SJerome Brunet * clk_rate_exclusive_put() calls. 51655e9b8b7SJerome Brunet * 51755e9b8b7SJerome Brunet * Must not be called from within atomic context. 51855e9b8b7SJerome Brunet */ 51955e9b8b7SJerome Brunet void clk_rate_exclusive_put(struct clk *clk); 52071a2f115SKuninori Morimoto 52171a2f115SKuninori Morimoto /** 522f8ce2547SRussell King * clk_enable - inform the system when the clock source should be running. 523f8ce2547SRussell King * @clk: clock source 524f8ce2547SRussell King * 525f8ce2547SRussell King * If the clock can not be enabled/disabled, this should return success. 526f8ce2547SRussell King * 52740d3e0f4SRussell King * May be called from atomic contexts. 52840d3e0f4SRussell King * 529f8ce2547SRussell King * Returns success (0) or negative errno. 530f8ce2547SRussell King */ 531f8ce2547SRussell King int clk_enable(struct clk *clk); 532f8ce2547SRussell King 533f8ce2547SRussell King /** 534266e4e9dSDong Aisheng * clk_bulk_enable - inform the system when the set of clks should be running. 535266e4e9dSDong Aisheng * @num_clks: the number of clk_bulk_data 536266e4e9dSDong Aisheng * @clks: the clk_bulk_data table of consumer 537266e4e9dSDong Aisheng * 538266e4e9dSDong Aisheng * May be called from atomic contexts. 539266e4e9dSDong Aisheng * 540266e4e9dSDong Aisheng * Returns success (0) or negative errno. 541266e4e9dSDong Aisheng */ 542266e4e9dSDong Aisheng int __must_check clk_bulk_enable(int num_clks, 543266e4e9dSDong Aisheng const struct clk_bulk_data *clks); 544266e4e9dSDong Aisheng 545266e4e9dSDong Aisheng /** 546f8ce2547SRussell King * clk_disable - inform the system when the clock source is no longer required. 547f8ce2547SRussell King * @clk: clock source 548f8ce2547SRussell King * 549f8ce2547SRussell King * Inform the system that a clock source is no longer required by 550f8ce2547SRussell King * a driver and may be shut down. 551f8ce2547SRussell King * 55240d3e0f4SRussell King * May be called from atomic contexts. 55340d3e0f4SRussell King * 554f8ce2547SRussell King * Implementation detail: if the clock source is shared between 555f8ce2547SRussell King * multiple drivers, clk_enable() calls must be balanced by the 556f8ce2547SRussell King * same number of clk_disable() calls for the clock source to be 557f8ce2547SRussell King * disabled. 558f8ce2547SRussell King */ 559f8ce2547SRussell King void clk_disable(struct clk *clk); 560f8ce2547SRussell King 561f8ce2547SRussell King /** 562266e4e9dSDong Aisheng * clk_bulk_disable - inform the system when the set of clks is no 563266e4e9dSDong Aisheng * longer required. 564266e4e9dSDong Aisheng * @num_clks: the number of clk_bulk_data 565266e4e9dSDong Aisheng * @clks: the clk_bulk_data table of consumer 566266e4e9dSDong Aisheng * 567266e4e9dSDong Aisheng * Inform the system that a set of clks is no longer required by 568266e4e9dSDong Aisheng * a driver and may be shut down. 569266e4e9dSDong Aisheng * 570266e4e9dSDong Aisheng * May be called from atomic contexts. 571266e4e9dSDong Aisheng * 572266e4e9dSDong Aisheng * Implementation detail: if the set of clks is shared between 573266e4e9dSDong Aisheng * multiple drivers, clk_bulk_enable() calls must be balanced by the 574266e4e9dSDong Aisheng * same number of clk_bulk_disable() calls for the clock source to be 575266e4e9dSDong Aisheng * disabled. 576266e4e9dSDong Aisheng */ 577266e4e9dSDong Aisheng void clk_bulk_disable(int num_clks, const struct clk_bulk_data *clks); 578266e4e9dSDong Aisheng 579266e4e9dSDong Aisheng /** 580f8ce2547SRussell King * clk_get_rate - obtain the current clock rate (in Hz) for a clock source. 581f8ce2547SRussell King * This is only valid once the clock source has been enabled. 582f8ce2547SRussell King * @clk: clock source 583f8ce2547SRussell King */ 584f8ce2547SRussell King unsigned long clk_get_rate(struct clk *clk); 585f8ce2547SRussell King 586f8ce2547SRussell King /** 587f8ce2547SRussell King * clk_put - "free" the clock source 588f8ce2547SRussell King * @clk: clock source 589f8ce2547SRussell King * 590f8ce2547SRussell King * Note: drivers must ensure that all clk_enable calls made on this 591f8ce2547SRussell King * clock source are balanced by clk_disable calls prior to calling 592f8ce2547SRussell King * this function. 593f7ad160bSAlex Raimondi * 594f7ad160bSAlex Raimondi * clk_put should not be called from within interrupt context. 595f8ce2547SRussell King */ 596f8ce2547SRussell King void clk_put(struct clk *clk); 597f8ce2547SRussell King 598a8a97db9SMark Brown /** 599266e4e9dSDong Aisheng * clk_bulk_put - "free" the clock source 600266e4e9dSDong Aisheng * @num_clks: the number of clk_bulk_data 601266e4e9dSDong Aisheng * @clks: the clk_bulk_data table of consumer 602266e4e9dSDong Aisheng * 603266e4e9dSDong Aisheng * Note: drivers must ensure that all clk_bulk_enable calls made on this 604266e4e9dSDong Aisheng * clock source are balanced by clk_bulk_disable calls prior to calling 605266e4e9dSDong Aisheng * this function. 606266e4e9dSDong Aisheng * 607266e4e9dSDong Aisheng * clk_bulk_put should not be called from within interrupt context. 608266e4e9dSDong Aisheng */ 609266e4e9dSDong Aisheng void clk_bulk_put(int num_clks, struct clk_bulk_data *clks); 610266e4e9dSDong Aisheng 611266e4e9dSDong Aisheng /** 612616e45dfSDong Aisheng * clk_bulk_put_all - "free" all the clock source 613616e45dfSDong Aisheng * @num_clks: the number of clk_bulk_data 614616e45dfSDong Aisheng * @clks: the clk_bulk_data table of consumer 615616e45dfSDong Aisheng * 616616e45dfSDong Aisheng * Note: drivers must ensure that all clk_bulk_enable calls made on this 617616e45dfSDong Aisheng * clock source are balanced by clk_bulk_disable calls prior to calling 618616e45dfSDong Aisheng * this function. 619616e45dfSDong Aisheng * 620616e45dfSDong Aisheng * clk_bulk_put_all should not be called from within interrupt context. 621616e45dfSDong Aisheng */ 622616e45dfSDong Aisheng void clk_bulk_put_all(int num_clks, struct clk_bulk_data *clks); 623616e45dfSDong Aisheng 624616e45dfSDong Aisheng /** 625a8a97db9SMark Brown * devm_clk_put - "free" a managed clock source 626da3dae54SMasanari Iida * @dev: device used to acquire the clock 627a8a97db9SMark Brown * @clk: clock source acquired with devm_clk_get() 628a8a97db9SMark Brown * 629a8a97db9SMark Brown * Note: drivers must ensure that all clk_enable calls made on this 630a8a97db9SMark Brown * clock source are balanced by clk_disable calls prior to calling 631a8a97db9SMark Brown * this function. 632a8a97db9SMark Brown * 633a8a97db9SMark Brown * clk_put should not be called from within interrupt context. 634a8a97db9SMark Brown */ 635a8a97db9SMark Brown void devm_clk_put(struct device *dev, struct clk *clk); 636f8ce2547SRussell King 637f8ce2547SRussell King /* 638f8ce2547SRussell King * The remaining APIs are optional for machine class support. 639f8ce2547SRussell King */ 640f8ce2547SRussell King 641f8ce2547SRussell King 642f8ce2547SRussell King /** 643f8ce2547SRussell King * clk_round_rate - adjust a rate to the exact rate a clock can provide 644f8ce2547SRussell King * @clk: clock source 645f8ce2547SRussell King * @rate: desired clock rate in Hz 646f8ce2547SRussell King * 647d2d14a77SRussell King * This answers the question "if I were to pass @rate to clk_set_rate(), 648d2d14a77SRussell King * what clock rate would I end up with?" without changing the hardware 649d2d14a77SRussell King * in any way. In other words: 650d2d14a77SRussell King * 651d2d14a77SRussell King * rate = clk_round_rate(clk, r); 652d2d14a77SRussell King * 653d2d14a77SRussell King * and: 654d2d14a77SRussell King * 655d2d14a77SRussell King * clk_set_rate(clk, r); 656d2d14a77SRussell King * rate = clk_get_rate(clk); 657d2d14a77SRussell King * 658d2d14a77SRussell King * are equivalent except the former does not modify the clock hardware 659d2d14a77SRussell King * in any way. 660d2d14a77SRussell King * 661f8ce2547SRussell King * Returns rounded clock rate in Hz, or negative errno. 662f8ce2547SRussell King */ 663f8ce2547SRussell King long clk_round_rate(struct clk *clk, unsigned long rate); 664f8ce2547SRussell King 665f8ce2547SRussell King /** 666f8ce2547SRussell King * clk_set_rate - set the clock rate for a clock source 667f8ce2547SRussell King * @clk: clock source 668f8ce2547SRussell King * @rate: desired clock rate in Hz 669f8ce2547SRussell King * 67064c76b31SMartin Blumenstingl * Updating the rate starts at the top-most affected clock and then 67164c76b31SMartin Blumenstingl * walks the tree down to the bottom-most clock that needs updating. 67264c76b31SMartin Blumenstingl * 673f8ce2547SRussell King * Returns success (0) or negative errno. 674f8ce2547SRussell King */ 675f8ce2547SRussell King int clk_set_rate(struct clk *clk, unsigned long rate); 676f8ce2547SRussell King 677f8ce2547SRussell King /** 67855e9b8b7SJerome Brunet * clk_set_rate_exclusive- set the clock rate and claim exclusivity over 67955e9b8b7SJerome Brunet * clock source 68055e9b8b7SJerome Brunet * @clk: clock source 68155e9b8b7SJerome Brunet * @rate: desired clock rate in Hz 68255e9b8b7SJerome Brunet * 68355e9b8b7SJerome Brunet * This helper function allows drivers to atomically set the rate of a producer 68455e9b8b7SJerome Brunet * and claim exclusivity over the rate control of the producer. 68555e9b8b7SJerome Brunet * 68655e9b8b7SJerome Brunet * It is essentially a combination of clk_set_rate() and 68755e9b8b7SJerome Brunet * clk_rate_exclusite_get(). Caller must balance this call with a call to 68855e9b8b7SJerome Brunet * clk_rate_exclusive_put() 68955e9b8b7SJerome Brunet * 69055e9b8b7SJerome Brunet * Returns success (0) or negative errno. 69155e9b8b7SJerome Brunet */ 69255e9b8b7SJerome Brunet int clk_set_rate_exclusive(struct clk *clk, unsigned long rate); 69355e9b8b7SJerome Brunet 69455e9b8b7SJerome Brunet /** 6954e88f3deSThierry Reding * clk_has_parent - check if a clock is a possible parent for another 6964e88f3deSThierry Reding * @clk: clock source 6974e88f3deSThierry Reding * @parent: parent clock source 6984e88f3deSThierry Reding * 6994e88f3deSThierry Reding * This function can be used in drivers that need to check that a clock can be 7004e88f3deSThierry Reding * the parent of another without actually changing the parent. 7014e88f3deSThierry Reding * 7024e88f3deSThierry Reding * Returns true if @parent is a possible parent for @clk, false otherwise. 7034e88f3deSThierry Reding */ 7044e88f3deSThierry Reding bool clk_has_parent(struct clk *clk, struct clk *parent); 7054e88f3deSThierry Reding 7064e88f3deSThierry Reding /** 7071c8e6004STomeu Vizoso * clk_set_rate_range - set a rate range for a clock source 7081c8e6004STomeu Vizoso * @clk: clock source 7091c8e6004STomeu Vizoso * @min: desired minimum clock rate in Hz, inclusive 7101c8e6004STomeu Vizoso * @max: desired maximum clock rate in Hz, inclusive 7111c8e6004STomeu Vizoso * 7121c8e6004STomeu Vizoso * Returns success (0) or negative errno. 7131c8e6004STomeu Vizoso */ 7141c8e6004STomeu Vizoso int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max); 7151c8e6004STomeu Vizoso 7161c8e6004STomeu Vizoso /** 7171c8e6004STomeu Vizoso * clk_set_min_rate - set a minimum clock rate for a clock source 7181c8e6004STomeu Vizoso * @clk: clock source 7191c8e6004STomeu Vizoso * @rate: desired minimum clock rate in Hz, inclusive 7201c8e6004STomeu Vizoso * 7211c8e6004STomeu Vizoso * Returns success (0) or negative errno. 7221c8e6004STomeu Vizoso */ 7231c8e6004STomeu Vizoso int clk_set_min_rate(struct clk *clk, unsigned long rate); 7241c8e6004STomeu Vizoso 7251c8e6004STomeu Vizoso /** 7261c8e6004STomeu Vizoso * clk_set_max_rate - set a maximum clock rate for a clock source 7271c8e6004STomeu Vizoso * @clk: clock source 7281c8e6004STomeu Vizoso * @rate: desired maximum clock rate in Hz, inclusive 7291c8e6004STomeu Vizoso * 7301c8e6004STomeu Vizoso * Returns success (0) or negative errno. 7311c8e6004STomeu Vizoso */ 7321c8e6004STomeu Vizoso int clk_set_max_rate(struct clk *clk, unsigned long rate); 7331c8e6004STomeu Vizoso 7341c8e6004STomeu Vizoso /** 735f8ce2547SRussell King * clk_set_parent - set the parent clock source for this clock 736f8ce2547SRussell King * @clk: clock source 737f8ce2547SRussell King * @parent: parent clock source 738f8ce2547SRussell King * 739f8ce2547SRussell King * Returns success (0) or negative errno. 740f8ce2547SRussell King */ 741f8ce2547SRussell King int clk_set_parent(struct clk *clk, struct clk *parent); 742f8ce2547SRussell King 743f8ce2547SRussell King /** 744f8ce2547SRussell King * clk_get_parent - get the parent clock source for this clock 745f8ce2547SRussell King * @clk: clock source 746f8ce2547SRussell King * 747f8ce2547SRussell King * Returns struct clk corresponding to parent clock source, or 748f8ce2547SRussell King * valid IS_ERR() condition containing errno. 749f8ce2547SRussell King */ 750f8ce2547SRussell King struct clk *clk_get_parent(struct clk *clk); 751f8ce2547SRussell King 75205fd8e73SSascha Hauer /** 75305fd8e73SSascha Hauer * clk_get_sys - get a clock based upon the device name 75405fd8e73SSascha Hauer * @dev_id: device name 75505fd8e73SSascha Hauer * @con_id: connection ID 75605fd8e73SSascha Hauer * 75705fd8e73SSascha Hauer * Returns a struct clk corresponding to the clock producer, or 75805fd8e73SSascha Hauer * valid IS_ERR() condition containing errno. The implementation 75905fd8e73SSascha Hauer * uses @dev_id and @con_id to determine the clock consumer, and 76005fd8e73SSascha Hauer * thereby the clock producer. In contrast to clk_get() this function 76105fd8e73SSascha Hauer * takes the device name instead of the device itself for identification. 76205fd8e73SSascha Hauer * 76305fd8e73SSascha Hauer * Drivers must assume that the clock source is not enabled. 76405fd8e73SSascha Hauer * 76505fd8e73SSascha Hauer * clk_get_sys should not be called from within interrupt context. 76605fd8e73SSascha Hauer */ 76705fd8e73SSascha Hauer struct clk *clk_get_sys(const char *dev_id, const char *con_id); 76805fd8e73SSascha Hauer 7698b95d1ceSRuss Dill /** 7708b95d1ceSRuss Dill * clk_save_context - save clock context for poweroff 7718b95d1ceSRuss Dill * 7728b95d1ceSRuss Dill * Saves the context of the clock register for powerstates in which the 7738b95d1ceSRuss Dill * contents of the registers will be lost. Occurs deep within the suspend 7748b95d1ceSRuss Dill * code so locking is not necessary. 7758b95d1ceSRuss Dill */ 7768b95d1ceSRuss Dill int clk_save_context(void); 7778b95d1ceSRuss Dill 7788b95d1ceSRuss Dill /** 7798b95d1ceSRuss Dill * clk_restore_context - restore clock context after poweroff 7808b95d1ceSRuss Dill * 7818b95d1ceSRuss Dill * This occurs with all clocks enabled. Occurs deep within the resume code 7828b95d1ceSRuss Dill * so locking is not necessary. 7838b95d1ceSRuss Dill */ 7848b95d1ceSRuss Dill void clk_restore_context(void); 7858b95d1ceSRuss Dill 78693abe8e4SViresh Kumar #else /* !CONFIG_HAVE_CLK */ 78793abe8e4SViresh Kumar 78893abe8e4SViresh Kumar static inline struct clk *clk_get(struct device *dev, const char *id) 78993abe8e4SViresh Kumar { 79093abe8e4SViresh Kumar return NULL; 79193abe8e4SViresh Kumar } 79293abe8e4SViresh Kumar 7936e0d4ff4SDong Aisheng static inline int __must_check clk_bulk_get(struct device *dev, int num_clks, 794266e4e9dSDong Aisheng struct clk_bulk_data *clks) 795266e4e9dSDong Aisheng { 796266e4e9dSDong Aisheng return 0; 797266e4e9dSDong Aisheng } 798266e4e9dSDong Aisheng 7992f25528eSSylwester Nawrocki static inline int __must_check clk_bulk_get_optional(struct device *dev, 8002f25528eSSylwester Nawrocki int num_clks, struct clk_bulk_data *clks) 8012f25528eSSylwester Nawrocki { 8022f25528eSSylwester Nawrocki return 0; 8032f25528eSSylwester Nawrocki } 8042f25528eSSylwester Nawrocki 805616e45dfSDong Aisheng static inline int __must_check clk_bulk_get_all(struct device *dev, 806616e45dfSDong Aisheng struct clk_bulk_data **clks) 807616e45dfSDong Aisheng { 808616e45dfSDong Aisheng return 0; 809616e45dfSDong Aisheng } 810616e45dfSDong Aisheng 81193abe8e4SViresh Kumar static inline struct clk *devm_clk_get(struct device *dev, const char *id) 81293abe8e4SViresh Kumar { 81393abe8e4SViresh Kumar return NULL; 81493abe8e4SViresh Kumar } 81593abe8e4SViresh Kumar 81660b8f0ddSPhil Edworthy static inline struct clk *devm_clk_get_optional(struct device *dev, 81760b8f0ddSPhil Edworthy const char *id) 81860b8f0ddSPhil Edworthy { 81960b8f0ddSPhil Edworthy return NULL; 82060b8f0ddSPhil Edworthy } 82160b8f0ddSPhil Edworthy 8226e0d4ff4SDong Aisheng static inline int __must_check devm_clk_bulk_get(struct device *dev, int num_clks, 823618aee02SDong Aisheng struct clk_bulk_data *clks) 824618aee02SDong Aisheng { 825618aee02SDong Aisheng return 0; 826618aee02SDong Aisheng } 827618aee02SDong Aisheng 8289bd5ef0bSSylwester Nawrocki static inline int __must_check devm_clk_bulk_get_optional(struct device *dev, 8299bd5ef0bSSylwester Nawrocki int num_clks, struct clk_bulk_data *clks) 8309bd5ef0bSSylwester Nawrocki { 8319bd5ef0bSSylwester Nawrocki return 0; 8329bd5ef0bSSylwester Nawrocki } 8339bd5ef0bSSylwester Nawrocki 834f08c2e28SDong Aisheng static inline int __must_check devm_clk_bulk_get_all(struct device *dev, 835f08c2e28SDong Aisheng struct clk_bulk_data **clks) 836f08c2e28SDong Aisheng { 837f08c2e28SDong Aisheng 838f08c2e28SDong Aisheng return 0; 839f08c2e28SDong Aisheng } 840f08c2e28SDong Aisheng 84171a2f115SKuninori Morimoto static inline struct clk *devm_get_clk_from_child(struct device *dev, 84271a2f115SKuninori Morimoto struct device_node *np, const char *con_id) 84371a2f115SKuninori Morimoto { 84471a2f115SKuninori Morimoto return NULL; 84571a2f115SKuninori Morimoto } 84671a2f115SKuninori Morimoto 84793abe8e4SViresh Kumar static inline void clk_put(struct clk *clk) {} 84893abe8e4SViresh Kumar 849266e4e9dSDong Aisheng static inline void clk_bulk_put(int num_clks, struct clk_bulk_data *clks) {} 850266e4e9dSDong Aisheng 851616e45dfSDong Aisheng static inline void clk_bulk_put_all(int num_clks, struct clk_bulk_data *clks) {} 852616e45dfSDong Aisheng 85393abe8e4SViresh Kumar static inline void devm_clk_put(struct device *dev, struct clk *clk) {} 85493abe8e4SViresh Kumar 85555e9b8b7SJerome Brunet 85655e9b8b7SJerome Brunet static inline int clk_rate_exclusive_get(struct clk *clk) 85755e9b8b7SJerome Brunet { 85855e9b8b7SJerome Brunet return 0; 85955e9b8b7SJerome Brunet } 86055e9b8b7SJerome Brunet 86155e9b8b7SJerome Brunet static inline void clk_rate_exclusive_put(struct clk *clk) {} 86255e9b8b7SJerome Brunet 86393abe8e4SViresh Kumar static inline int clk_enable(struct clk *clk) 86493abe8e4SViresh Kumar { 86593abe8e4SViresh Kumar return 0; 86693abe8e4SViresh Kumar } 86793abe8e4SViresh Kumar 868570aaec7SAndrey Smirnov static inline int __must_check clk_bulk_enable(int num_clks, 869570aaec7SAndrey Smirnov const struct clk_bulk_data *clks) 870266e4e9dSDong Aisheng { 871266e4e9dSDong Aisheng return 0; 872266e4e9dSDong Aisheng } 873266e4e9dSDong Aisheng 87493abe8e4SViresh Kumar static inline void clk_disable(struct clk *clk) {} 87593abe8e4SViresh Kumar 876266e4e9dSDong Aisheng 877266e4e9dSDong Aisheng static inline void clk_bulk_disable(int num_clks, 878570aaec7SAndrey Smirnov const struct clk_bulk_data *clks) {} 879266e4e9dSDong Aisheng 88093abe8e4SViresh Kumar static inline unsigned long clk_get_rate(struct clk *clk) 88193abe8e4SViresh Kumar { 88293abe8e4SViresh Kumar return 0; 88393abe8e4SViresh Kumar } 88493abe8e4SViresh Kumar 88593abe8e4SViresh Kumar static inline int clk_set_rate(struct clk *clk, unsigned long rate) 88693abe8e4SViresh Kumar { 88793abe8e4SViresh Kumar return 0; 88893abe8e4SViresh Kumar } 88993abe8e4SViresh Kumar 89055e9b8b7SJerome Brunet static inline int clk_set_rate_exclusive(struct clk *clk, unsigned long rate) 89155e9b8b7SJerome Brunet { 89255e9b8b7SJerome Brunet return 0; 89355e9b8b7SJerome Brunet } 89455e9b8b7SJerome Brunet 89593abe8e4SViresh Kumar static inline long clk_round_rate(struct clk *clk, unsigned long rate) 89693abe8e4SViresh Kumar { 89793abe8e4SViresh Kumar return 0; 89893abe8e4SViresh Kumar } 89993abe8e4SViresh Kumar 9004e88f3deSThierry Reding static inline bool clk_has_parent(struct clk *clk, struct clk *parent) 9014e88f3deSThierry Reding { 9024e88f3deSThierry Reding return true; 9034e88f3deSThierry Reding } 9044e88f3deSThierry Reding 905b88c9f41SDmitry Osipenko static inline int clk_set_rate_range(struct clk *clk, unsigned long min, 906b88c9f41SDmitry Osipenko unsigned long max) 907b88c9f41SDmitry Osipenko { 908b88c9f41SDmitry Osipenko return 0; 909b88c9f41SDmitry Osipenko } 910b88c9f41SDmitry Osipenko 911b88c9f41SDmitry Osipenko static inline int clk_set_min_rate(struct clk *clk, unsigned long rate) 912b88c9f41SDmitry Osipenko { 913b88c9f41SDmitry Osipenko return 0; 914b88c9f41SDmitry Osipenko } 915b88c9f41SDmitry Osipenko 916b88c9f41SDmitry Osipenko static inline int clk_set_max_rate(struct clk *clk, unsigned long rate) 917b88c9f41SDmitry Osipenko { 918b88c9f41SDmitry Osipenko return 0; 919b88c9f41SDmitry Osipenko } 920b88c9f41SDmitry Osipenko 92193abe8e4SViresh Kumar static inline int clk_set_parent(struct clk *clk, struct clk *parent) 92293abe8e4SViresh Kumar { 92393abe8e4SViresh Kumar return 0; 92493abe8e4SViresh Kumar } 92593abe8e4SViresh Kumar 92693abe8e4SViresh Kumar static inline struct clk *clk_get_parent(struct clk *clk) 92793abe8e4SViresh Kumar { 92893abe8e4SViresh Kumar return NULL; 92993abe8e4SViresh Kumar } 93093abe8e4SViresh Kumar 931b81ea968SDaniel Lezcano static inline struct clk *clk_get_sys(const char *dev_id, const char *con_id) 932b81ea968SDaniel Lezcano { 933b81ea968SDaniel Lezcano return NULL; 934b81ea968SDaniel Lezcano } 9358b95d1ceSRuss Dill 9368b95d1ceSRuss Dill static inline int clk_save_context(void) 9378b95d1ceSRuss Dill { 9388b95d1ceSRuss Dill return 0; 9398b95d1ceSRuss Dill } 9408b95d1ceSRuss Dill 9418b95d1ceSRuss Dill static inline void clk_restore_context(void) {} 9428b95d1ceSRuss Dill 94393abe8e4SViresh Kumar #endif 94493abe8e4SViresh Kumar 94593abe8e4SViresh Kumar /* clk_prepare_enable helps cases using clk_enable in non-atomic context. */ 94693abe8e4SViresh Kumar static inline int clk_prepare_enable(struct clk *clk) 94793abe8e4SViresh Kumar { 94893abe8e4SViresh Kumar int ret; 94993abe8e4SViresh Kumar 95093abe8e4SViresh Kumar ret = clk_prepare(clk); 95193abe8e4SViresh Kumar if (ret) 95293abe8e4SViresh Kumar return ret; 95393abe8e4SViresh Kumar ret = clk_enable(clk); 95493abe8e4SViresh Kumar if (ret) 95593abe8e4SViresh Kumar clk_unprepare(clk); 95693abe8e4SViresh Kumar 95793abe8e4SViresh Kumar return ret; 95893abe8e4SViresh Kumar } 95993abe8e4SViresh Kumar 96093abe8e4SViresh Kumar /* clk_disable_unprepare helps cases using clk_disable in non-atomic context. */ 96193abe8e4SViresh Kumar static inline void clk_disable_unprepare(struct clk *clk) 96293abe8e4SViresh Kumar { 96393abe8e4SViresh Kumar clk_disable(clk); 96493abe8e4SViresh Kumar clk_unprepare(clk); 96593abe8e4SViresh Kumar } 96693abe8e4SViresh Kumar 967570aaec7SAndrey Smirnov static inline int __must_check 968570aaec7SAndrey Smirnov clk_bulk_prepare_enable(int num_clks, const struct clk_bulk_data *clks) 9693c48d86cSBjorn Andersson { 9703c48d86cSBjorn Andersson int ret; 9713c48d86cSBjorn Andersson 9723c48d86cSBjorn Andersson ret = clk_bulk_prepare(num_clks, clks); 9733c48d86cSBjorn Andersson if (ret) 9743c48d86cSBjorn Andersson return ret; 9753c48d86cSBjorn Andersson ret = clk_bulk_enable(num_clks, clks); 9763c48d86cSBjorn Andersson if (ret) 9773c48d86cSBjorn Andersson clk_bulk_unprepare(num_clks, clks); 9783c48d86cSBjorn Andersson 9793c48d86cSBjorn Andersson return ret; 9803c48d86cSBjorn Andersson } 9813c48d86cSBjorn Andersson 9823c48d86cSBjorn Andersson static inline void clk_bulk_disable_unprepare(int num_clks, 983570aaec7SAndrey Smirnov const struct clk_bulk_data *clks) 9843c48d86cSBjorn Andersson { 9853c48d86cSBjorn Andersson clk_bulk_disable(num_clks, clks); 9863c48d86cSBjorn Andersson clk_bulk_unprepare(num_clks, clks); 9873c48d86cSBjorn Andersson } 9883c48d86cSBjorn Andersson 98960b8f0ddSPhil Edworthy /** 990*c9744843SMaxime Ripard * clk_drop_range - Reset any range set on that clock 991*c9744843SMaxime Ripard * @clk: clock source 992*c9744843SMaxime Ripard * 993*c9744843SMaxime Ripard * Returns success (0) or negative errno. 994*c9744843SMaxime Ripard */ 995*c9744843SMaxime Ripard static inline int clk_drop_range(struct clk *clk) 996*c9744843SMaxime Ripard { 997*c9744843SMaxime Ripard return clk_set_rate_range(clk, 0, ULONG_MAX); 998*c9744843SMaxime Ripard } 999*c9744843SMaxime Ripard 1000*c9744843SMaxime Ripard /** 100160b8f0ddSPhil Edworthy * clk_get_optional - lookup and obtain a reference to an optional clock 100260b8f0ddSPhil Edworthy * producer. 100360b8f0ddSPhil Edworthy * @dev: device for clock "consumer" 100460b8f0ddSPhil Edworthy * @id: clock consumer ID 100560b8f0ddSPhil Edworthy * 100660b8f0ddSPhil Edworthy * Behaves the same as clk_get() except where there is no clock producer. In 100760b8f0ddSPhil Edworthy * this case, instead of returning -ENOENT, the function returns NULL. 100860b8f0ddSPhil Edworthy */ 100960b8f0ddSPhil Edworthy static inline struct clk *clk_get_optional(struct device *dev, const char *id) 101060b8f0ddSPhil Edworthy { 101160b8f0ddSPhil Edworthy struct clk *clk = clk_get(dev, id); 101260b8f0ddSPhil Edworthy 101360b8f0ddSPhil Edworthy if (clk == ERR_PTR(-ENOENT)) 101460b8f0ddSPhil Edworthy return NULL; 101560b8f0ddSPhil Edworthy 101660b8f0ddSPhil Edworthy return clk; 101760b8f0ddSPhil Edworthy } 101860b8f0ddSPhil Edworthy 1019137f8a72SRob Herring #if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) 1020766e6a4eSGrant Likely struct clk *of_clk_get(struct device_node *np, int index); 1021766e6a4eSGrant Likely struct clk *of_clk_get_by_name(struct device_node *np, const char *name); 1022766e6a4eSGrant Likely struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec); 1023766e6a4eSGrant Likely #else 1024766e6a4eSGrant Likely static inline struct clk *of_clk_get(struct device_node *np, int index) 1025766e6a4eSGrant Likely { 10269f1612d3SShawn Guo return ERR_PTR(-ENOENT); 1027766e6a4eSGrant Likely } 1028766e6a4eSGrant Likely static inline struct clk *of_clk_get_by_name(struct device_node *np, 1029766e6a4eSGrant Likely const char *name) 1030766e6a4eSGrant Likely { 10319f1612d3SShawn Guo return ERR_PTR(-ENOENT); 1032766e6a4eSGrant Likely } 1033428c9de5SGeert Uytterhoeven static inline struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec) 1034428c9de5SGeert Uytterhoeven { 1035428c9de5SGeert Uytterhoeven return ERR_PTR(-ENOENT); 1036428c9de5SGeert Uytterhoeven } 1037766e6a4eSGrant Likely #endif 1038766e6a4eSGrant Likely 1039f8ce2547SRussell King #endif 1040