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); |
Geert Uytterhoeven | 54e7301 | 2014-04-22 15:11:42 +0200 | [diff] [blame] | 71 | long (*round_rate)(struct clk_hw *hw, |
| 72 | unsigned long rate, |
| 73 | unsigned long *parent_rate); |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 74 | long (*determine_rate)(struct clk_hw *hw, |
| 75 | unsigned long rate, |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 76 | unsigned long min_rate, |
| 77 | unsigned long max_rate, |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 78 | unsigned long *best_parent_rate, |
Tomeu Vizoso | 646cafc | 2014-12-02 08:54:22 +0100 | [diff] [blame] | 79 | struct clk_hw **best_parent_clk); |
Mike Turquette | 69fe8a8 | 2012-03-15 23:11:18 -0700 | [diff] [blame] | 80 | int (*set_parent)(struct clk_hw *hw, u8 index); |
| 81 | u8 (*get_parent)(struct clk_hw *hw); |
Geert Uytterhoeven | 54e7301 | 2014-04-22 15:11:42 +0200 | [diff] [blame] | 82 | int (*set_rate)(struct clk_hw *hw, |
| 83 | unsigned long rate, |
| 84 | unsigned long parent_rate); |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 85 | int (*set_rate_and_parent)(struct clk_hw *hw, |
| 86 | unsigned long rate, |
Geert Uytterhoeven | 54e7301 | 2014-04-22 15:11:42 +0200 | [diff] [blame] | 87 | unsigned long parent_rate, |
| 88 | u8 index); |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 89 | unsigned long (*recalc_accuracy)(struct clk_hw *hw, |
Geert Uytterhoeven | 54e7301 | 2014-04-22 15:11:42 +0200 | [diff] [blame] | 90 | unsigned long parent_accuracy); |
Mike Turquette | 69fe8a8 | 2012-03-15 23:11:18 -0700 | [diff] [blame] | 91 | void (*init)(struct clk_hw *hw); |
Geert Uytterhoeven | 54e7301 | 2014-04-22 15:11:42 +0200 | [diff] [blame] | 92 | int (*debug_init)(struct clk_hw *hw, |
| 93 | struct dentry *dentry); |
Mike Turquette | 69fe8a8 | 2012-03-15 23:11:18 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | Part 3 - hardware clk implementations |
| 97 | |
| 98 | The strength of the common struct clk comes from its .ops and .hw pointers |
| 99 | which abstract the details of struct clk from the hardware-specific bits, and |
| 100 | vice versa. To illustrate consider the simple gateable clk implementation in |
| 101 | drivers/clk/clk-gate.c: |
| 102 | |
| 103 | struct clk_gate { |
| 104 | struct clk_hw hw; |
| 105 | void __iomem *reg; |
| 106 | u8 bit_idx; |
| 107 | ... |
| 108 | }; |
| 109 | |
| 110 | struct clk_gate contains struct clk_hw hw as well as hardware-specific |
| 111 | knowledge about which register and bit controls this clk's gating. |
| 112 | Nothing about clock topology or accounting, such as enable_count or |
| 113 | notifier_count, is needed here. That is all handled by the common |
| 114 | framework code and struct clk. |
| 115 | |
| 116 | Let's walk through enabling this clk from driver code: |
| 117 | |
| 118 | struct clk *clk; |
| 119 | clk = clk_get(NULL, "my_gateable_clk"); |
| 120 | |
| 121 | clk_prepare(clk); |
| 122 | clk_enable(clk); |
| 123 | |
| 124 | The call graph for clk_enable is very simple: |
| 125 | |
| 126 | clk_enable(clk); |
| 127 | clk->ops->enable(clk->hw); |
| 128 | [resolves to...] |
| 129 | clk_gate_enable(hw); |
| 130 | [resolves struct clk gate with to_clk_gate(hw)] |
| 131 | clk_gate_set_bit(gate); |
| 132 | |
| 133 | And the definition of clk_gate_set_bit: |
| 134 | |
| 135 | static void clk_gate_set_bit(struct clk_gate *gate) |
| 136 | { |
| 137 | u32 reg; |
| 138 | |
| 139 | reg = __raw_readl(gate->reg); |
| 140 | reg |= BIT(gate->bit_idx); |
| 141 | writel(reg, gate->reg); |
| 142 | } |
| 143 | |
| 144 | Note that to_clk_gate is defined as: |
| 145 | |
| 146 | #define to_clk_gate(_hw) container_of(_hw, struct clk_gate, clk) |
| 147 | |
| 148 | This pattern of abstraction is used for every clock hardware |
| 149 | representation. |
| 150 | |
| 151 | Part 4 - supporting your own clk hardware |
| 152 | |
| 153 | When implementing support for a new type of clock it only necessary to |
| 154 | include the following header: |
| 155 | |
| 156 | #include <linux/clk-provider.h> |
| 157 | |
| 158 | include/linux/clk.h is included within that header and clk-private.h |
| 159 | must never be included from the code which implements the operations for |
| 160 | a clock. More on that below in Part 5. |
| 161 | |
| 162 | To construct a clk hardware structure for your platform you must define |
| 163 | the following: |
| 164 | |
| 165 | struct clk_foo { |
| 166 | struct clk_hw hw; |
| 167 | ... hardware specific data goes here ... |
| 168 | }; |
| 169 | |
| 170 | To take advantage of your data you'll need to support valid operations |
| 171 | for your clk: |
| 172 | |
| 173 | struct clk_ops clk_foo_ops { |
| 174 | .enable = &clk_foo_enable; |
| 175 | .disable = &clk_foo_disable; |
| 176 | }; |
| 177 | |
| 178 | Implement the above functions using container_of: |
| 179 | |
| 180 | #define to_clk_foo(_hw) container_of(_hw, struct clk_foo, hw) |
| 181 | |
| 182 | int clk_foo_enable(struct clk_hw *hw) |
| 183 | { |
| 184 | struct clk_foo *foo; |
| 185 | |
| 186 | foo = to_clk_foo(hw); |
| 187 | |
| 188 | ... perform magic on foo ... |
| 189 | |
| 190 | return 0; |
| 191 | }; |
| 192 | |
| 193 | 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] | 194 | hardware capabilities of that clock. A cell marked as "y" means |
Mike Turquette | 69fe8a8 | 2012-03-15 23:11:18 -0700 | [diff] [blame] | 195 | mandatory, a cell marked as "n" implies that either including that |
Eduardo Valentin | a368a6a | 2013-02-28 09:59:07 -0400 | [diff] [blame] | 196 | callback is invalid or otherwise unnecessary. Empty cells are either |
Mike Turquette | 69fe8a8 | 2012-03-15 23:11:18 -0700 | [diff] [blame] | 197 | optional or must be evaluated on a case-by-case basis. |
| 198 | |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 199 | clock hardware characteristics |
| 200 | ----------------------------------------------------------- |
| 201 | | gate | change rate | single parent | multiplexer | root | |
| 202 | |------|-------------|---------------|-------------|------| |
| 203 | .prepare | | | | | | |
| 204 | .unprepare | | | | | | |
| 205 | | | | | | | |
| 206 | .enable | y | | | | | |
| 207 | .disable | y | | | | | |
| 208 | .is_enabled | y | | | | | |
| 209 | | | | | | | |
| 210 | .recalc_rate | | y | | | | |
| 211 | .round_rate | | y [1] | | | | |
| 212 | .determine_rate | | y [1] | | | | |
| 213 | .set_rate | | y | | | | |
| 214 | | | | | | | |
| 215 | .set_parent | | | n | y | n | |
| 216 | .get_parent | | | n | y | n | |
| 217 | | | | | | | |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 218 | .recalc_accuracy| | | | | | |
| 219 | | | | | | | |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 220 | .init | | | | | | |
| 221 | ----------------------------------------------------------- |
| 222 | [1] either one of round_rate or determine_rate is required. |
Mike Turquette | 69fe8a8 | 2012-03-15 23:11:18 -0700 | [diff] [blame] | 223 | |
| 224 | Finally, register your clock at run-time with a hardware-specific |
| 225 | registration function. This function simply populates struct clk_foo's |
| 226 | data and then passes the common struct clk parameters to the framework |
| 227 | with a call to: |
| 228 | |
| 229 | clk_register(...) |
| 230 | |
| 231 | See the basic clock types in drivers/clk/clk-*.c for examples. |
| 232 | |
| 233 | Part 5 - static initialization of clock data |
| 234 | |
| 235 | For platforms with many clocks (often numbering into the hundreds) it |
| 236 | may be desirable to statically initialize some clock data. This |
| 237 | presents a problem since the definition of struct clk should be hidden |
| 238 | from everyone except for the clock core in drivers/clk/clk.c. |
| 239 | |
| 240 | To get around this problem struct clk's definition is exposed in |
| 241 | include/linux/clk-private.h along with some macros for more easily |
| 242 | initializing instances of the basic clock types. These clocks must |
| 243 | still be initialized with the common clock framework via a call to |
| 244 | __clk_init. |
| 245 | |
| 246 | clk-private.h must NEVER be included by code which implements struct |
| 247 | clk_ops callbacks, nor must it be included by any logic which pokes |
| 248 | around inside of struct clk at run-time. To do so is a layering |
| 249 | violation. |
| 250 | |
| 251 | To better enforce this policy, always follow this simple rule: any |
| 252 | statically initialized clock data MUST be defined in a separate file |
| 253 | from the logic that implements its ops. Basically separate the logic |
| 254 | from the data and all is well. |
Olof Johansson | 1e43525 | 2013-04-27 14:10:18 -0700 | [diff] [blame] | 255 | |
| 256 | Part 6 - Disabling clock gating of unused clocks |
| 257 | |
| 258 | Sometimes during development it can be useful to be able to bypass the |
| 259 | default disabling of unused clocks. For example, if drivers aren't enabling |
| 260 | clocks properly but rely on them being on from the bootloader, bypassing |
| 261 | the disabling means that the driver will remain functional while the issues |
| 262 | are sorted out. |
| 263 | |
| 264 | To bypass this disabling, include "clk_ignore_unused" in the bootargs to the |
| 265 | kernel. |
Laurent Pinchart | 843bad8 | 2014-02-28 13:40:56 +0100 | [diff] [blame] | 266 | |
| 267 | Part 7 - Locking |
| 268 | |
| 269 | The common clock framework uses two global locks, the prepare lock and the |
| 270 | enable lock. |
| 271 | |
| 272 | The enable lock is a spinlock and is held across calls to the .enable, |
| 273 | .disable and .is_enabled operations. Those operations are thus not allowed to |
| 274 | sleep, and calls to the clk_enable(), clk_disable() and clk_is_enabled() API |
| 275 | functions are allowed in atomic context. |
| 276 | |
| 277 | The prepare lock is a mutex and is held across calls to all other operations. |
| 278 | All those operations are allowed to sleep, and calls to the corresponding API |
| 279 | functions are not allowed in atomic context. |
| 280 | |
| 281 | This effectively divides operations in two groups from a locking perspective. |
| 282 | |
| 283 | Drivers don't need to manually protect resources shared between the operations |
| 284 | of one group, regardless of whether those resources are shared by multiple |
| 285 | clocks or not. However, access to resources that are shared between operations |
| 286 | of the two groups needs to be protected by the drivers. An example of such a |
| 287 | resource would be a register that controls both the clock rate and the clock |
| 288 | enable/disable state. |
| 289 | |
| 290 | The clock framework is reentrant, in that a driver is allowed to call clock |
| 291 | framework functions from within its implementation of clock operations. This |
| 292 | can for instance cause a .set_rate operation of one clock being called from |
| 293 | within the .set_rate operation of another clock. This case must be considered |
| 294 | in the driver implementations, but the code flow is usually controlled by the |
| 295 | driver in that case. |
| 296 | |
| 297 | Note that locking must also be considered when code outside of the common |
| 298 | clock framework needs to access resources used by the clock operations. This |
| 299 | is considered out of scope of this document. |