clk.h 8.9 KB

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