1f8ce2547SRussell King /* 2f8ce2547SRussell King * linux/include/linux/clk.h 3f8ce2547SRussell King * 4f8ce2547SRussell King * Copyright (C) 2004 ARM Limited. 5f8ce2547SRussell King * Written by Deep Blue Solutions Limited. 6b2476490SMike Turquette * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org> 7f8ce2547SRussell King * 8f8ce2547SRussell King * This program is free software; you can redistribute it and/or modify 9f8ce2547SRussell King * it under the terms of the GNU General Public License version 2 as 10f8ce2547SRussell King * published by the Free Software Foundation. 11f8ce2547SRussell King */ 12686f8c5dSTodd Poynor #ifndef __LINUX_CLK_H 13686f8c5dSTodd Poynor #define __LINUX_CLK_H 14f8ce2547SRussell King 159f1612d3SShawn Guo #include <linux/err.h> 1640d3e0f4SRussell King #include <linux/kernel.h> 17b2476490SMike Turquette #include <linux/notifier.h> 1840d3e0f4SRussell King 19f8ce2547SRussell King struct device; 20f8ce2547SRussell King struct clk; 2171a2f115SKuninori Morimoto struct device_node; 2271a2f115SKuninori Morimoto struct of_phandle_args; 23f8ce2547SRussell King 24b2476490SMike Turquette /** 25b2476490SMike Turquette * DOC: clk notifier callback types 26b2476490SMike Turquette * 27b2476490SMike Turquette * PRE_RATE_CHANGE - called immediately before the clk rate is changed, 28b2476490SMike Turquette * to indicate that the rate change will proceed. Drivers must 29b2476490SMike Turquette * immediately terminate any operations that will be affected by the 30fb72a059SSoren Brinkmann * rate change. Callbacks may either return NOTIFY_DONE, NOTIFY_OK, 31fb72a059SSoren Brinkmann * NOTIFY_STOP or NOTIFY_BAD. 32b2476490SMike Turquette * 33b2476490SMike Turquette * ABORT_RATE_CHANGE: called if the rate change failed for some reason 34b2476490SMike Turquette * after PRE_RATE_CHANGE. In this case, all registered notifiers on 35b2476490SMike Turquette * the clk will be called with ABORT_RATE_CHANGE. Callbacks must 36fb72a059SSoren Brinkmann * always return NOTIFY_DONE or NOTIFY_OK. 37b2476490SMike Turquette * 38b2476490SMike Turquette * POST_RATE_CHANGE - called after the clk rate change has successfully 39fb72a059SSoren Brinkmann * completed. Callbacks must always return NOTIFY_DONE or NOTIFY_OK. 40b2476490SMike Turquette * 41b2476490SMike Turquette */ 42b2476490SMike Turquette #define PRE_RATE_CHANGE BIT(0) 43b2476490SMike Turquette #define POST_RATE_CHANGE BIT(1) 44b2476490SMike Turquette #define ABORT_RATE_CHANGE BIT(2) 45b2476490SMike Turquette 46b2476490SMike Turquette /** 47b2476490SMike Turquette * struct clk_notifier - associate a clk with a notifier 48b2476490SMike Turquette * @clk: struct clk * to associate the notifier with 49b2476490SMike Turquette * @notifier_head: a blocking_notifier_head for this clk 50b2476490SMike Turquette * @node: linked list pointers 51b2476490SMike Turquette * 52b2476490SMike Turquette * A list of struct clk_notifier is maintained by the notifier code. 53b2476490SMike Turquette * An entry is created whenever code registers the first notifier on a 54b2476490SMike Turquette * particular @clk. Future notifiers on that @clk are added to the 55b2476490SMike Turquette * @notifier_head. 56b2476490SMike Turquette */ 57b2476490SMike Turquette struct clk_notifier { 58b2476490SMike Turquette struct clk *clk; 59b2476490SMike Turquette struct srcu_notifier_head notifier_head; 60b2476490SMike Turquette struct list_head node; 61b2476490SMike Turquette }; 62b2476490SMike Turquette 63b2476490SMike Turquette /** 64b2476490SMike Turquette * struct clk_notifier_data - rate data to pass to the notifier callback 65b2476490SMike Turquette * @clk: struct clk * being changed 66b2476490SMike Turquette * @old_rate: previous rate of this clk 67b2476490SMike Turquette * @new_rate: new rate of this clk 68b2476490SMike Turquette * 69b2476490SMike Turquette * For a pre-notifier, old_rate is the clk's rate before this rate 70b2476490SMike Turquette * change, and new_rate is what the rate will be in the future. For a 71b2476490SMike Turquette * post-notifier, old_rate and new_rate are both set to the clk's 72b2476490SMike Turquette * current rate (this was done to optimize the implementation). 73b2476490SMike Turquette */ 74b2476490SMike Turquette struct clk_notifier_data { 75b2476490SMike Turquette struct clk *clk; 76b2476490SMike Turquette unsigned long old_rate; 77b2476490SMike Turquette unsigned long new_rate; 78b2476490SMike Turquette }; 79b2476490SMike Turquette 80266e4e9dSDong Aisheng /** 81266e4e9dSDong Aisheng * struct clk_bulk_data - Data used for bulk clk operations. 82266e4e9dSDong Aisheng * 83266e4e9dSDong Aisheng * @id: clock consumer ID 84266e4e9dSDong Aisheng * @clk: struct clk * to store the associated clock 85266e4e9dSDong Aisheng * 86266e4e9dSDong Aisheng * The CLK APIs provide a series of clk_bulk_() API calls as 87266e4e9dSDong Aisheng * a convenience to consumers which require multiple clks. This 88266e4e9dSDong Aisheng * structure is used to manage data for these calls. 89266e4e9dSDong Aisheng */ 90266e4e9dSDong Aisheng struct clk_bulk_data { 91266e4e9dSDong Aisheng const char *id; 92266e4e9dSDong Aisheng struct clk *clk; 93266e4e9dSDong Aisheng }; 94266e4e9dSDong Aisheng 95e81b87d2SKrzysztof Kozlowski #ifdef CONFIG_COMMON_CLK 96e81b87d2SKrzysztof Kozlowski 9786bcfa2eSMike Turquette /** 9886bcfa2eSMike Turquette * clk_notifier_register: register a clock rate-change notifier callback 9986bcfa2eSMike Turquette * @clk: clock whose rate we are interested in 10086bcfa2eSMike Turquette * @nb: notifier block with callback function pointer 10186bcfa2eSMike Turquette * 10286bcfa2eSMike Turquette * ProTip: debugging across notifier chains can be frustrating. Make sure that 10386bcfa2eSMike Turquette * your notifier callback function prints a nice big warning in case of 10486bcfa2eSMike Turquette * failure. 10586bcfa2eSMike Turquette */ 106b2476490SMike Turquette int clk_notifier_register(struct clk *clk, struct notifier_block *nb); 107b2476490SMike Turquette 10886bcfa2eSMike Turquette /** 10986bcfa2eSMike Turquette * clk_notifier_unregister: unregister a clock rate-change notifier callback 11086bcfa2eSMike Turquette * @clk: clock whose rate we are no longer interested in 11186bcfa2eSMike Turquette * @nb: notifier block which will be unregistered 11286bcfa2eSMike Turquette */ 113b2476490SMike Turquette int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb); 114b2476490SMike Turquette 1155279fc40SBoris BREZILLON /** 1165279fc40SBoris BREZILLON * clk_get_accuracy - obtain the clock accuracy in ppb (parts per billion) 1175279fc40SBoris BREZILLON * for a clock source. 1185279fc40SBoris BREZILLON * @clk: clock source 1195279fc40SBoris BREZILLON * 1205279fc40SBoris BREZILLON * This gets the clock source accuracy expressed in ppb. 1215279fc40SBoris BREZILLON * A perfect clock returns 0. 1225279fc40SBoris BREZILLON */ 1235279fc40SBoris BREZILLON long clk_get_accuracy(struct clk *clk); 1245279fc40SBoris BREZILLON 125e59c5371SMike Turquette /** 126e59c5371SMike Turquette * clk_set_phase - adjust the phase shift of a clock signal 127e59c5371SMike Turquette * @clk: clock signal source 128e59c5371SMike Turquette * @degrees: number of degrees the signal is shifted 129e59c5371SMike Turquette * 130e59c5371SMike Turquette * Shifts the phase of a clock signal by the specified degrees. Returns 0 on 131e59c5371SMike Turquette * success, -EERROR otherwise. 132e59c5371SMike Turquette */ 133e59c5371SMike Turquette int clk_set_phase(struct clk *clk, int degrees); 134e59c5371SMike Turquette 135e59c5371SMike Turquette /** 136e59c5371SMike Turquette * clk_get_phase - return the phase shift of a clock signal 137e59c5371SMike Turquette * @clk: clock signal source 138e59c5371SMike Turquette * 139e59c5371SMike Turquette * Returns the phase shift of a clock node in degrees, otherwise returns 140e59c5371SMike Turquette * -EERROR. 141e59c5371SMike Turquette */ 142e59c5371SMike Turquette int clk_get_phase(struct clk *clk); 143e59c5371SMike Turquette 1443d3801efSMichael Turquette /** 1453d3801efSMichael Turquette * clk_is_match - check if two clk's point to the same hardware clock 1463d3801efSMichael Turquette * @p: clk compared against q 1473d3801efSMichael Turquette * @q: clk compared against p 1483d3801efSMichael Turquette * 1493d3801efSMichael Turquette * Returns true if the two struct clk pointers both point to the same hardware 1500e056eb5Smchehab@s-opensource.com * clock node. Put differently, returns true if @p and @q 1510e056eb5Smchehab@s-opensource.com * share the same &struct clk_core object. 1523d3801efSMichael Turquette * 1533d3801efSMichael Turquette * Returns false otherwise. Note that two NULL clks are treated as matching. 1543d3801efSMichael Turquette */ 1553d3801efSMichael Turquette bool clk_is_match(const struct clk *p, const struct clk *q); 1563d3801efSMichael Turquette 1575279fc40SBoris BREZILLON #else 1585279fc40SBoris BREZILLON 159e81b87d2SKrzysztof Kozlowski static inline int clk_notifier_register(struct clk *clk, 160e81b87d2SKrzysztof Kozlowski struct notifier_block *nb) 161e81b87d2SKrzysztof Kozlowski { 162e81b87d2SKrzysztof Kozlowski return -ENOTSUPP; 163e81b87d2SKrzysztof Kozlowski } 164e81b87d2SKrzysztof Kozlowski 165e81b87d2SKrzysztof Kozlowski static inline int clk_notifier_unregister(struct clk *clk, 166e81b87d2SKrzysztof Kozlowski struct notifier_block *nb) 167e81b87d2SKrzysztof Kozlowski { 168e81b87d2SKrzysztof Kozlowski return -ENOTSUPP; 169e81b87d2SKrzysztof Kozlowski } 170e81b87d2SKrzysztof Kozlowski 1715279fc40SBoris BREZILLON static inline long clk_get_accuracy(struct clk *clk) 1725279fc40SBoris BREZILLON { 1735279fc40SBoris BREZILLON return -ENOTSUPP; 1745279fc40SBoris BREZILLON } 1755279fc40SBoris BREZILLON 176e59c5371SMike Turquette static inline long clk_set_phase(struct clk *clk, int phase) 177e59c5371SMike Turquette { 178e59c5371SMike Turquette return -ENOTSUPP; 179e59c5371SMike Turquette } 180e59c5371SMike Turquette 181e59c5371SMike Turquette static inline long clk_get_phase(struct clk *clk) 182e59c5371SMike Turquette { 183e59c5371SMike Turquette return -ENOTSUPP; 184e59c5371SMike Turquette } 185e59c5371SMike Turquette 1863d3801efSMichael Turquette static inline bool clk_is_match(const struct clk *p, const struct clk *q) 1873d3801efSMichael Turquette { 1883d3801efSMichael Turquette return p == q; 1893d3801efSMichael Turquette } 1903d3801efSMichael Turquette 1917e87aed9SMark Brown #endif 192b2476490SMike Turquette 193f8ce2547SRussell King /** 19493abe8e4SViresh Kumar * clk_prepare - prepare a clock source 19593abe8e4SViresh Kumar * @clk: clock source 19693abe8e4SViresh Kumar * 19793abe8e4SViresh Kumar * This prepares the clock source for use. 19893abe8e4SViresh Kumar * 19993abe8e4SViresh Kumar * Must not be called from within atomic context. 20093abe8e4SViresh Kumar */ 20193abe8e4SViresh Kumar #ifdef CONFIG_HAVE_CLK_PREPARE 20293abe8e4SViresh Kumar int clk_prepare(struct clk *clk); 203266e4e9dSDong Aisheng int __must_check clk_bulk_prepare(int num_clks, 204266e4e9dSDong Aisheng const struct clk_bulk_data *clks); 20593abe8e4SViresh Kumar #else 20693abe8e4SViresh Kumar static inline int clk_prepare(struct clk *clk) 20793abe8e4SViresh Kumar { 20893abe8e4SViresh Kumar might_sleep(); 20993abe8e4SViresh Kumar return 0; 21093abe8e4SViresh Kumar } 211266e4e9dSDong Aisheng 212266e4e9dSDong Aisheng static inline int clk_bulk_prepare(int num_clks, struct clk_bulk_data *clks) 213266e4e9dSDong Aisheng { 214266e4e9dSDong Aisheng might_sleep(); 215266e4e9dSDong Aisheng return 0; 216266e4e9dSDong Aisheng } 21793abe8e4SViresh Kumar #endif 21893abe8e4SViresh Kumar 21993abe8e4SViresh Kumar /** 22093abe8e4SViresh Kumar * clk_unprepare - undo preparation of a clock source 22193abe8e4SViresh Kumar * @clk: clock source 22293abe8e4SViresh Kumar * 22393abe8e4SViresh Kumar * This undoes a previously prepared clock. The caller must balance 22493abe8e4SViresh Kumar * the number of prepare and unprepare calls. 22593abe8e4SViresh Kumar * 22693abe8e4SViresh Kumar * Must not be called from within atomic context. 22793abe8e4SViresh Kumar */ 22893abe8e4SViresh Kumar #ifdef CONFIG_HAVE_CLK_PREPARE 22993abe8e4SViresh Kumar void clk_unprepare(struct clk *clk); 230266e4e9dSDong Aisheng void clk_bulk_unprepare(int num_clks, const struct clk_bulk_data *clks); 23193abe8e4SViresh Kumar #else 23293abe8e4SViresh Kumar static inline void clk_unprepare(struct clk *clk) 23393abe8e4SViresh Kumar { 23493abe8e4SViresh Kumar might_sleep(); 23593abe8e4SViresh Kumar } 236266e4e9dSDong Aisheng static inline void clk_bulk_unprepare(int num_clks, struct clk_bulk_data *clks) 237266e4e9dSDong Aisheng { 238266e4e9dSDong Aisheng might_sleep(); 239266e4e9dSDong Aisheng } 24093abe8e4SViresh Kumar #endif 24193abe8e4SViresh Kumar 24293abe8e4SViresh Kumar #ifdef CONFIG_HAVE_CLK 24393abe8e4SViresh Kumar /** 244f8ce2547SRussell King * clk_get - lookup and obtain a reference to a clock producer. 245f8ce2547SRussell King * @dev: device for clock "consumer" 246a58b3a4aSJan-Simon Möller * @id: clock consumer ID 247f8ce2547SRussell King * 248f8ce2547SRussell King * Returns a struct clk corresponding to the clock producer, or 249f8ce2547SRussell King * valid IS_ERR() condition containing errno. The implementation 250f8ce2547SRussell King * uses @dev and @id to determine the clock consumer, and thereby 251f8ce2547SRussell King * the clock producer. (IOW, @id may be identical strings, but 252f8ce2547SRussell King * clk_get may return different clock producers depending on @dev.) 253f8ce2547SRussell King * 254f8ce2547SRussell King * Drivers must assume that the clock source is not enabled. 255f7ad160bSAlex Raimondi * 256f7ad160bSAlex Raimondi * clk_get should not be called from within interrupt context. 257f8ce2547SRussell King */ 258f8ce2547SRussell King struct clk *clk_get(struct device *dev, const char *id); 259f8ce2547SRussell King 260f8ce2547SRussell King /** 261266e4e9dSDong Aisheng * clk_bulk_get - lookup and obtain a number of references to clock producer. 262266e4e9dSDong Aisheng * @dev: device for clock "consumer" 263266e4e9dSDong Aisheng * @num_clks: the number of clk_bulk_data 264266e4e9dSDong Aisheng * @clks: the clk_bulk_data table of consumer 265266e4e9dSDong Aisheng * 266266e4e9dSDong Aisheng * This helper function allows drivers to get several clk consumers in one 267266e4e9dSDong Aisheng * operation. If any of the clk cannot be acquired then any clks 268266e4e9dSDong Aisheng * that were obtained will be freed before returning to the caller. 269266e4e9dSDong Aisheng * 270266e4e9dSDong Aisheng * Returns 0 if all clocks specified in clk_bulk_data table are obtained 271266e4e9dSDong Aisheng * successfully, or valid IS_ERR() condition containing errno. 272266e4e9dSDong Aisheng * The implementation uses @dev and @clk_bulk_data.id to determine the 273266e4e9dSDong Aisheng * clock consumer, and thereby the clock producer. 274266e4e9dSDong Aisheng * The clock returned is stored in each @clk_bulk_data.clk field. 275266e4e9dSDong Aisheng * 276266e4e9dSDong Aisheng * Drivers must assume that the clock source is not enabled. 277266e4e9dSDong Aisheng * 278266e4e9dSDong Aisheng * clk_bulk_get should not be called from within interrupt context. 279266e4e9dSDong Aisheng */ 280266e4e9dSDong Aisheng int __must_check clk_bulk_get(struct device *dev, int num_clks, 281266e4e9dSDong Aisheng struct clk_bulk_data *clks); 282266e4e9dSDong Aisheng 283266e4e9dSDong Aisheng /** 284*618aee02SDong Aisheng * devm_clk_bulk_get - managed get multiple clk consumers 285*618aee02SDong Aisheng * @dev: device for clock "consumer" 286*618aee02SDong Aisheng * @num_clks: the number of clk_bulk_data 287*618aee02SDong Aisheng * @clks: the clk_bulk_data table of consumer 288*618aee02SDong Aisheng * 289*618aee02SDong Aisheng * Return 0 on success, an errno on failure. 290*618aee02SDong Aisheng * 291*618aee02SDong Aisheng * This helper function allows drivers to get several clk 292*618aee02SDong Aisheng * consumers in one operation with management, the clks will 293*618aee02SDong Aisheng * automatically be freed when the device is unbound. 294*618aee02SDong Aisheng */ 295*618aee02SDong Aisheng int __must_check devm_clk_bulk_get(struct device *dev, int num_clks, 296*618aee02SDong Aisheng struct clk_bulk_data *clks); 297*618aee02SDong Aisheng 298*618aee02SDong Aisheng /** 299a8a97db9SMark Brown * devm_clk_get - lookup and obtain a managed reference to a clock producer. 300a8a97db9SMark Brown * @dev: device for clock "consumer" 301a58b3a4aSJan-Simon Möller * @id: clock consumer ID 302a8a97db9SMark Brown * 303a8a97db9SMark Brown * Returns a struct clk corresponding to the clock producer, or 304a8a97db9SMark Brown * valid IS_ERR() condition containing errno. The implementation 305a8a97db9SMark Brown * uses @dev and @id to determine the clock consumer, and thereby 306a8a97db9SMark Brown * the clock producer. (IOW, @id may be identical strings, but 307a8a97db9SMark Brown * clk_get may return different clock producers depending on @dev.) 308a8a97db9SMark Brown * 309a8a97db9SMark Brown * Drivers must assume that the clock source is not enabled. 310a8a97db9SMark Brown * 311a8a97db9SMark Brown * devm_clk_get should not be called from within interrupt context. 312a8a97db9SMark Brown * 313a8a97db9SMark Brown * The clock will automatically be freed when the device is unbound 314a8a97db9SMark Brown * from the bus. 315a8a97db9SMark Brown */ 316a8a97db9SMark Brown struct clk *devm_clk_get(struct device *dev, const char *id); 317a8a97db9SMark Brown 318a8a97db9SMark Brown /** 31971a2f115SKuninori Morimoto * devm_get_clk_from_child - lookup and obtain a managed reference to a 32071a2f115SKuninori Morimoto * clock producer from child node. 32171a2f115SKuninori Morimoto * @dev: device for clock "consumer" 32271a2f115SKuninori Morimoto * @np: pointer to clock consumer node 32371a2f115SKuninori Morimoto * @con_id: clock consumer ID 32471a2f115SKuninori Morimoto * 32571a2f115SKuninori Morimoto * This function parses the clocks, and uses them to look up the 32671a2f115SKuninori Morimoto * struct clk from the registered list of clock providers by using 32771a2f115SKuninori Morimoto * @np and @con_id 32871a2f115SKuninori Morimoto * 32971a2f115SKuninori Morimoto * The clock will automatically be freed when the device is unbound 33071a2f115SKuninori Morimoto * from the bus. 33171a2f115SKuninori Morimoto */ 33271a2f115SKuninori Morimoto struct clk *devm_get_clk_from_child(struct device *dev, 33371a2f115SKuninori Morimoto struct device_node *np, const char *con_id); 33471a2f115SKuninori Morimoto 33571a2f115SKuninori Morimoto /** 336f8ce2547SRussell King * clk_enable - inform the system when the clock source should be running. 337f8ce2547SRussell King * @clk: clock source 338f8ce2547SRussell King * 339f8ce2547SRussell King * If the clock can not be enabled/disabled, this should return success. 340f8ce2547SRussell King * 34140d3e0f4SRussell King * May be called from atomic contexts. 34240d3e0f4SRussell King * 343f8ce2547SRussell King * Returns success (0) or negative errno. 344f8ce2547SRussell King */ 345f8ce2547SRussell King int clk_enable(struct clk *clk); 346f8ce2547SRussell King 347f8ce2547SRussell King /** 348266e4e9dSDong Aisheng * clk_bulk_enable - inform the system when the set of clks should be running. 349266e4e9dSDong Aisheng * @num_clks: the number of clk_bulk_data 350266e4e9dSDong Aisheng * @clks: the clk_bulk_data table of consumer 351266e4e9dSDong Aisheng * 352266e4e9dSDong Aisheng * May be called from atomic contexts. 353266e4e9dSDong Aisheng * 354266e4e9dSDong Aisheng * Returns success (0) or negative errno. 355266e4e9dSDong Aisheng */ 356266e4e9dSDong Aisheng int __must_check clk_bulk_enable(int num_clks, 357266e4e9dSDong Aisheng const struct clk_bulk_data *clks); 358266e4e9dSDong Aisheng 359266e4e9dSDong Aisheng /** 360f8ce2547SRussell King * clk_disable - inform the system when the clock source is no longer required. 361f8ce2547SRussell King * @clk: clock source 362f8ce2547SRussell King * 363f8ce2547SRussell King * Inform the system that a clock source is no longer required by 364f8ce2547SRussell King * a driver and may be shut down. 365f8ce2547SRussell King * 36640d3e0f4SRussell King * May be called from atomic contexts. 36740d3e0f4SRussell King * 368f8ce2547SRussell King * Implementation detail: if the clock source is shared between 369f8ce2547SRussell King * multiple drivers, clk_enable() calls must be balanced by the 370f8ce2547SRussell King * same number of clk_disable() calls for the clock source to be 371f8ce2547SRussell King * disabled. 372f8ce2547SRussell King */ 373f8ce2547SRussell King void clk_disable(struct clk *clk); 374f8ce2547SRussell King 375f8ce2547SRussell King /** 376266e4e9dSDong Aisheng * clk_bulk_disable - inform the system when the set of clks is no 377266e4e9dSDong Aisheng * longer required. 378266e4e9dSDong Aisheng * @num_clks: the number of clk_bulk_data 379266e4e9dSDong Aisheng * @clks: the clk_bulk_data table of consumer 380266e4e9dSDong Aisheng * 381266e4e9dSDong Aisheng * Inform the system that a set of clks is no longer required by 382266e4e9dSDong Aisheng * a driver and may be shut down. 383266e4e9dSDong Aisheng * 384266e4e9dSDong Aisheng * May be called from atomic contexts. 385266e4e9dSDong Aisheng * 386266e4e9dSDong Aisheng * Implementation detail: if the set of clks is shared between 387266e4e9dSDong Aisheng * multiple drivers, clk_bulk_enable() calls must be balanced by the 388266e4e9dSDong Aisheng * same number of clk_bulk_disable() calls for the clock source to be 389266e4e9dSDong Aisheng * disabled. 390266e4e9dSDong Aisheng */ 391266e4e9dSDong Aisheng void clk_bulk_disable(int num_clks, const struct clk_bulk_data *clks); 392266e4e9dSDong Aisheng 393266e4e9dSDong Aisheng /** 394f8ce2547SRussell King * clk_get_rate - obtain the current clock rate (in Hz) for a clock source. 395f8ce2547SRussell King * This is only valid once the clock source has been enabled. 396f8ce2547SRussell King * @clk: clock source 397f8ce2547SRussell King */ 398f8ce2547SRussell King unsigned long clk_get_rate(struct clk *clk); 399f8ce2547SRussell King 400f8ce2547SRussell King /** 401f8ce2547SRussell King * clk_put - "free" the clock source 402f8ce2547SRussell King * @clk: clock source 403f8ce2547SRussell King * 404f8ce2547SRussell King * Note: drivers must ensure that all clk_enable calls made on this 405f8ce2547SRussell King * clock source are balanced by clk_disable calls prior to calling 406f8ce2547SRussell King * this function. 407f7ad160bSAlex Raimondi * 408f7ad160bSAlex Raimondi * clk_put should not be called from within interrupt context. 409f8ce2547SRussell King */ 410f8ce2547SRussell King void clk_put(struct clk *clk); 411f8ce2547SRussell King 412a8a97db9SMark Brown /** 413266e4e9dSDong Aisheng * clk_bulk_put - "free" the clock source 414266e4e9dSDong Aisheng * @num_clks: the number of clk_bulk_data 415266e4e9dSDong Aisheng * @clks: the clk_bulk_data table of consumer 416266e4e9dSDong Aisheng * 417266e4e9dSDong Aisheng * Note: drivers must ensure that all clk_bulk_enable calls made on this 418266e4e9dSDong Aisheng * clock source are balanced by clk_bulk_disable calls prior to calling 419266e4e9dSDong Aisheng * this function. 420266e4e9dSDong Aisheng * 421266e4e9dSDong Aisheng * clk_bulk_put should not be called from within interrupt context. 422266e4e9dSDong Aisheng */ 423266e4e9dSDong Aisheng void clk_bulk_put(int num_clks, struct clk_bulk_data *clks); 424266e4e9dSDong Aisheng 425266e4e9dSDong Aisheng /** 426a8a97db9SMark Brown * devm_clk_put - "free" a managed clock source 427da3dae54SMasanari Iida * @dev: device used to acquire the clock 428a8a97db9SMark Brown * @clk: clock source acquired with devm_clk_get() 429a8a97db9SMark Brown * 430a8a97db9SMark Brown * Note: drivers must ensure that all clk_enable calls made on this 431a8a97db9SMark Brown * clock source are balanced by clk_disable calls prior to calling 432a8a97db9SMark Brown * this function. 433a8a97db9SMark Brown * 434a8a97db9SMark Brown * clk_put should not be called from within interrupt context. 435a8a97db9SMark Brown */ 436a8a97db9SMark Brown void devm_clk_put(struct device *dev, struct clk *clk); 437f8ce2547SRussell King 438f8ce2547SRussell King /* 439f8ce2547SRussell King * The remaining APIs are optional for machine class support. 440f8ce2547SRussell King */ 441f8ce2547SRussell King 442f8ce2547SRussell King 443f8ce2547SRussell King /** 444f8ce2547SRussell King * clk_round_rate - adjust a rate to the exact rate a clock can provide 445f8ce2547SRussell King * @clk: clock source 446f8ce2547SRussell King * @rate: desired clock rate in Hz 447f8ce2547SRussell King * 448d2d14a77SRussell King * This answers the question "if I were to pass @rate to clk_set_rate(), 449d2d14a77SRussell King * what clock rate would I end up with?" without changing the hardware 450d2d14a77SRussell King * in any way. In other words: 451d2d14a77SRussell King * 452d2d14a77SRussell King * rate = clk_round_rate(clk, r); 453d2d14a77SRussell King * 454d2d14a77SRussell King * and: 455d2d14a77SRussell King * 456d2d14a77SRussell King * clk_set_rate(clk, r); 457d2d14a77SRussell King * rate = clk_get_rate(clk); 458d2d14a77SRussell King * 459d2d14a77SRussell King * are equivalent except the former does not modify the clock hardware 460d2d14a77SRussell King * in any way. 461d2d14a77SRussell King * 462f8ce2547SRussell King * Returns rounded clock rate in Hz, or negative errno. 463f8ce2547SRussell King */ 464f8ce2547SRussell King long clk_round_rate(struct clk *clk, unsigned long rate); 465f8ce2547SRussell King 466f8ce2547SRussell King /** 467f8ce2547SRussell King * clk_set_rate - set the clock rate for a clock source 468f8ce2547SRussell King * @clk: clock source 469f8ce2547SRussell King * @rate: desired clock rate in Hz 470f8ce2547SRussell King * 471f8ce2547SRussell King * Returns success (0) or negative errno. 472f8ce2547SRussell King */ 473f8ce2547SRussell King int clk_set_rate(struct clk *clk, unsigned long rate); 474f8ce2547SRussell King 475f8ce2547SRussell King /** 4764e88f3deSThierry Reding * clk_has_parent - check if a clock is a possible parent for another 4774e88f3deSThierry Reding * @clk: clock source 4784e88f3deSThierry Reding * @parent: parent clock source 4794e88f3deSThierry Reding * 4804e88f3deSThierry Reding * This function can be used in drivers that need to check that a clock can be 4814e88f3deSThierry Reding * the parent of another without actually changing the parent. 4824e88f3deSThierry Reding * 4834e88f3deSThierry Reding * Returns true if @parent is a possible parent for @clk, false otherwise. 4844e88f3deSThierry Reding */ 4854e88f3deSThierry Reding bool clk_has_parent(struct clk *clk, struct clk *parent); 4864e88f3deSThierry Reding 4874e88f3deSThierry Reding /** 4881c8e6004STomeu Vizoso * clk_set_rate_range - set a rate range for a clock source 4891c8e6004STomeu Vizoso * @clk: clock source 4901c8e6004STomeu Vizoso * @min: desired minimum clock rate in Hz, inclusive 4911c8e6004STomeu Vizoso * @max: desired maximum clock rate in Hz, inclusive 4921c8e6004STomeu Vizoso * 4931c8e6004STomeu Vizoso * Returns success (0) or negative errno. 4941c8e6004STomeu Vizoso */ 4951c8e6004STomeu Vizoso int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max); 4961c8e6004STomeu Vizoso 4971c8e6004STomeu Vizoso /** 4981c8e6004STomeu Vizoso * clk_set_min_rate - set a minimum clock rate for a clock source 4991c8e6004STomeu Vizoso * @clk: clock source 5001c8e6004STomeu Vizoso * @rate: desired minimum clock rate in Hz, inclusive 5011c8e6004STomeu Vizoso * 5021c8e6004STomeu Vizoso * Returns success (0) or negative errno. 5031c8e6004STomeu Vizoso */ 5041c8e6004STomeu Vizoso int clk_set_min_rate(struct clk *clk, unsigned long rate); 5051c8e6004STomeu Vizoso 5061c8e6004STomeu Vizoso /** 5071c8e6004STomeu Vizoso * clk_set_max_rate - set a maximum clock rate for a clock source 5081c8e6004STomeu Vizoso * @clk: clock source 5091c8e6004STomeu Vizoso * @rate: desired maximum clock rate in Hz, inclusive 5101c8e6004STomeu Vizoso * 5111c8e6004STomeu Vizoso * Returns success (0) or negative errno. 5121c8e6004STomeu Vizoso */ 5131c8e6004STomeu Vizoso int clk_set_max_rate(struct clk *clk, unsigned long rate); 5141c8e6004STomeu Vizoso 5151c8e6004STomeu Vizoso /** 516f8ce2547SRussell King * clk_set_parent - set the parent clock source for this clock 517f8ce2547SRussell King * @clk: clock source 518f8ce2547SRussell King * @parent: parent clock source 519f8ce2547SRussell King * 520f8ce2547SRussell King * Returns success (0) or negative errno. 521f8ce2547SRussell King */ 522f8ce2547SRussell King int clk_set_parent(struct clk *clk, struct clk *parent); 523f8ce2547SRussell King 524f8ce2547SRussell King /** 525f8ce2547SRussell King * clk_get_parent - get the parent clock source for this clock 526f8ce2547SRussell King * @clk: clock source 527f8ce2547SRussell King * 528f8ce2547SRussell King * Returns struct clk corresponding to parent clock source, or 529f8ce2547SRussell King * valid IS_ERR() condition containing errno. 530f8ce2547SRussell King */ 531f8ce2547SRussell King struct clk *clk_get_parent(struct clk *clk); 532f8ce2547SRussell King 53305fd8e73SSascha Hauer /** 53405fd8e73SSascha Hauer * clk_get_sys - get a clock based upon the device name 53505fd8e73SSascha Hauer * @dev_id: device name 53605fd8e73SSascha Hauer * @con_id: connection ID 53705fd8e73SSascha Hauer * 53805fd8e73SSascha Hauer * Returns a struct clk corresponding to the clock producer, or 53905fd8e73SSascha Hauer * valid IS_ERR() condition containing errno. The implementation 54005fd8e73SSascha Hauer * uses @dev_id and @con_id to determine the clock consumer, and 54105fd8e73SSascha Hauer * thereby the clock producer. In contrast to clk_get() this function 54205fd8e73SSascha Hauer * takes the device name instead of the device itself for identification. 54305fd8e73SSascha Hauer * 54405fd8e73SSascha Hauer * Drivers must assume that the clock source is not enabled. 54505fd8e73SSascha Hauer * 54605fd8e73SSascha Hauer * clk_get_sys should not be called from within interrupt context. 54705fd8e73SSascha Hauer */ 54805fd8e73SSascha Hauer struct clk *clk_get_sys(const char *dev_id, const char *con_id); 54905fd8e73SSascha Hauer 55093abe8e4SViresh Kumar #else /* !CONFIG_HAVE_CLK */ 55193abe8e4SViresh Kumar 55293abe8e4SViresh Kumar static inline struct clk *clk_get(struct device *dev, const char *id) 55393abe8e4SViresh Kumar { 55493abe8e4SViresh Kumar return NULL; 55593abe8e4SViresh Kumar } 55693abe8e4SViresh Kumar 557266e4e9dSDong Aisheng static inline int clk_bulk_get(struct device *dev, int num_clks, 558266e4e9dSDong Aisheng struct clk_bulk_data *clks) 559266e4e9dSDong Aisheng { 560266e4e9dSDong Aisheng return 0; 561266e4e9dSDong Aisheng } 562266e4e9dSDong Aisheng 56393abe8e4SViresh Kumar static inline struct clk *devm_clk_get(struct device *dev, const char *id) 56493abe8e4SViresh Kumar { 56593abe8e4SViresh Kumar return NULL; 56693abe8e4SViresh Kumar } 56793abe8e4SViresh Kumar 568*618aee02SDong Aisheng static inline int devm_clk_bulk_get(struct device *dev, int num_clks, 569*618aee02SDong Aisheng struct clk_bulk_data *clks) 570*618aee02SDong Aisheng { 571*618aee02SDong Aisheng return 0; 572*618aee02SDong Aisheng } 573*618aee02SDong Aisheng 57471a2f115SKuninori Morimoto static inline struct clk *devm_get_clk_from_child(struct device *dev, 57571a2f115SKuninori Morimoto struct device_node *np, const char *con_id) 57671a2f115SKuninori Morimoto { 57771a2f115SKuninori Morimoto return NULL; 57871a2f115SKuninori Morimoto } 57971a2f115SKuninori Morimoto 58093abe8e4SViresh Kumar static inline void clk_put(struct clk *clk) {} 58193abe8e4SViresh Kumar 582266e4e9dSDong Aisheng static inline void clk_bulk_put(int num_clks, struct clk_bulk_data *clks) {} 583266e4e9dSDong Aisheng 58493abe8e4SViresh Kumar static inline void devm_clk_put(struct device *dev, struct clk *clk) {} 58593abe8e4SViresh Kumar 58693abe8e4SViresh Kumar static inline int clk_enable(struct clk *clk) 58793abe8e4SViresh Kumar { 58893abe8e4SViresh Kumar return 0; 58993abe8e4SViresh Kumar } 59093abe8e4SViresh Kumar 591266e4e9dSDong Aisheng static inline int clk_bulk_enable(int num_clks, struct clk_bulk_data *clks) 592266e4e9dSDong Aisheng { 593266e4e9dSDong Aisheng return 0; 594266e4e9dSDong Aisheng } 595266e4e9dSDong Aisheng 59693abe8e4SViresh Kumar static inline void clk_disable(struct clk *clk) {} 59793abe8e4SViresh Kumar 598266e4e9dSDong Aisheng 599266e4e9dSDong Aisheng static inline void clk_bulk_disable(int num_clks, 600266e4e9dSDong Aisheng struct clk_bulk_data *clks) {} 601266e4e9dSDong Aisheng 60293abe8e4SViresh Kumar static inline unsigned long clk_get_rate(struct clk *clk) 60393abe8e4SViresh Kumar { 60493abe8e4SViresh Kumar return 0; 60593abe8e4SViresh Kumar } 60693abe8e4SViresh Kumar 60793abe8e4SViresh Kumar static inline int clk_set_rate(struct clk *clk, unsigned long rate) 60893abe8e4SViresh Kumar { 60993abe8e4SViresh Kumar return 0; 61093abe8e4SViresh Kumar } 61193abe8e4SViresh Kumar 61293abe8e4SViresh Kumar static inline long clk_round_rate(struct clk *clk, unsigned long rate) 61393abe8e4SViresh Kumar { 61493abe8e4SViresh Kumar return 0; 61593abe8e4SViresh Kumar } 61693abe8e4SViresh Kumar 6174e88f3deSThierry Reding static inline bool clk_has_parent(struct clk *clk, struct clk *parent) 6184e88f3deSThierry Reding { 6194e88f3deSThierry Reding return true; 6204e88f3deSThierry Reding } 6214e88f3deSThierry Reding 62293abe8e4SViresh Kumar static inline int clk_set_parent(struct clk *clk, struct clk *parent) 62393abe8e4SViresh Kumar { 62493abe8e4SViresh Kumar return 0; 62593abe8e4SViresh Kumar } 62693abe8e4SViresh Kumar 62793abe8e4SViresh Kumar static inline struct clk *clk_get_parent(struct clk *clk) 62893abe8e4SViresh Kumar { 62993abe8e4SViresh Kumar return NULL; 63093abe8e4SViresh Kumar } 63193abe8e4SViresh Kumar 632b81ea968SDaniel Lezcano static inline struct clk *clk_get_sys(const char *dev_id, const char *con_id) 633b81ea968SDaniel Lezcano { 634b81ea968SDaniel Lezcano return NULL; 635b81ea968SDaniel Lezcano } 63693abe8e4SViresh Kumar #endif 63793abe8e4SViresh Kumar 63893abe8e4SViresh Kumar /* clk_prepare_enable helps cases using clk_enable in non-atomic context. */ 63993abe8e4SViresh Kumar static inline int clk_prepare_enable(struct clk *clk) 64093abe8e4SViresh Kumar { 64193abe8e4SViresh Kumar int ret; 64293abe8e4SViresh Kumar 64393abe8e4SViresh Kumar ret = clk_prepare(clk); 64493abe8e4SViresh Kumar if (ret) 64593abe8e4SViresh Kumar return ret; 64693abe8e4SViresh Kumar ret = clk_enable(clk); 64793abe8e4SViresh Kumar if (ret) 64893abe8e4SViresh Kumar clk_unprepare(clk); 64993abe8e4SViresh Kumar 65093abe8e4SViresh Kumar return ret; 65193abe8e4SViresh Kumar } 65293abe8e4SViresh Kumar 65393abe8e4SViresh Kumar /* clk_disable_unprepare helps cases using clk_disable in non-atomic context. */ 65493abe8e4SViresh Kumar static inline void clk_disable_unprepare(struct clk *clk) 65593abe8e4SViresh Kumar { 65693abe8e4SViresh Kumar clk_disable(clk); 65793abe8e4SViresh Kumar clk_unprepare(clk); 65893abe8e4SViresh Kumar } 65993abe8e4SViresh Kumar 660137f8a72SRob Herring #if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) 661766e6a4eSGrant Likely struct clk *of_clk_get(struct device_node *np, int index); 662766e6a4eSGrant Likely struct clk *of_clk_get_by_name(struct device_node *np, const char *name); 663766e6a4eSGrant Likely struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec); 664766e6a4eSGrant Likely #else 665766e6a4eSGrant Likely static inline struct clk *of_clk_get(struct device_node *np, int index) 666766e6a4eSGrant Likely { 6679f1612d3SShawn Guo return ERR_PTR(-ENOENT); 668766e6a4eSGrant Likely } 669766e6a4eSGrant Likely static inline struct clk *of_clk_get_by_name(struct device_node *np, 670766e6a4eSGrant Likely const char *name) 671766e6a4eSGrant Likely { 6729f1612d3SShawn Guo return ERR_PTR(-ENOENT); 673766e6a4eSGrant Likely } 674766e6a4eSGrant Likely #endif 675766e6a4eSGrant Likely 676f8ce2547SRussell King #endif 677