clk.txt 10 KB

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