clk-provider.h revision 27d545915fd49cbe18a3877d82359896e9851efb
1/*
2 *  linux/include/linux/clk-provider.h
3 *
4 *  Copyright (c) 2010-2011 Jeremy Kerr <jeremy.kerr@canonical.com>
5 *  Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#ifndef __LINUX_CLK_PROVIDER_H
12#define __LINUX_CLK_PROVIDER_H
13
14#include <linux/clk.h>
15
16#ifdef CONFIG_COMMON_CLK
17
18/**
19 * struct clk_hw - handle for traversing from a struct clk to its corresponding
20 * hardware-specific structure.  struct clk_hw should be declared within struct
21 * clk_foo and then referenced by the struct clk instance that uses struct
22 * clk_foo's clk_ops
23 *
24 * clk: pointer to the struct clk instance that points back to this struct
25 * clk_hw instance
26 */
27struct clk_hw {
28	struct clk *clk;
29};
30
31/*
32 * flags used across common struct clk.  these flags should only affect the
33 * top-level framework.  custom flags for dealing with hardware specifics
34 * belong in struct clk_foo
35 */
36#define CLK_SET_RATE_GATE	BIT(0) /* must be gated across rate change */
37#define CLK_SET_PARENT_GATE	BIT(1) /* must be gated across re-parent */
38#define CLK_SET_RATE_PARENT	BIT(2) /* propagate rate change up one level */
39#define CLK_IGNORE_UNUSED	BIT(3) /* do not gate even if unused */
40#define CLK_IS_ROOT		BIT(4) /* root clk, has no parent */
41
42/**
43 * struct clk_ops -  Callback operations for hardware clocks; these are to
44 * be provided by the clock implementation, and will be called by drivers
45 * through the clk_* api.
46 *
47 * @prepare:	Prepare the clock for enabling. This must not return until
48 * 		the clock is fully prepared, and it's safe to call clk_enable.
49 * 		This callback is intended to allow clock implementations to
50 * 		do any initialisation that may sleep. Called with
51 * 		prepare_lock held.
52 *
53 * @unprepare:	Release the clock from its prepared state. This will typically
54 * 		undo any work done in the @prepare callback. Called with
55 * 		prepare_lock held.
56 *
57 * @enable:	Enable the clock atomically. This must not return until the
58 * 		clock is generating a valid clock signal, usable by consumer
59 * 		devices. Called with enable_lock held. This function must not
60 * 		sleep.
61 *
62 * @disable:	Disable the clock atomically. Called with enable_lock held.
63 * 		This function must not sleep.
64 *
65 * @recalc_rate	Recalculate the rate of this clock, by quering hardware.  The
66 * 		parent rate is an input parameter.  It is up to the caller to
67 * 		insure that the prepare_mutex is held across this call.
68 * 		Returns the calculated rate.  Optional, but recommended - if
69 * 		this op is not set then clock rate will be initialized to 0.
70 *
71 * @round_rate:	Given a target rate as input, returns the closest rate actually
72 * 		supported by the clock.
73 *
74 * @get_parent:	Queries the hardware to determine the parent of a clock.  The
75 * 		return value is a u8 which specifies the index corresponding to
76 * 		the parent clock.  This index can be applied to either the
77 * 		.parent_names or .parents arrays.  In short, this function
78 * 		translates the parent value read from hardware into an array
79 * 		index.  Currently only called when the clock is initialized by
80 * 		__clk_init.  This callback is mandatory for clocks with
81 * 		multiple parents.  It is optional (and unnecessary) for clocks
82 * 		with 0 or 1 parents.
83 *
84 * @set_parent:	Change the input source of this clock; for clocks with multiple
85 * 		possible parents specify a new parent by passing in the index
86 * 		as a u8 corresponding to the parent in either the .parent_names
87 * 		or .parents arrays.  This function in affect translates an
88 * 		array index into the value programmed into the hardware.
89 * 		Returns 0 on success, -EERROR otherwise.
90 *
91 * @set_rate:	Change the rate of this clock. If this callback returns
92 * 		CLK_SET_RATE_PARENT, the rate change will be propagated to the
93 * 		parent clock (which may propagate again if the parent clock
94 * 		also sets this flag). The requested rate of the parent is
95 * 		passed back from the callback in the second 'unsigned long *'
96 * 		argument.  Note that it is up to the hardware clock's set_rate
97 * 		implementation to insure that clocks do not run out of spec
98 * 		when propgating the call to set_rate up to the parent.  One way
99 * 		to do this is to gate the clock (via clk_disable and/or
100 * 		clk_unprepare) before calling clk_set_rate, then ungating it
101 * 		afterward.  If your clock also has the CLK_GATE_SET_RATE flag
102 * 		set then this will insure safety.  Returns 0 on success,
103 * 		-EERROR otherwise.
104 *
105 * The clk_enable/clk_disable and clk_prepare/clk_unprepare pairs allow
106 * implementations to split any work between atomic (enable) and sleepable
107 * (prepare) contexts.  If enabling a clock requires code that might sleep,
108 * this must be done in clk_prepare.  Clock enable code that will never be
109 * called in a sleepable context may be implement in clk_enable.
110 *
111 * Typically, drivers will call clk_prepare when a clock may be needed later
112 * (eg. when a device is opened), and clk_enable when the clock is actually
113 * required (eg. from an interrupt). Note that clk_prepare MUST have been
114 * called before clk_enable.
115 */
116struct clk_ops {
117	int		(*prepare)(struct clk_hw *hw);
118	void		(*unprepare)(struct clk_hw *hw);
119	int		(*enable)(struct clk_hw *hw);
120	void		(*disable)(struct clk_hw *hw);
121	int		(*is_enabled)(struct clk_hw *hw);
122	unsigned long	(*recalc_rate)(struct clk_hw *hw,
123					unsigned long parent_rate);
124	long		(*round_rate)(struct clk_hw *hw, unsigned long,
125					unsigned long *);
126	int		(*set_parent)(struct clk_hw *hw, u8 index);
127	u8		(*get_parent)(struct clk_hw *hw);
128	int		(*set_rate)(struct clk_hw *hw, unsigned long);
129	void		(*init)(struct clk_hw *hw);
130};
131
132/*
133 * DOC: Basic clock implementations common to many platforms
134 *
135 * Each basic clock hardware type is comprised of a structure describing the
136 * clock hardware, implementations of the relevant callbacks in struct clk_ops,
137 * unique flags for that hardware type, a registration function and an
138 * alternative macro for static initialization
139 */
140
141/**
142 * struct clk_fixed_rate - fixed-rate clock
143 * @hw:		handle between common and hardware-specific interfaces
144 * @fixed_rate:	constant frequency of clock
145 */
146struct clk_fixed_rate {
147	struct		clk_hw hw;
148	unsigned long	fixed_rate;
149	u8		flags;
150};
151
152extern const struct clk_ops clk_fixed_rate_ops;
153struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
154		const char *parent_name, unsigned long flags,
155		unsigned long fixed_rate);
156
157/**
158 * struct clk_gate - gating clock
159 *
160 * @hw:		handle between common and hardware-specific interfaces
161 * @reg:	register controlling gate
162 * @bit_idx:	single bit controlling gate
163 * @flags:	hardware-specific flags
164 * @lock:	register lock
165 *
166 * Clock which can gate its output.  Implements .enable & .disable
167 *
168 * Flags:
169 * CLK_GATE_SET_DISABLE - by default this clock sets the bit at bit_idx to
170 * 	enable the clock.  Setting this flag does the opposite: setting the bit
171 * 	disable the clock and clearing it enables the clock
172 */
173struct clk_gate {
174	struct clk_hw hw;
175	void __iomem	*reg;
176	u8		bit_idx;
177	u8		flags;
178	spinlock_t	*lock;
179};
180
181#define CLK_GATE_SET_TO_DISABLE		BIT(0)
182
183extern const struct clk_ops clk_gate_ops;
184struct clk *clk_register_gate(struct device *dev, const char *name,
185		const char *parent_name, unsigned long flags,
186		void __iomem *reg, u8 bit_idx,
187		u8 clk_gate_flags, spinlock_t *lock);
188
189/**
190 * struct clk_divider - adjustable divider clock
191 *
192 * @hw:		handle between common and hardware-specific interfaces
193 * @reg:	register containing the divider
194 * @shift:	shift to the divider bit field
195 * @width:	width of the divider bit field
196 * @lock:	register lock
197 *
198 * Clock with an adjustable divider affecting its output frequency.  Implements
199 * .recalc_rate, .set_rate and .round_rate
200 *
201 * Flags:
202 * CLK_DIVIDER_ONE_BASED - by default the divisor is the value read from the
203 * 	register plus one.  If CLK_DIVIDER_ONE_BASED is set then the divider is
204 * 	the raw value read from the register, with the value of zero considered
205 * 	invalid
206 * CLK_DIVIDER_POWER_OF_TWO - clock divisor is 2 raised to the value read from
207 * 	the hardware register
208 */
209struct clk_divider {
210	struct clk_hw	hw;
211	void __iomem	*reg;
212	u8		shift;
213	u8		width;
214	u8		flags;
215	spinlock_t	*lock;
216};
217
218#define CLK_DIVIDER_ONE_BASED		BIT(0)
219#define CLK_DIVIDER_POWER_OF_TWO	BIT(1)
220
221extern const struct clk_ops clk_divider_ops;
222struct clk *clk_register_divider(struct device *dev, const char *name,
223		const char *parent_name, unsigned long flags,
224		void __iomem *reg, u8 shift, u8 width,
225		u8 clk_divider_flags, spinlock_t *lock);
226
227/**
228 * struct clk_mux - multiplexer clock
229 *
230 * @hw:		handle between common and hardware-specific interfaces
231 * @reg:	register controlling multiplexer
232 * @shift:	shift to multiplexer bit field
233 * @width:	width of mutliplexer bit field
234 * @num_clks:	number of parent clocks
235 * @lock:	register lock
236 *
237 * Clock with multiple selectable parents.  Implements .get_parent, .set_parent
238 * and .recalc_rate
239 *
240 * Flags:
241 * CLK_MUX_INDEX_ONE - register index starts at 1, not 0
242 * CLK_MUX_INDEX_BITWISE - register index is a single bit (power of two)
243 */
244struct clk_mux {
245	struct clk_hw	hw;
246	void __iomem	*reg;
247	u8		shift;
248	u8		width;
249	u8		flags;
250	spinlock_t	*lock;
251};
252
253#define CLK_MUX_INDEX_ONE		BIT(0)
254#define CLK_MUX_INDEX_BIT		BIT(1)
255
256extern const struct clk_ops clk_mux_ops;
257struct clk *clk_register_mux(struct device *dev, const char *name,
258		const char **parent_names, u8 num_parents, unsigned long flags,
259		void __iomem *reg, u8 shift, u8 width,
260		u8 clk_mux_flags, spinlock_t *lock);
261
262/**
263 * clk_register - allocate a new clock, register it and return an opaque cookie
264 * @dev: device that is registering this clock
265 * @name: clock name
266 * @ops: operations this clock supports
267 * @hw: link to hardware-specific clock data
268 * @parent_names: array of string names for all possible parents
269 * @num_parents: number of possible parents
270 * @flags: framework-level hints and quirks
271 *
272 * clk_register is the primary interface for populating the clock tree with new
273 * clock nodes.  It returns a pointer to the newly allocated struct clk which
274 * cannot be dereferenced by driver code but may be used in conjuction with the
275 * rest of the clock API.  In the event of an error clk_register will return an
276 * error code; drivers must test for an error code after calling clk_register.
277 */
278struct clk *clk_register(struct device *dev, const char *name,
279		const struct clk_ops *ops, struct clk_hw *hw,
280		const char **parent_names, u8 num_parents, unsigned long flags);
281
282/* helper functions */
283const char *__clk_get_name(struct clk *clk);
284struct clk_hw *__clk_get_hw(struct clk *clk);
285u8 __clk_get_num_parents(struct clk *clk);
286struct clk *__clk_get_parent(struct clk *clk);
287inline int __clk_get_enable_count(struct clk *clk);
288inline int __clk_get_prepare_count(struct clk *clk);
289unsigned long __clk_get_rate(struct clk *clk);
290unsigned long __clk_get_flags(struct clk *clk);
291int __clk_is_enabled(struct clk *clk);
292struct clk *__clk_lookup(const char *name);
293
294/*
295 * FIXME clock api without lock protection
296 */
297int __clk_prepare(struct clk *clk);
298void __clk_unprepare(struct clk *clk);
299void __clk_reparent(struct clk *clk, struct clk *new_parent);
300unsigned long __clk_round_rate(struct clk *clk, unsigned long rate);
301
302#endif /* CONFIG_COMMON_CLK */
303#endif /* CLK_PROVIDER_H */
304