clk.txt 8.5 KB

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