clk.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. ========================
  2. The Common Clk Framework
  3. ========================
  4. :Author: Mike Turquette <mturquette@ti.com>
  5. This document endeavours to explain the common clk framework details,
  6. and how to port a platform over to this framework. It is not yet a
  7. detailed explanation of the clock api in include/linux/clk.h, but
  8. perhaps someday it will include that information.
  9. Introduction and interface split
  10. ================================
  11. The common clk framework is an interface to control the clock nodes
  12. available on various devices today. This may come in the form of clock
  13. gating, rate adjustment, muxing or other operations. This framework is
  14. enabled with the CONFIG_COMMON_CLK option.
  15. The interface itself is divided into two halves, each shielded from the
  16. details of its counterpart. First is the common definition of struct
  17. clk which unifies the framework-level accounting and infrastructure that
  18. has traditionally been duplicated across a variety of platforms. Second
  19. is a common implementation of the clk.h api, defined in
  20. drivers/clk/clk.c. Finally there is struct clk_ops, whose operations
  21. are invoked by the clk api implementation.
  22. The second half of the interface is comprised of the hardware-specific
  23. callbacks registered with struct clk_ops and the corresponding
  24. hardware-specific structures needed to model a particular clock. For
  25. the remainder of this document any reference to a callback in struct
  26. clk_ops, such as .enable or .set_rate, implies the hardware-specific
  27. implementation of that code. Likewise, references to struct clk_foo
  28. serve as a convenient shorthand for the implementation of the
  29. hardware-specific bits for the hypothetical "foo" hardware.
  30. Tying the two halves of this interface together is struct clk_hw, which
  31. is defined in struct clk_foo and pointed to within struct clk_core. This
  32. allows for easy navigation between the two discrete halves of the common
  33. clock interface.
  34. Common data structures and api
  35. ==============================
  36. Below is the common struct clk_core definition from
  37. drivers/clk/clk.c, modified for brevity::
  38. struct clk_core {
  39. const char *name;
  40. const struct clk_ops *ops;
  41. struct clk_hw *hw;
  42. struct module *owner;
  43. struct clk_core *parent;
  44. const char **parent_names;
  45. struct clk_core **parents;
  46. u8 num_parents;
  47. u8 new_parent_index;
  48. ...
  49. };
  50. The members above make up the core of the clk tree topology. The clk
  51. api itself defines several driver-facing functions which operate on
  52. struct clk. That api is documented in include/linux/clk.h.
  53. Platforms and devices utilizing the common struct clk_core use the struct
  54. clk_ops pointer in struct clk_core to perform the hardware-specific parts of
  55. the operations defined in clk-provider.h::
  56. struct clk_ops {
  57. int (*prepare)(struct clk_hw *hw);
  58. void (*unprepare)(struct clk_hw *hw);
  59. int (*is_prepared)(struct clk_hw *hw);
  60. void (*unprepare_unused)(struct clk_hw *hw);
  61. int (*enable)(struct clk_hw *hw);
  62. void (*disable)(struct clk_hw *hw);
  63. int (*is_enabled)(struct clk_hw *hw);
  64. void (*disable_unused)(struct clk_hw *hw);
  65. unsigned long (*recalc_rate)(struct clk_hw *hw,
  66. unsigned long parent_rate);
  67. long (*round_rate)(struct clk_hw *hw,
  68. unsigned long rate,
  69. unsigned long *parent_rate);
  70. int (*determine_rate)(struct clk_hw *hw,
  71. struct clk_rate_request *req);
  72. int (*set_parent)(struct clk_hw *hw, u8 index);
  73. u8 (*get_parent)(struct clk_hw *hw);
  74. int (*set_rate)(struct clk_hw *hw,
  75. unsigned long rate,
  76. unsigned long parent_rate);
  77. int (*set_rate_and_parent)(struct clk_hw *hw,
  78. unsigned long rate,
  79. unsigned long parent_rate,
  80. u8 index);
  81. unsigned long (*recalc_accuracy)(struct clk_hw *hw,
  82. unsigned long parent_accuracy);
  83. int (*get_phase)(struct clk_hw *hw);
  84. int (*set_phase)(struct clk_hw *hw, int degrees);
  85. void (*init)(struct clk_hw *hw);
  86. void (*debug_init)(struct clk_hw *hw,
  87. struct dentry *dentry);
  88. };
  89. Hardware clk implementations
  90. ============================
  91. The strength of the common struct clk_core comes from its .ops and .hw pointers
  92. which abstract the details of struct clk from the hardware-specific bits, and
  93. vice versa. To illustrate consider the simple gateable clk implementation in
  94. drivers/clk/clk-gate.c::
  95. struct clk_gate {
  96. struct clk_hw hw;
  97. void __iomem *reg;
  98. u8 bit_idx;
  99. ...
  100. };
  101. struct clk_gate contains struct clk_hw hw as well as hardware-specific
  102. knowledge about which register and bit controls this clk's gating.
  103. Nothing about clock topology or accounting, such as enable_count or
  104. notifier_count, is needed here. That is all handled by the common
  105. framework code and struct clk_core.
  106. Let's walk through enabling this clk from driver code::
  107. struct clk *clk;
  108. clk = clk_get(NULL, "my_gateable_clk");
  109. clk_prepare(clk);
  110. clk_enable(clk);
  111. The call graph for clk_enable is very simple::
  112. clk_enable(clk);
  113. clk->ops->enable(clk->hw);
  114. [resolves to...]
  115. clk_gate_enable(hw);
  116. [resolves struct clk gate with to_clk_gate(hw)]
  117. clk_gate_set_bit(gate);
  118. And the definition of clk_gate_set_bit::
  119. static void clk_gate_set_bit(struct clk_gate *gate)
  120. {
  121. u32 reg;
  122. reg = __raw_readl(gate->reg);
  123. reg |= BIT(gate->bit_idx);
  124. writel(reg, gate->reg);
  125. }
  126. Note that to_clk_gate is defined as::
  127. #define to_clk_gate(_hw) container_of(_hw, struct clk_gate, hw)
  128. This pattern of abstraction is used for every clock hardware
  129. representation.
  130. Supporting your own clk hardware
  131. ================================
  132. When implementing support for a new type of clock it is only necessary to
  133. include the following header::
  134. #include <linux/clk-provider.h>
  135. To construct a clk hardware structure for your platform you must define
  136. the following::
  137. struct clk_foo {
  138. struct clk_hw hw;
  139. ... hardware specific data goes here ...
  140. };
  141. To take advantage of your data you'll need to support valid operations
  142. for your clk::
  143. struct clk_ops clk_foo_ops = {
  144. .enable = &clk_foo_enable,
  145. .disable = &clk_foo_disable,
  146. };
  147. Implement the above functions using container_of::
  148. #define to_clk_foo(_hw) container_of(_hw, struct clk_foo, hw)
  149. int clk_foo_enable(struct clk_hw *hw)
  150. {
  151. struct clk_foo *foo;
  152. foo = to_clk_foo(hw);
  153. ... perform magic on foo ...
  154. return 0;
  155. };
  156. Below is a matrix detailing which clk_ops are mandatory based upon the
  157. hardware capabilities of that clock. A cell marked as "y" means
  158. mandatory, a cell marked as "n" implies that either including that
  159. callback is invalid or otherwise unnecessary. Empty cells are either
  160. optional or must be evaluated on a case-by-case basis.
  161. .. table:: clock hardware characteristics
  162. +----------------+------+-------------+---------------+-------------+------+
  163. | | gate | change rate | single parent | multiplexer | root |
  164. +================+======+=============+===============+=============+======+
  165. |.prepare | | | | | |
  166. +----------------+------+-------------+---------------+-------------+------+
  167. |.unprepare | | | | | |
  168. +----------------+------+-------------+---------------+-------------+------+
  169. +----------------+------+-------------+---------------+-------------+------+
  170. |.enable | y | | | | |
  171. +----------------+------+-------------+---------------+-------------+------+
  172. |.disable | y | | | | |
  173. +----------------+------+-------------+---------------+-------------+------+
  174. |.is_enabled | y | | | | |
  175. +----------------+------+-------------+---------------+-------------+------+
  176. +----------------+------+-------------+---------------+-------------+------+
  177. |.recalc_rate | | y | | | |
  178. +----------------+------+-------------+---------------+-------------+------+
  179. |.round_rate | | y [1]_ | | | |
  180. +----------------+------+-------------+---------------+-------------+------+
  181. |.determine_rate | | y [1]_ | | | |
  182. +----------------+------+-------------+---------------+-------------+------+
  183. |.set_rate | | y | | | |
  184. +----------------+------+-------------+---------------+-------------+------+
  185. +----------------+------+-------------+---------------+-------------+------+
  186. |.set_parent | | | n | y | n |
  187. +----------------+------+-------------+---------------+-------------+------+
  188. |.get_parent | | | n | y | n |
  189. +----------------+------+-------------+---------------+-------------+------+
  190. +----------------+------+-------------+---------------+-------------+------+
  191. |.recalc_accuracy| | | | | |
  192. +----------------+------+-------------+---------------+-------------+------+
  193. +----------------+------+-------------+---------------+-------------+------+
  194. |.init | | | | | |
  195. +----------------+------+-------------+---------------+-------------+------+
  196. .. [1] either one of round_rate or determine_rate is required.
  197. Finally, register your clock at run-time with a hardware-specific
  198. registration function. This function simply populates struct clk_foo's
  199. data and then passes the common struct clk parameters to the framework
  200. with a call to::
  201. clk_register(...)
  202. See the basic clock types in ``drivers/clk/clk-*.c`` for examples.
  203. Disabling clock gating of unused clocks
  204. =======================================
  205. Sometimes during development it can be useful to be able to bypass the
  206. default disabling of unused clocks. For example, if drivers aren't enabling
  207. clocks properly but rely on them being on from the bootloader, bypassing
  208. the disabling means that the driver will remain functional while the issues
  209. are sorted out.
  210. To bypass this disabling, include "clk_ignore_unused" in the bootargs to the
  211. kernel.
  212. Locking
  213. =======
  214. The common clock framework uses two global locks, the prepare lock and the
  215. enable lock.
  216. The enable lock is a spinlock and is held across calls to the .enable,
  217. .disable operations. Those operations are thus not allowed to sleep,
  218. and calls to the clk_enable(), clk_disable() API functions are allowed in
  219. atomic context.
  220. For clk_is_enabled() API, it is also designed to be allowed to be used in
  221. atomic context. However, it doesn't really make any sense to hold the enable
  222. lock in core, unless you want to do something else with the information of
  223. the enable state with that lock held. Otherwise, seeing if a clk is enabled is
  224. a one-shot read of the enabled state, which could just as easily change after
  225. the function returns because the lock is released. Thus the user of this API
  226. needs to handle synchronizing the read of the state with whatever they're
  227. using it for to make sure that the enable state doesn't change during that
  228. time.
  229. The prepare lock is a mutex and is held across calls to all other operations.
  230. All those operations are allowed to sleep, and calls to the corresponding API
  231. functions are not allowed in atomic context.
  232. This effectively divides operations in two groups from a locking perspective.
  233. Drivers don't need to manually protect resources shared between the operations
  234. of one group, regardless of whether those resources are shared by multiple
  235. clocks or not. However, access to resources that are shared between operations
  236. of the two groups needs to be protected by the drivers. An example of such a
  237. resource would be a register that controls both the clock rate and the clock
  238. enable/disable state.
  239. The clock framework is reentrant, in that a driver is allowed to call clock
  240. framework functions from within its implementation of clock operations. This
  241. can for instance cause a .set_rate operation of one clock being called from
  242. within the .set_rate operation of another clock. This case must be considered
  243. in the driver implementations, but the code flow is usually controlled by the
  244. driver in that case.
  245. Note that locking must also be considered when code outside of the common
  246. clock framework needs to access resources used by the clock operations. This
  247. is considered out of scope of this document.