clk.txt 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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, unsigned long,
  60. unsigned long *);
  61. long (*determine_rate)(struct clk_hw *hw,
  62. unsigned long rate,
  63. unsigned long *best_parent_rate,
  64. struct clk **best_parent_clk);
  65. int (*set_parent)(struct clk_hw *hw, u8 index);
  66. u8 (*get_parent)(struct clk_hw *hw);
  67. int (*set_rate)(struct clk_hw *hw, unsigned long);
  68. void (*init)(struct clk_hw *hw);
  69. };
  70. Part 3 - hardware clk implementations
  71. The strength of the common struct clk comes from its .ops and .hw pointers
  72. which abstract the details of struct clk from the hardware-specific bits, and
  73. vice versa. To illustrate consider the simple gateable clk implementation in
  74. drivers/clk/clk-gate.c:
  75. struct clk_gate {
  76. struct clk_hw hw;
  77. void __iomem *reg;
  78. u8 bit_idx;
  79. ...
  80. };
  81. struct clk_gate contains struct clk_hw hw as well as hardware-specific
  82. knowledge about which register and bit controls this clk's gating.
  83. Nothing about clock topology or accounting, such as enable_count or
  84. notifier_count, is needed here. That is all handled by the common
  85. framework code and struct clk.
  86. Let's walk through enabling this clk from driver code:
  87. struct clk *clk;
  88. clk = clk_get(NULL, "my_gateable_clk");
  89. clk_prepare(clk);
  90. clk_enable(clk);
  91. The call graph for clk_enable is very simple:
  92. clk_enable(clk);
  93. clk->ops->enable(clk->hw);
  94. [resolves to...]
  95. clk_gate_enable(hw);
  96. [resolves struct clk gate with to_clk_gate(hw)]
  97. clk_gate_set_bit(gate);
  98. And the definition of clk_gate_set_bit:
  99. static void clk_gate_set_bit(struct clk_gate *gate)
  100. {
  101. u32 reg;
  102. reg = __raw_readl(gate->reg);
  103. reg |= BIT(gate->bit_idx);
  104. writel(reg, gate->reg);
  105. }
  106. Note that to_clk_gate is defined as:
  107. #define to_clk_gate(_hw) container_of(_hw, struct clk_gate, clk)
  108. This pattern of abstraction is used for every clock hardware
  109. representation.
  110. Part 4 - supporting your own clk hardware
  111. When implementing support for a new type of clock it only necessary to
  112. include the following header:
  113. #include <linux/clk-provider.h>
  114. include/linux/clk.h is included within that header and clk-private.h
  115. must never be included from the code which implements the operations for
  116. a clock. More on that below in Part 5.
  117. To construct a clk hardware structure for your platform you must define
  118. the following:
  119. struct clk_foo {
  120. struct clk_hw hw;
  121. ... hardware specific data goes here ...
  122. };
  123. To take advantage of your data you'll need to support valid operations
  124. for your clk:
  125. struct clk_ops clk_foo_ops {
  126. .enable = &clk_foo_enable;
  127. .disable = &clk_foo_disable;
  128. };
  129. Implement the above functions using container_of:
  130. #define to_clk_foo(_hw) container_of(_hw, struct clk_foo, hw)
  131. int clk_foo_enable(struct clk_hw *hw)
  132. {
  133. struct clk_foo *foo;
  134. foo = to_clk_foo(hw);
  135. ... perform magic on foo ...
  136. return 0;
  137. };
  138. Below is a matrix detailing which clk_ops are mandatory based upon the
  139. hardware capabilities of that clock. A cell marked as "y" means
  140. mandatory, a cell marked as "n" implies that either including that
  141. callback is invalid or otherwise unnecessary. Empty cells are either
  142. optional or must be evaluated on a case-by-case basis.
  143. clock hardware characteristics
  144. -----------------------------------------------------------
  145. | gate | change rate | single parent | multiplexer | root |
  146. |------|-------------|---------------|-------------|------|
  147. .prepare | | | | | |
  148. .unprepare | | | | | |
  149. | | | | | |
  150. .enable | y | | | | |
  151. .disable | y | | | | |
  152. .is_enabled | y | | | | |
  153. | | | | | |
  154. .recalc_rate | | y | | | |
  155. .round_rate | | y [1] | | | |
  156. .determine_rate | | y [1] | | | |
  157. .set_rate | | y | | | |
  158. | | | | | |
  159. .set_parent | | | n | y | n |
  160. .get_parent | | | n | y | n |
  161. | | | | | |
  162. .init | | | | | |
  163. -----------------------------------------------------------
  164. [1] either one of round_rate or determine_rate is required.
  165. Finally, register your clock at run-time with a hardware-specific
  166. registration function. This function simply populates struct clk_foo's
  167. data and then passes the common struct clk parameters to the framework
  168. with a call to:
  169. clk_register(...)
  170. See the basic clock types in drivers/clk/clk-*.c for examples.
  171. Part 5 - static initialization of clock data
  172. For platforms with many clocks (often numbering into the hundreds) it
  173. may be desirable to statically initialize some clock data. This
  174. presents a problem since the definition of struct clk should be hidden
  175. from everyone except for the clock core in drivers/clk/clk.c.
  176. To get around this problem struct clk's definition is exposed in
  177. include/linux/clk-private.h along with some macros for more easily
  178. initializing instances of the basic clock types. These clocks must
  179. still be initialized with the common clock framework via a call to
  180. __clk_init.
  181. clk-private.h must NEVER be included by code which implements struct
  182. clk_ops callbacks, nor must it be included by any logic which pokes
  183. around inside of struct clk at run-time. To do so is a layering
  184. violation.
  185. To better enforce this policy, always follow this simple rule: any
  186. statically initialized clock data MUST be defined in a separate file
  187. from the logic that implements its ops. Basically separate the logic
  188. from the data and all is well.
  189. Part 6 - Disabling clock gating of unused clocks
  190. Sometimes during development it can be useful to be able to bypass the
  191. default disabling of unused clocks. For example, if drivers aren't enabling
  192. clocks properly but rely on them being on from the bootloader, bypassing
  193. the disabling means that the driver will remain functional while the issues
  194. are sorted out.
  195. To bypass this disabling, include "clk_ignore_unused" in the bootargs to the
  196. kernel.