Mike Turquette | 69fe8a8 | 2012-03-15 23:11:18 -0700 | [diff] [blame] | 1 | The Common Clk Framework |
| 2 | Mike Turquette <mturquette@ti.com> |
| 3 | |
| 4 | This document endeavours to explain the common clk framework details, |
| 5 | and how to port a platform over to this framework. It is not yet a |
| 6 | detailed explanation of the clock api in include/linux/clk.h, but |
| 7 | perhaps someday it will include that information. |
| 8 | |
| 9 | Part 1 - 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 | |
| 16 | The interface itself is divided into two halves, each shielded from the |
| 17 | details of its counterpart. First is the common definition of struct |
| 18 | clk which unifies the framework-level accounting and infrastructure that |
| 19 | has traditionally been duplicated across a variety of platforms. Second |
| 20 | is a common implementation of the clk.h api, defined in |
| 21 | drivers/clk/clk.c. Finally there is struct clk_ops, whose operations |
| 22 | are invoked by the clk api implementation. |
| 23 | |
| 24 | The second half of the interface is comprised of the hardware-specific |
| 25 | callbacks registered with struct clk_ops and the corresponding |
| 26 | hardware-specific structures needed to model a particular clock. For |
| 27 | the remainder of this document any reference to a callback in struct |
| 28 | clk_ops, such as .enable or .set_rate, implies the hardware-specific |
| 29 | implementation of that code. Likewise, references to struct clk_foo |
| 30 | serve as a convenient shorthand for the implementation of the |
| 31 | hardware-specific bits for the hypothetical "foo" hardware. |
| 32 | |
| 33 | Tying the two halves of this interface together is struct clk_hw, which |
| 34 | is defined in struct clk_foo and pointed to within struct clk. This |
Sachin Kamat | 1354195 | 2013-06-10 10:02:39 +0530 | [diff] [blame] | 35 | allows for easy navigation between the two discrete halves of the common |
Mike Turquette | 69fe8a8 | 2012-03-15 23:11:18 -0700 | [diff] [blame] | 36 | clock interface. |
| 37 | |
| 38 | Part 2 - common data structures and api |
| 39 | |
| 40 | Below is the common struct clk definition from |
| 41 | include/linux/clk-private.h, modified for brevity: |
| 42 | |
| 43 | struct clk { |
| 44 | const char *name; |
| 45 | const struct clk_ops *ops; |
| 46 | struct clk_hw *hw; |
| 47 | char **parent_names; |
| 48 | struct clk **parents; |
| 49 | struct clk *parent; |
| 50 | struct hlist_head children; |
| 51 | struct hlist_node child_node; |
| 52 | ... |
| 53 | }; |
| 54 | |
| 55 | The members above make up the core of the clk tree topology. The clk |
| 56 | api itself defines several driver-facing functions which operate on |
| 57 | struct clk. That api is documented in include/linux/clk.h. |
| 58 | |
| 59 | Platforms and devices utilizing the common struct clk use the struct |
| 60 | clk_ops pointer in struct clk to perform the hardware-specific parts of |
| 61 | the operations defined in clk.h: |
| 62 | |
| 63 | struct clk_ops { |
| 64 | int (*prepare)(struct clk_hw *hw); |
| 65 | void (*unprepare)(struct clk_hw *hw); |
| 66 | int (*enable)(struct clk_hw *hw); |
| 67 | void (*disable)(struct clk_hw *hw); |
| 68 | int (*is_enabled)(struct clk_hw *hw); |
| 69 | unsigned long (*recalc_rate)(struct clk_hw *hw, |
| 70 | unsigned long parent_rate); |
| 71 | long (*round_rate)(struct clk_hw *hw, unsigned long, |
| 72 | unsigned long *); |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 73 | long (*determine_rate)(struct clk_hw *hw, |
| 74 | unsigned long rate, |
| 75 | unsigned long *best_parent_rate, |
| 76 | struct clk **best_parent_clk); |
Mike Turquette | 69fe8a8 | 2012-03-15 23:11:18 -0700 | [diff] [blame] | 77 | int (*set_parent)(struct clk_hw *hw, u8 index); |
| 78 | u8 (*get_parent)(struct clk_hw *hw); |
| 79 | int (*set_rate)(struct clk_hw *hw, unsigned long); |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame^] | 80 | int (*set_rate_and_parent)(struct clk_hw *hw, |
| 81 | unsigned long rate, |
| 82 | unsigned long parent_rate, u8 index); |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 83 | unsigned long (*recalc_accuracy)(struct clk_hw *hw, |
| 84 | unsigned long parent_accuracy); |
Mike Turquette | 69fe8a8 | 2012-03-15 23:11:18 -0700 | [diff] [blame] | 85 | void (*init)(struct clk_hw *hw); |
| 86 | }; |
| 87 | |
| 88 | Part 3 - hardware clk implementations |
| 89 | |
| 90 | The strength of the common struct clk comes from its .ops and .hw pointers |
| 91 | which abstract the details of struct clk from the hardware-specific bits, and |
| 92 | vice versa. To illustrate consider the simple gateable clk implementation in |
| 93 | drivers/clk/clk-gate.c: |
| 94 | |
| 95 | struct clk_gate { |
| 96 | struct clk_hw hw; |
| 97 | void __iomem *reg; |
| 98 | u8 bit_idx; |
| 99 | ... |
| 100 | }; |
| 101 | |
| 102 | struct clk_gate contains struct clk_hw hw as well as hardware-specific |
| 103 | knowledge about which register and bit controls this clk's gating. |
| 104 | Nothing about clock topology or accounting, such as enable_count or |
| 105 | notifier_count, is needed here. That is all handled by the common |
| 106 | framework code and struct clk. |
| 107 | |
| 108 | Let's walk through enabling this clk from driver code: |
| 109 | |
| 110 | struct clk *clk; |
| 111 | clk = clk_get(NULL, "my_gateable_clk"); |
| 112 | |
| 113 | clk_prepare(clk); |
| 114 | clk_enable(clk); |
| 115 | |
| 116 | The call graph for clk_enable is very simple: |
| 117 | |
| 118 | clk_enable(clk); |
| 119 | clk->ops->enable(clk->hw); |
| 120 | [resolves to...] |
| 121 | clk_gate_enable(hw); |
| 122 | [resolves struct clk gate with to_clk_gate(hw)] |
| 123 | clk_gate_set_bit(gate); |
| 124 | |
| 125 | And the definition of clk_gate_set_bit: |
| 126 | |
| 127 | static void clk_gate_set_bit(struct clk_gate *gate) |
| 128 | { |
| 129 | u32 reg; |
| 130 | |
| 131 | reg = __raw_readl(gate->reg); |
| 132 | reg |= BIT(gate->bit_idx); |
| 133 | writel(reg, gate->reg); |
| 134 | } |
| 135 | |
| 136 | Note that to_clk_gate is defined as: |
| 137 | |
| 138 | #define to_clk_gate(_hw) container_of(_hw, struct clk_gate, clk) |
| 139 | |
| 140 | This pattern of abstraction is used for every clock hardware |
| 141 | representation. |
| 142 | |
| 143 | Part 4 - supporting your own clk hardware |
| 144 | |
| 145 | When implementing support for a new type of clock it only necessary to |
| 146 | include the following header: |
| 147 | |
| 148 | #include <linux/clk-provider.h> |
| 149 | |
| 150 | include/linux/clk.h is included within that header and clk-private.h |
| 151 | must never be included from the code which implements the operations for |
| 152 | a clock. More on that below in Part 5. |
| 153 | |
| 154 | To construct a clk hardware structure for your platform you must define |
| 155 | the following: |
| 156 | |
| 157 | struct clk_foo { |
| 158 | struct clk_hw hw; |
| 159 | ... hardware specific data goes here ... |
| 160 | }; |
| 161 | |
| 162 | To take advantage of your data you'll need to support valid operations |
| 163 | for your clk: |
| 164 | |
| 165 | struct clk_ops clk_foo_ops { |
| 166 | .enable = &clk_foo_enable; |
| 167 | .disable = &clk_foo_disable; |
| 168 | }; |
| 169 | |
| 170 | Implement the above functions using container_of: |
| 171 | |
| 172 | #define to_clk_foo(_hw) container_of(_hw, struct clk_foo, hw) |
| 173 | |
| 174 | int clk_foo_enable(struct clk_hw *hw) |
| 175 | { |
| 176 | struct clk_foo *foo; |
| 177 | |
| 178 | foo = to_clk_foo(hw); |
| 179 | |
| 180 | ... perform magic on foo ... |
| 181 | |
| 182 | return 0; |
| 183 | }; |
| 184 | |
| 185 | Below is a matrix detailing which clk_ops are mandatory based upon the |
Eduardo Valentin | a368a6a | 2013-02-28 09:59:07 -0400 | [diff] [blame] | 186 | hardware capabilities of that clock. A cell marked as "y" means |
Mike Turquette | 69fe8a8 | 2012-03-15 23:11:18 -0700 | [diff] [blame] | 187 | mandatory, a cell marked as "n" implies that either including that |
Eduardo Valentin | a368a6a | 2013-02-28 09:59:07 -0400 | [diff] [blame] | 188 | callback is invalid or otherwise unnecessary. Empty cells are either |
Mike Turquette | 69fe8a8 | 2012-03-15 23:11:18 -0700 | [diff] [blame] | 189 | optional or must be evaluated on a case-by-case basis. |
| 190 | |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 191 | clock hardware characteristics |
| 192 | ----------------------------------------------------------- |
| 193 | | gate | change rate | single parent | multiplexer | root | |
| 194 | |------|-------------|---------------|-------------|------| |
| 195 | .prepare | | | | | | |
| 196 | .unprepare | | | | | | |
| 197 | | | | | | | |
| 198 | .enable | y | | | | | |
| 199 | .disable | y | | | | | |
| 200 | .is_enabled | y | | | | | |
| 201 | | | | | | | |
| 202 | .recalc_rate | | y | | | | |
| 203 | .round_rate | | y [1] | | | | |
| 204 | .determine_rate | | y [1] | | | | |
| 205 | .set_rate | | y | | | | |
| 206 | | | | | | | |
| 207 | .set_parent | | | n | y | n | |
| 208 | .get_parent | | | n | y | n | |
| 209 | | | | | | | |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 210 | .recalc_accuracy| | | | | | |
| 211 | | | | | | | |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 212 | .init | | | | | | |
| 213 | ----------------------------------------------------------- |
| 214 | [1] either one of round_rate or determine_rate is required. |
Mike Turquette | 69fe8a8 | 2012-03-15 23:11:18 -0700 | [diff] [blame] | 215 | |
| 216 | Finally, register your clock at run-time with a hardware-specific |
| 217 | registration function. This function simply populates struct clk_foo's |
| 218 | data and then passes the common struct clk parameters to the framework |
| 219 | with a call to: |
| 220 | |
| 221 | clk_register(...) |
| 222 | |
| 223 | See the basic clock types in drivers/clk/clk-*.c for examples. |
| 224 | |
| 225 | Part 5 - static initialization of clock data |
| 226 | |
| 227 | For platforms with many clocks (often numbering into the hundreds) it |
| 228 | may be desirable to statically initialize some clock data. This |
| 229 | presents a problem since the definition of struct clk should be hidden |
| 230 | from everyone except for the clock core in drivers/clk/clk.c. |
| 231 | |
| 232 | To get around this problem struct clk's definition is exposed in |
| 233 | include/linux/clk-private.h along with some macros for more easily |
| 234 | initializing instances of the basic clock types. These clocks must |
| 235 | still be initialized with the common clock framework via a call to |
| 236 | __clk_init. |
| 237 | |
| 238 | clk-private.h must NEVER be included by code which implements struct |
| 239 | clk_ops callbacks, nor must it be included by any logic which pokes |
| 240 | around inside of struct clk at run-time. To do so is a layering |
| 241 | violation. |
| 242 | |
| 243 | To better enforce this policy, always follow this simple rule: any |
| 244 | statically initialized clock data MUST be defined in a separate file |
| 245 | from the logic that implements its ops. Basically separate the logic |
| 246 | from the data and all is well. |
Olof Johansson | 1e43525 | 2013-04-27 14:10:18 -0700 | [diff] [blame] | 247 | |
| 248 | Part 6 - Disabling clock gating of unused clocks |
| 249 | |
| 250 | Sometimes during development it can be useful to be able to bypass the |
| 251 | default disabling of unused clocks. For example, if drivers aren't enabling |
| 252 | clocks properly but rely on them being on from the bootloader, bypassing |
| 253 | the disabling means that the driver will remain functional while the issues |
| 254 | are sorted out. |
| 255 | |
| 256 | To bypass this disabling, include "clk_ignore_unused" in the bootargs to the |
| 257 | kernel. |