• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1		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
35allows for easy navigation between the two discrete halves of the common
36clock 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,
72						unsigned long rate,
73						unsigned long *parent_rate);
74		int		(*determine_rate)(struct clk_hw *hw,
75						  struct clk_rate_request *req);
76		int		(*set_parent)(struct clk_hw *hw, u8 index);
77		u8		(*get_parent)(struct clk_hw *hw);
78		int		(*set_rate)(struct clk_hw *hw,
79					    unsigned long rate,
80					    unsigned long parent_rate);
81		int		(*set_rate_and_parent)(struct clk_hw *hw,
82					    unsigned long rate,
83					    unsigned long parent_rate,
84					    u8 index);
85		unsigned long	(*recalc_accuracy)(struct clk_hw *hw,
86						unsigned long parent_accuracy);
87		void		(*init)(struct clk_hw *hw);
88		int		(*debug_init)(struct clk_hw *hw,
89					      struct dentry *dentry);
90	};
91
92	Part 3 - hardware clk implementations
93
94The strength of the common struct clk comes from its .ops and .hw pointers
95which abstract the details of struct clk from the hardware-specific bits, and
96vice versa.  To illustrate consider the simple gateable clk implementation in
97drivers/clk/clk-gate.c:
98
99struct clk_gate {
100	struct clk_hw	hw;
101	void __iomem    *reg;
102	u8              bit_idx;
103	...
104};
105
106struct clk_gate contains struct clk_hw hw as well as hardware-specific
107knowledge about which register and bit controls this clk's gating.
108Nothing about clock topology or accounting, such as enable_count or
109notifier_count, is needed here.  That is all handled by the common
110framework code and struct clk.
111
112Let's walk through enabling this clk from driver code:
113
114	struct clk *clk;
115	clk = clk_get(NULL, "my_gateable_clk");
116
117	clk_prepare(clk);
118	clk_enable(clk);
119
120The call graph for clk_enable is very simple:
121
122clk_enable(clk);
123	clk->ops->enable(clk->hw);
124	[resolves to...]
125		clk_gate_enable(hw);
126		[resolves struct clk gate with to_clk_gate(hw)]
127			clk_gate_set_bit(gate);
128
129And the definition of clk_gate_set_bit:
130
131static void clk_gate_set_bit(struct clk_gate *gate)
132{
133	u32 reg;
134
135	reg = __raw_readl(gate->reg);
136	reg |= BIT(gate->bit_idx);
137	writel(reg, gate->reg);
138}
139
140Note that to_clk_gate is defined as:
141
142#define to_clk_gate(_hw) container_of(_hw, struct clk_gate, clk)
143
144This pattern of abstraction is used for every clock hardware
145representation.
146
147	Part 4 - supporting your own clk hardware
148
149When implementing support for a new type of clock it only necessary to
150include the following header:
151
152#include <linux/clk-provider.h>
153
154include/linux/clk.h is included within that header and clk-private.h
155must never be included from the code which implements the operations for
156a clock.  More on that below in Part 5.
157
158To construct a clk hardware structure for your platform you must define
159the following:
160
161struct clk_foo {
162	struct clk_hw hw;
163	... hardware specific data goes here ...
164};
165
166To take advantage of your data you'll need to support valid operations
167for your clk:
168
169struct clk_ops clk_foo_ops {
170	.enable		= &clk_foo_enable;
171	.disable	= &clk_foo_disable;
172};
173
174Implement the above functions using container_of:
175
176#define to_clk_foo(_hw) container_of(_hw, struct clk_foo, hw)
177
178int clk_foo_enable(struct clk_hw *hw)
179{
180	struct clk_foo *foo;
181
182	foo = to_clk_foo(hw);
183
184	... perform magic on foo ...
185
186	return 0;
187};
188
189Below is a matrix detailing which clk_ops are mandatory based upon the
190hardware capabilities of that clock.  A cell marked as "y" means
191mandatory, a cell marked as "n" implies that either including that
192callback is invalid or otherwise unnecessary.  Empty cells are either
193optional or must be evaluated on a case-by-case basis.
194
195                              clock hardware characteristics
196                -----------------------------------------------------------
197                | gate | change rate | single parent | multiplexer | root |
198                |------|-------------|---------------|-------------|------|
199.prepare        |      |             |               |             |      |
200.unprepare      |      |             |               |             |      |
201                |      |             |               |             |      |
202.enable         | y    |             |               |             |      |
203.disable        | y    |             |               |             |      |
204.is_enabled     | y    |             |               |             |      |
205                |      |             |               |             |      |
206.recalc_rate    |      | y           |               |             |      |
207.round_rate     |      | y [1]       |               |             |      |
208.determine_rate |      | y [1]       |               |             |      |
209.set_rate       |      | y           |               |             |      |
210                |      |             |               |             |      |
211.set_parent     |      |             | n             | y           | n    |
212.get_parent     |      |             | n             | y           | n    |
213                |      |             |               |             |      |
214.recalc_accuracy|      |             |               |             |      |
215                |      |             |               |             |      |
216.init           |      |             |               |             |      |
217                -----------------------------------------------------------
218[1] either one of round_rate or determine_rate is required.
219
220Finally, register your clock at run-time with a hardware-specific
221registration function.  This function simply populates struct clk_foo's
222data and then passes the common struct clk parameters to the framework
223with a call to:
224
225clk_register(...)
226
227See the basic clock types in drivers/clk/clk-*.c for examples.
228
229	Part 5 - Disabling clock gating of unused clocks
230
231Sometimes during development it can be useful to be able to bypass the
232default disabling of unused clocks. For example, if drivers aren't enabling
233clocks properly but rely on them being on from the bootloader, bypassing
234the disabling means that the driver will remain functional while the issues
235are sorted out.
236
237To bypass this disabling, include "clk_ignore_unused" in the bootargs to the
238kernel.
239
240	Part 6 - Locking
241
242The common clock framework uses two global locks, the prepare lock and the
243enable lock.
244
245The enable lock is a spinlock and is held across calls to the .enable,
246.disable and .is_enabled operations. Those operations are thus not allowed to
247sleep, and calls to the clk_enable(), clk_disable() and clk_is_enabled() API
248functions are allowed in atomic context.
249
250The prepare lock is a mutex and is held across calls to all other operations.
251All those operations are allowed to sleep, and calls to the corresponding API
252functions are not allowed in atomic context.
253
254This effectively divides operations in two groups from a locking perspective.
255
256Drivers don't need to manually protect resources shared between the operations
257of one group, regardless of whether those resources are shared by multiple
258clocks or not. However, access to resources that are shared between operations
259of the two groups needs to be protected by the drivers. An example of such a
260resource would be a register that controls both the clock rate and the clock
261enable/disable state.
262
263The clock framework is reentrant, in that a driver is allowed to call clock
264framework functions from within its implementation of clock operations. This
265can for instance cause a .set_rate operation of one clock being called from
266within the .set_rate operation of another clock. This case must be considered
267in the driver implementations, but the code flow is usually controlled by the
268driver in that case.
269
270Note that locking must also be considered when code outside of the common
271clock framework needs to access resources used by the clock operations. This
272is considered out of scope of this document.
273