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 21f8ce2547SRussell King struct clk; 22f8ce2547SRussell King 23b2476490SMike Turquette #ifdef CONFIG_COMMON_CLK 24b2476490SMike Turquette 25b2476490SMike Turquette /** 26b2476490SMike Turquette * DOC: clk notifier callback types 27b2476490SMike Turquette * 28b2476490SMike Turquette * PRE_RATE_CHANGE - called immediately before the clk rate is changed, 29b2476490SMike Turquette * to indicate that the rate change will proceed. Drivers must 30b2476490SMike Turquette * immediately terminate any operations that will be affected by the 31b2476490SMike Turquette * rate change. Callbacks may either return NOTIFY_DONE or 32b2476490SMike Turquette * NOTIFY_STOP. 33b2476490SMike Turquette * 34b2476490SMike Turquette * ABORT_RATE_CHANGE: called if the rate change failed for some reason 35b2476490SMike Turquette * after PRE_RATE_CHANGE. In this case, all registered notifiers on 36b2476490SMike Turquette * the clk will be called with ABORT_RATE_CHANGE. Callbacks must 37b2476490SMike Turquette * always return NOTIFY_DONE. 38b2476490SMike Turquette * 39b2476490SMike Turquette * POST_RATE_CHANGE - called after the clk rate change has successfully 40b2476490SMike Turquette * completed. Callbacks must always return NOTIFY_DONE. 41b2476490SMike Turquette * 42b2476490SMike Turquette */ 43b2476490SMike Turquette #define PRE_RATE_CHANGE BIT(0) 44b2476490SMike Turquette #define POST_RATE_CHANGE BIT(1) 45b2476490SMike Turquette #define ABORT_RATE_CHANGE BIT(2) 46b2476490SMike Turquette 47b2476490SMike Turquette /** 48b2476490SMike Turquette * struct clk_notifier - associate a clk with a notifier 49b2476490SMike Turquette * @clk: struct clk * to associate the notifier with 50b2476490SMike Turquette * @notifier_head: a blocking_notifier_head for this clk 51b2476490SMike Turquette * @node: linked list pointers 52b2476490SMike Turquette * 53b2476490SMike Turquette * A list of struct clk_notifier is maintained by the notifier code. 54b2476490SMike Turquette * An entry is created whenever code registers the first notifier on a 55b2476490SMike Turquette * particular @clk. Future notifiers on that @clk are added to the 56b2476490SMike Turquette * @notifier_head. 57b2476490SMike Turquette */ 58b2476490SMike Turquette struct clk_notifier { 59b2476490SMike Turquette struct clk *clk; 60b2476490SMike Turquette struct srcu_notifier_head notifier_head; 61b2476490SMike Turquette struct list_head node; 62b2476490SMike Turquette }; 63b2476490SMike Turquette 64b2476490SMike Turquette /** 65b2476490SMike Turquette * struct clk_notifier_data - rate data to pass to the notifier callback 66b2476490SMike Turquette * @clk: struct clk * being changed 67b2476490SMike Turquette * @old_rate: previous rate of this clk 68b2476490SMike Turquette * @new_rate: new rate of this clk 69b2476490SMike Turquette * 70b2476490SMike Turquette * For a pre-notifier, old_rate is the clk's rate before this rate 71b2476490SMike Turquette * change, and new_rate is what the rate will be in the future. For a 72b2476490SMike Turquette * post-notifier, old_rate and new_rate are both set to the clk's 73b2476490SMike Turquette * current rate (this was done to optimize the implementation). 74b2476490SMike Turquette */ 75b2476490SMike Turquette struct clk_notifier_data { 76b2476490SMike Turquette struct clk *clk; 77b2476490SMike Turquette unsigned long old_rate; 78b2476490SMike Turquette unsigned long new_rate; 79b2476490SMike Turquette }; 80b2476490SMike Turquette 81b2476490SMike Turquette int clk_notifier_register(struct clk *clk, struct notifier_block *nb); 82b2476490SMike Turquette 83b2476490SMike Turquette int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb); 84b2476490SMike Turquette 857e87aed9SMark Brown #endif 86b2476490SMike Turquette 87f8ce2547SRussell King /** 88*93abe8e4SViresh Kumar * clk_prepare - prepare a clock source 89*93abe8e4SViresh Kumar * @clk: clock source 90*93abe8e4SViresh Kumar * 91*93abe8e4SViresh Kumar * This prepares the clock source for use. 92*93abe8e4SViresh Kumar * 93*93abe8e4SViresh Kumar * Must not be called from within atomic context. 94*93abe8e4SViresh Kumar */ 95*93abe8e4SViresh Kumar #ifdef CONFIG_HAVE_CLK_PREPARE 96*93abe8e4SViresh Kumar int clk_prepare(struct clk *clk); 97*93abe8e4SViresh Kumar #else 98*93abe8e4SViresh Kumar static inline int clk_prepare(struct clk *clk) 99*93abe8e4SViresh Kumar { 100*93abe8e4SViresh Kumar might_sleep(); 101*93abe8e4SViresh Kumar return 0; 102*93abe8e4SViresh Kumar } 103*93abe8e4SViresh Kumar #endif 104*93abe8e4SViresh Kumar 105*93abe8e4SViresh Kumar /** 106*93abe8e4SViresh Kumar * clk_unprepare - undo preparation of a clock source 107*93abe8e4SViresh Kumar * @clk: clock source 108*93abe8e4SViresh Kumar * 109*93abe8e4SViresh Kumar * This undoes a previously prepared clock. The caller must balance 110*93abe8e4SViresh Kumar * the number of prepare and unprepare calls. 111*93abe8e4SViresh Kumar * 112*93abe8e4SViresh Kumar * Must not be called from within atomic context. 113*93abe8e4SViresh Kumar */ 114*93abe8e4SViresh Kumar #ifdef CONFIG_HAVE_CLK_PREPARE 115*93abe8e4SViresh Kumar void clk_unprepare(struct clk *clk); 116*93abe8e4SViresh Kumar #else 117*93abe8e4SViresh Kumar static inline void clk_unprepare(struct clk *clk) 118*93abe8e4SViresh Kumar { 119*93abe8e4SViresh Kumar might_sleep(); 120*93abe8e4SViresh Kumar } 121*93abe8e4SViresh Kumar #endif 122*93abe8e4SViresh Kumar 123*93abe8e4SViresh Kumar #ifdef CONFIG_HAVE_CLK 124*93abe8e4SViresh Kumar /** 125f8ce2547SRussell King * clk_get - lookup and obtain a reference to a clock producer. 126f8ce2547SRussell King * @dev: device for clock "consumer" 127a58b3a4aSJan-Simon Möller * @id: clock consumer ID 128f8ce2547SRussell King * 129f8ce2547SRussell King * Returns a struct clk corresponding to the clock producer, or 130f8ce2547SRussell King * valid IS_ERR() condition containing errno. The implementation 131f8ce2547SRussell King * uses @dev and @id to determine the clock consumer, and thereby 132f8ce2547SRussell King * the clock producer. (IOW, @id may be identical strings, but 133f8ce2547SRussell King * clk_get may return different clock producers depending on @dev.) 134f8ce2547SRussell King * 135f8ce2547SRussell King * Drivers must assume that the clock source is not enabled. 136f7ad160bSAlex Raimondi * 137f7ad160bSAlex Raimondi * clk_get should not be called from within interrupt context. 138f8ce2547SRussell King */ 139f8ce2547SRussell King struct clk *clk_get(struct device *dev, const char *id); 140f8ce2547SRussell King 141f8ce2547SRussell King /** 142a8a97db9SMark Brown * devm_clk_get - lookup and obtain a managed reference to a clock producer. 143a8a97db9SMark Brown * @dev: device for clock "consumer" 144a58b3a4aSJan-Simon Möller * @id: clock consumer ID 145a8a97db9SMark Brown * 146a8a97db9SMark Brown * Returns a struct clk corresponding to the clock producer, or 147a8a97db9SMark Brown * valid IS_ERR() condition containing errno. The implementation 148a8a97db9SMark Brown * uses @dev and @id to determine the clock consumer, and thereby 149a8a97db9SMark Brown * the clock producer. (IOW, @id may be identical strings, but 150a8a97db9SMark Brown * clk_get may return different clock producers depending on @dev.) 151a8a97db9SMark Brown * 152a8a97db9SMark Brown * Drivers must assume that the clock source is not enabled. 153a8a97db9SMark Brown * 154a8a97db9SMark Brown * devm_clk_get should not be called from within interrupt context. 155a8a97db9SMark Brown * 156a8a97db9SMark Brown * The clock will automatically be freed when the device is unbound 157a8a97db9SMark Brown * from the bus. 158a8a97db9SMark Brown */ 159a8a97db9SMark Brown struct clk *devm_clk_get(struct device *dev, const char *id); 160a8a97db9SMark Brown 161a8a97db9SMark Brown /** 162f8ce2547SRussell King * clk_enable - inform the system when the clock source should be running. 163f8ce2547SRussell King * @clk: clock source 164f8ce2547SRussell King * 165f8ce2547SRussell King * If the clock can not be enabled/disabled, this should return success. 166f8ce2547SRussell King * 16740d3e0f4SRussell King * May be called from atomic contexts. 16840d3e0f4SRussell King * 169f8ce2547SRussell King * Returns success (0) or negative errno. 170f8ce2547SRussell King */ 171f8ce2547SRussell King int clk_enable(struct clk *clk); 172f8ce2547SRussell King 173f8ce2547SRussell King /** 174f8ce2547SRussell King * clk_disable - inform the system when the clock source is no longer required. 175f8ce2547SRussell King * @clk: clock source 176f8ce2547SRussell King * 177f8ce2547SRussell King * Inform the system that a clock source is no longer required by 178f8ce2547SRussell King * a driver and may be shut down. 179f8ce2547SRussell King * 18040d3e0f4SRussell King * May be called from atomic contexts. 18140d3e0f4SRussell King * 182f8ce2547SRussell King * Implementation detail: if the clock source is shared between 183f8ce2547SRussell King * multiple drivers, clk_enable() calls must be balanced by the 184f8ce2547SRussell King * same number of clk_disable() calls for the clock source to be 185f8ce2547SRussell King * disabled. 186f8ce2547SRussell King */ 187f8ce2547SRussell King void clk_disable(struct clk *clk); 188f8ce2547SRussell King 189f8ce2547SRussell King /** 190f8ce2547SRussell King * clk_get_rate - obtain the current clock rate (in Hz) for a clock source. 191f8ce2547SRussell King * This is only valid once the clock source has been enabled. 192f8ce2547SRussell King * @clk: clock source 193f8ce2547SRussell King */ 194f8ce2547SRussell King unsigned long clk_get_rate(struct clk *clk); 195f8ce2547SRussell King 196f8ce2547SRussell King /** 197f8ce2547SRussell King * clk_put - "free" the clock source 198f8ce2547SRussell King * @clk: clock source 199f8ce2547SRussell King * 200f8ce2547SRussell King * Note: drivers must ensure that all clk_enable calls made on this 201f8ce2547SRussell King * clock source are balanced by clk_disable calls prior to calling 202f8ce2547SRussell King * this function. 203f7ad160bSAlex Raimondi * 204f7ad160bSAlex Raimondi * clk_put should not be called from within interrupt context. 205f8ce2547SRussell King */ 206f8ce2547SRussell King void clk_put(struct clk *clk); 207f8ce2547SRussell King 208a8a97db9SMark Brown /** 209a8a97db9SMark Brown * devm_clk_put - "free" a managed clock source 210a8a97db9SMark Brown * @dev: device used to acuqire the clock 211a8a97db9SMark Brown * @clk: clock source acquired with devm_clk_get() 212a8a97db9SMark Brown * 213a8a97db9SMark Brown * Note: drivers must ensure that all clk_enable calls made on this 214a8a97db9SMark Brown * clock source are balanced by clk_disable calls prior to calling 215a8a97db9SMark Brown * this function. 216a8a97db9SMark Brown * 217a8a97db9SMark Brown * clk_put should not be called from within interrupt context. 218a8a97db9SMark Brown */ 219a8a97db9SMark Brown void devm_clk_put(struct device *dev, struct clk *clk); 220f8ce2547SRussell King 221f8ce2547SRussell King /* 222f8ce2547SRussell King * The remaining APIs are optional for machine class support. 223f8ce2547SRussell King */ 224f8ce2547SRussell King 225f8ce2547SRussell King 226f8ce2547SRussell King /** 227f8ce2547SRussell King * clk_round_rate - adjust a rate to the exact rate a clock can provide 228f8ce2547SRussell King * @clk: clock source 229f8ce2547SRussell King * @rate: desired clock rate in Hz 230f8ce2547SRussell King * 231f8ce2547SRussell King * Returns rounded clock rate in Hz, or negative errno. 232f8ce2547SRussell King */ 233f8ce2547SRussell King long clk_round_rate(struct clk *clk, unsigned long rate); 234f8ce2547SRussell King 235f8ce2547SRussell King /** 236f8ce2547SRussell King * clk_set_rate - set the clock rate for a clock source 237f8ce2547SRussell King * @clk: clock source 238f8ce2547SRussell King * @rate: desired clock rate in Hz 239f8ce2547SRussell King * 240f8ce2547SRussell King * Returns success (0) or negative errno. 241f8ce2547SRussell King */ 242f8ce2547SRussell King int clk_set_rate(struct clk *clk, unsigned long rate); 243f8ce2547SRussell King 244f8ce2547SRussell King /** 245f8ce2547SRussell King * clk_set_parent - set the parent clock source for this clock 246f8ce2547SRussell King * @clk: clock source 247f8ce2547SRussell King * @parent: parent clock source 248f8ce2547SRussell King * 249f8ce2547SRussell King * Returns success (0) or negative errno. 250f8ce2547SRussell King */ 251f8ce2547SRussell King int clk_set_parent(struct clk *clk, struct clk *parent); 252f8ce2547SRussell King 253f8ce2547SRussell King /** 254f8ce2547SRussell King * clk_get_parent - get the parent clock source for this clock 255f8ce2547SRussell King * @clk: clock source 256f8ce2547SRussell King * 257f8ce2547SRussell King * Returns struct clk corresponding to parent clock source, or 258f8ce2547SRussell King * valid IS_ERR() condition containing errno. 259f8ce2547SRussell King */ 260f8ce2547SRussell King struct clk *clk_get_parent(struct clk *clk); 261f8ce2547SRussell King 26205fd8e73SSascha Hauer /** 26305fd8e73SSascha Hauer * clk_get_sys - get a clock based upon the device name 26405fd8e73SSascha Hauer * @dev_id: device name 26505fd8e73SSascha Hauer * @con_id: connection ID 26605fd8e73SSascha Hauer * 26705fd8e73SSascha Hauer * Returns a struct clk corresponding to the clock producer, or 26805fd8e73SSascha Hauer * valid IS_ERR() condition containing errno. The implementation 26905fd8e73SSascha Hauer * uses @dev_id and @con_id to determine the clock consumer, and 27005fd8e73SSascha Hauer * thereby the clock producer. In contrast to clk_get() this function 27105fd8e73SSascha Hauer * takes the device name instead of the device itself for identification. 27205fd8e73SSascha Hauer * 27305fd8e73SSascha Hauer * Drivers must assume that the clock source is not enabled. 27405fd8e73SSascha Hauer * 27505fd8e73SSascha Hauer * clk_get_sys should not be called from within interrupt context. 27605fd8e73SSascha Hauer */ 27705fd8e73SSascha Hauer struct clk *clk_get_sys(const char *dev_id, const char *con_id); 27805fd8e73SSascha Hauer 279*93abe8e4SViresh Kumar #else /* !CONFIG_HAVE_CLK */ 280*93abe8e4SViresh Kumar 281*93abe8e4SViresh Kumar static inline struct clk *clk_get(struct device *dev, const char *id) 282*93abe8e4SViresh Kumar { 283*93abe8e4SViresh Kumar return NULL; 284*93abe8e4SViresh Kumar } 285*93abe8e4SViresh Kumar 286*93abe8e4SViresh Kumar static inline struct clk *devm_clk_get(struct device *dev, const char *id) 287*93abe8e4SViresh Kumar { 288*93abe8e4SViresh Kumar return NULL; 289*93abe8e4SViresh Kumar } 290*93abe8e4SViresh Kumar 291*93abe8e4SViresh Kumar static inline void clk_put(struct clk *clk) {} 292*93abe8e4SViresh Kumar 293*93abe8e4SViresh Kumar static inline void devm_clk_put(struct device *dev, struct clk *clk) {} 294*93abe8e4SViresh Kumar 295*93abe8e4SViresh Kumar static inline int clk_enable(struct clk *clk) 296*93abe8e4SViresh Kumar { 297*93abe8e4SViresh Kumar return 0; 298*93abe8e4SViresh Kumar } 299*93abe8e4SViresh Kumar 300*93abe8e4SViresh Kumar static inline void clk_disable(struct clk *clk) {} 301*93abe8e4SViresh Kumar 302*93abe8e4SViresh Kumar static inline unsigned long clk_get_rate(struct clk *clk) 303*93abe8e4SViresh Kumar { 304*93abe8e4SViresh Kumar return 0; 305*93abe8e4SViresh Kumar } 306*93abe8e4SViresh Kumar 307*93abe8e4SViresh Kumar static inline int clk_set_rate(struct clk *clk, unsigned long rate) 308*93abe8e4SViresh Kumar { 309*93abe8e4SViresh Kumar return 0; 310*93abe8e4SViresh Kumar } 311*93abe8e4SViresh Kumar 312*93abe8e4SViresh Kumar static inline long clk_round_rate(struct clk *clk, unsigned long rate) 313*93abe8e4SViresh Kumar { 314*93abe8e4SViresh Kumar return 0; 315*93abe8e4SViresh Kumar } 316*93abe8e4SViresh Kumar 317*93abe8e4SViresh Kumar static inline int clk_set_parent(struct clk *clk, struct clk *parent) 318*93abe8e4SViresh Kumar { 319*93abe8e4SViresh Kumar return 0; 320*93abe8e4SViresh Kumar } 321*93abe8e4SViresh Kumar 322*93abe8e4SViresh Kumar static inline struct clk *clk_get_parent(struct clk *clk) 323*93abe8e4SViresh Kumar { 324*93abe8e4SViresh Kumar return NULL; 325*93abe8e4SViresh Kumar } 326*93abe8e4SViresh Kumar 327*93abe8e4SViresh Kumar #endif 328*93abe8e4SViresh Kumar 329*93abe8e4SViresh Kumar /* clk_prepare_enable helps cases using clk_enable in non-atomic context. */ 330*93abe8e4SViresh Kumar static inline int clk_prepare_enable(struct clk *clk) 331*93abe8e4SViresh Kumar { 332*93abe8e4SViresh Kumar int ret; 333*93abe8e4SViresh Kumar 334*93abe8e4SViresh Kumar ret = clk_prepare(clk); 335*93abe8e4SViresh Kumar if (ret) 336*93abe8e4SViresh Kumar return ret; 337*93abe8e4SViresh Kumar ret = clk_enable(clk); 338*93abe8e4SViresh Kumar if (ret) 339*93abe8e4SViresh Kumar clk_unprepare(clk); 340*93abe8e4SViresh Kumar 341*93abe8e4SViresh Kumar return ret; 342*93abe8e4SViresh Kumar } 343*93abe8e4SViresh Kumar 344*93abe8e4SViresh Kumar /* clk_disable_unprepare helps cases using clk_disable in non-atomic context. */ 345*93abe8e4SViresh Kumar static inline void clk_disable_unprepare(struct clk *clk) 346*93abe8e4SViresh Kumar { 347*93abe8e4SViresh Kumar clk_disable(clk); 348*93abe8e4SViresh Kumar clk_unprepare(clk); 349*93abe8e4SViresh Kumar } 350*93abe8e4SViresh Kumar 351c0683039STony Lindgren /** 352c0683039STony Lindgren * clk_add_alias - add a new clock alias 353c0683039STony Lindgren * @alias: name for clock alias 354c0683039STony Lindgren * @alias_dev_name: device name 355c0683039STony Lindgren * @id: platform specific clock name 356c0683039STony Lindgren * @dev: device 357c0683039STony Lindgren * 358c0683039STony Lindgren * Allows using generic clock names for drivers by adding a new alias. 359c0683039STony Lindgren * Assumes clkdev, see clkdev.h for more info. 360c0683039STony Lindgren */ 361c0683039STony Lindgren int clk_add_alias(const char *alias, const char *alias_dev_name, char *id, 362c0683039STony Lindgren struct device *dev); 363c0683039STony Lindgren 364766e6a4eSGrant Likely struct device_node; 365766e6a4eSGrant Likely struct of_phandle_args; 366766e6a4eSGrant Likely 367137f8a72SRob Herring #if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) 368766e6a4eSGrant Likely struct clk *of_clk_get(struct device_node *np, int index); 369766e6a4eSGrant Likely struct clk *of_clk_get_by_name(struct device_node *np, const char *name); 370766e6a4eSGrant Likely struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec); 371766e6a4eSGrant Likely #else 372766e6a4eSGrant Likely static inline struct clk *of_clk_get(struct device_node *np, int index) 373766e6a4eSGrant Likely { 3749f1612d3SShawn Guo return ERR_PTR(-ENOENT); 375766e6a4eSGrant Likely } 376766e6a4eSGrant Likely static inline struct clk *of_clk_get_by_name(struct device_node *np, 377766e6a4eSGrant Likely const char *name) 378766e6a4eSGrant Likely { 3799f1612d3SShawn Guo return ERR_PTR(-ENOENT); 380766e6a4eSGrant Likely } 381766e6a4eSGrant Likely #endif 382766e6a4eSGrant Likely 383f8ce2547SRussell King #endif 384