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