clk.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * linux/include/linux/clk.h
  3. *
  4. * Copyright (C) 2004 ARM Limited.
  5. * Written by Deep Blue Solutions Limited.
  6. * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #ifndef __LINUX_CLK_H
  13. #define __LINUX_CLK_H
  14. #include <linux/err.h>
  15. #include <linux/kernel.h>
  16. #include <linux/notifier.h>
  17. struct device;
  18. struct clk;
  19. #ifdef CONFIG_COMMON_CLK
  20. /**
  21. * DOC: clk notifier callback types
  22. *
  23. * PRE_RATE_CHANGE - called immediately before the clk rate is changed,
  24. * to indicate that the rate change will proceed. Drivers must
  25. * immediately terminate any operations that will be affected by the
  26. * rate change. Callbacks may either return NOTIFY_DONE, NOTIFY_OK,
  27. * NOTIFY_STOP or NOTIFY_BAD.
  28. *
  29. * ABORT_RATE_CHANGE: called if the rate change failed for some reason
  30. * after PRE_RATE_CHANGE. In this case, all registered notifiers on
  31. * the clk will be called with ABORT_RATE_CHANGE. Callbacks must
  32. * always return NOTIFY_DONE or NOTIFY_OK.
  33. *
  34. * POST_RATE_CHANGE - called after the clk rate change has successfully
  35. * completed. Callbacks must always return NOTIFY_DONE or NOTIFY_OK.
  36. *
  37. */
  38. #define PRE_RATE_CHANGE BIT(0)
  39. #define POST_RATE_CHANGE BIT(1)
  40. #define ABORT_RATE_CHANGE BIT(2)
  41. /**
  42. * struct clk_notifier - associate a clk with a notifier
  43. * @clk: struct clk * to associate the notifier with
  44. * @notifier_head: a blocking_notifier_head for this clk
  45. * @node: linked list pointers
  46. *
  47. * A list of struct clk_notifier is maintained by the notifier code.
  48. * An entry is created whenever code registers the first notifier on a
  49. * particular @clk. Future notifiers on that @clk are added to the
  50. * @notifier_head.
  51. */
  52. struct clk_notifier {
  53. struct clk *clk;
  54. struct srcu_notifier_head notifier_head;
  55. struct list_head node;
  56. };
  57. /**
  58. * struct clk_notifier_data - rate data to pass to the notifier callback
  59. * @clk: struct clk * being changed
  60. * @old_rate: previous rate of this clk
  61. * @new_rate: new rate of this clk
  62. *
  63. * For a pre-notifier, old_rate is the clk's rate before this rate
  64. * change, and new_rate is what the rate will be in the future. For a
  65. * post-notifier, old_rate and new_rate are both set to the clk's
  66. * current rate (this was done to optimize the implementation).
  67. */
  68. struct clk_notifier_data {
  69. struct clk *clk;
  70. unsigned long old_rate;
  71. unsigned long new_rate;
  72. };
  73. int clk_notifier_register(struct clk *clk, struct notifier_block *nb);
  74. int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb);
  75. #endif
  76. /**
  77. * clk_prepare - prepare a clock source
  78. * @clk: clock source
  79. *
  80. * This prepares the clock source for use.
  81. *
  82. * Must not be called from within atomic context.
  83. */
  84. #ifdef CONFIG_HAVE_CLK_PREPARE
  85. int clk_prepare(struct clk *clk);
  86. #else
  87. static inline int clk_prepare(struct clk *clk)
  88. {
  89. might_sleep();
  90. return 0;
  91. }
  92. #endif
  93. /**
  94. * clk_unprepare - undo preparation of a clock source
  95. * @clk: clock source
  96. *
  97. * This undoes a previously prepared clock. The caller must balance
  98. * the number of prepare and unprepare calls.
  99. *
  100. * Must not be called from within atomic context.
  101. */
  102. #ifdef CONFIG_HAVE_CLK_PREPARE
  103. void clk_unprepare(struct clk *clk);
  104. #else
  105. static inline void clk_unprepare(struct clk *clk)
  106. {
  107. might_sleep();
  108. }
  109. #endif
  110. #ifdef CONFIG_HAVE_CLK
  111. /**
  112. * clk_get - lookup and obtain a reference to a clock producer.
  113. * @dev: device for clock "consumer"
  114. * @id: clock consumer ID
  115. *
  116. * Returns a struct clk corresponding to the clock producer, or
  117. * valid IS_ERR() condition containing errno. The implementation
  118. * uses @dev and @id to determine the clock consumer, and thereby
  119. * the clock producer. (IOW, @id may be identical strings, but
  120. * clk_get may return different clock producers depending on @dev.)
  121. *
  122. * Drivers must assume that the clock source is not enabled.
  123. *
  124. * clk_get should not be called from within interrupt context.
  125. */
  126. struct clk *clk_get(struct device *dev, const char *id);
  127. /**
  128. * devm_clk_get - lookup and obtain a managed reference to a clock producer.
  129. * @dev: device for clock "consumer"
  130. * @id: clock consumer ID
  131. *
  132. * Returns a struct clk corresponding to the clock producer, or
  133. * valid IS_ERR() condition containing errno. The implementation
  134. * uses @dev and @id to determine the clock consumer, and thereby
  135. * the clock producer. (IOW, @id may be identical strings, but
  136. * clk_get may return different clock producers depending on @dev.)
  137. *
  138. * Drivers must assume that the clock source is not enabled.
  139. *
  140. * devm_clk_get should not be called from within interrupt context.
  141. *
  142. * The clock will automatically be freed when the device is unbound
  143. * from the bus.
  144. */
  145. struct clk *devm_clk_get(struct device *dev, const char *id);
  146. /**
  147. * clk_enable - inform the system when the clock source should be running.
  148. * @clk: clock source
  149. *
  150. * If the clock can not be enabled/disabled, this should return success.
  151. *
  152. * May be called from atomic contexts.
  153. *
  154. * Returns success (0) or negative errno.
  155. */
  156. int clk_enable(struct clk *clk);
  157. /**
  158. * clk_disable - inform the system when the clock source is no longer required.
  159. * @clk: clock source
  160. *
  161. * Inform the system that a clock source is no longer required by
  162. * a driver and may be shut down.
  163. *
  164. * May be called from atomic contexts.
  165. *
  166. * Implementation detail: if the clock source is shared between
  167. * multiple drivers, clk_enable() calls must be balanced by the
  168. * same number of clk_disable() calls for the clock source to be
  169. * disabled.
  170. */
  171. void clk_disable(struct clk *clk);
  172. /**
  173. * clk_get_rate - obtain the current clock rate (in Hz) for a clock source.
  174. * This is only valid once the clock source has been enabled.
  175. * @clk: clock source
  176. */
  177. unsigned long clk_get_rate(struct clk *clk);
  178. /**
  179. * clk_put - "free" the clock source
  180. * @clk: clock source
  181. *
  182. * Note: drivers must ensure that all clk_enable calls made on this
  183. * clock source are balanced by clk_disable calls prior to calling
  184. * this function.
  185. *
  186. * clk_put should not be called from within interrupt context.
  187. */
  188. void clk_put(struct clk *clk);
  189. /**
  190. * devm_clk_put - "free" a managed clock source
  191. * @dev: device used to acuqire the clock
  192. * @clk: clock source acquired with devm_clk_get()
  193. *
  194. * Note: drivers must ensure that all clk_enable calls made on this
  195. * clock source are balanced by clk_disable calls prior to calling
  196. * this function.
  197. *
  198. * clk_put should not be called from within interrupt context.
  199. */
  200. void devm_clk_put(struct device *dev, struct clk *clk);
  201. /*
  202. * The remaining APIs are optional for machine class support.
  203. */
  204. /**
  205. * clk_round_rate - adjust a rate to the exact rate a clock can provide
  206. * @clk: clock source
  207. * @rate: desired clock rate in Hz
  208. *
  209. * Returns rounded clock rate in Hz, or negative errno.
  210. */
  211. long clk_round_rate(struct clk *clk, unsigned long rate);
  212. /**
  213. * clk_set_rate - set the clock rate for a clock source
  214. * @clk: clock source
  215. * @rate: desired clock rate in Hz
  216. *
  217. * Returns success (0) or negative errno.
  218. */
  219. int clk_set_rate(struct clk *clk, unsigned long rate);
  220. /**
  221. * clk_set_parent - set the parent clock source for this clock
  222. * @clk: clock source
  223. * @parent: parent clock source
  224. *
  225. * Returns success (0) or negative errno.
  226. */
  227. int clk_set_parent(struct clk *clk, struct clk *parent);
  228. /**
  229. * clk_get_parent - get the parent clock source for this clock
  230. * @clk: clock source
  231. *
  232. * Returns struct clk corresponding to parent clock source, or
  233. * valid IS_ERR() condition containing errno.
  234. */
  235. struct clk *clk_get_parent(struct clk *clk);
  236. /**
  237. * clk_get_sys - get a clock based upon the device name
  238. * @dev_id: device name
  239. * @con_id: connection ID
  240. *
  241. * Returns a struct clk corresponding to the clock producer, or
  242. * valid IS_ERR() condition containing errno. The implementation
  243. * uses @dev_id and @con_id to determine the clock consumer, and
  244. * thereby the clock producer. In contrast to clk_get() this function
  245. * takes the device name instead of the device itself for identification.
  246. *
  247. * Drivers must assume that the clock source is not enabled.
  248. *
  249. * clk_get_sys should not be called from within interrupt context.
  250. */
  251. struct clk *clk_get_sys(const char *dev_id, const char *con_id);
  252. #else /* !CONFIG_HAVE_CLK */
  253. static inline struct clk *clk_get(struct device *dev, const char *id)
  254. {
  255. return NULL;
  256. }
  257. static inline struct clk *devm_clk_get(struct device *dev, const char *id)
  258. {
  259. return NULL;
  260. }
  261. static inline void clk_put(struct clk *clk) {}
  262. static inline void devm_clk_put(struct device *dev, struct clk *clk) {}
  263. static inline int clk_enable(struct clk *clk)
  264. {
  265. return 0;
  266. }
  267. static inline void clk_disable(struct clk *clk) {}
  268. static inline unsigned long clk_get_rate(struct clk *clk)
  269. {
  270. return 0;
  271. }
  272. static inline int clk_set_rate(struct clk *clk, unsigned long rate)
  273. {
  274. return 0;
  275. }
  276. static inline long clk_round_rate(struct clk *clk, unsigned long rate)
  277. {
  278. return 0;
  279. }
  280. static inline int clk_set_parent(struct clk *clk, struct clk *parent)
  281. {
  282. return 0;
  283. }
  284. static inline struct clk *clk_get_parent(struct clk *clk)
  285. {
  286. return NULL;
  287. }
  288. #endif
  289. /* clk_prepare_enable helps cases using clk_enable in non-atomic context. */
  290. static inline int clk_prepare_enable(struct clk *clk)
  291. {
  292. int ret;
  293. ret = clk_prepare(clk);
  294. if (ret)
  295. return ret;
  296. ret = clk_enable(clk);
  297. if (ret)
  298. clk_unprepare(clk);
  299. return ret;
  300. }
  301. /* clk_disable_unprepare helps cases using clk_disable in non-atomic context. */
  302. static inline void clk_disable_unprepare(struct clk *clk)
  303. {
  304. clk_disable(clk);
  305. clk_unprepare(clk);
  306. }
  307. /**
  308. * clk_add_alias - add a new clock alias
  309. * @alias: name for clock alias
  310. * @alias_dev_name: device name
  311. * @id: platform specific clock name
  312. * @dev: device
  313. *
  314. * Allows using generic clock names for drivers by adding a new alias.
  315. * Assumes clkdev, see clkdev.h for more info.
  316. */
  317. int clk_add_alias(const char *alias, const char *alias_dev_name, char *id,
  318. struct device *dev);
  319. struct device_node;
  320. struct of_phandle_args;
  321. #if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK)
  322. struct clk *of_clk_get(struct device_node *np, int index);
  323. struct clk *of_clk_get_by_name(struct device_node *np, const char *name);
  324. struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec);
  325. #else
  326. static inline struct clk *of_clk_get(struct device_node *np, int index)
  327. {
  328. return ERR_PTR(-ENOENT);
  329. }
  330. static inline struct clk *of_clk_get_by_name(struct device_node *np,
  331. const char *name)
  332. {
  333. return ERR_PTR(-ENOENT);
  334. }
  335. #endif
  336. #endif