clk.txt 10 KB

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