xref: /openbmc/linux/include/linux/clk.h (revision b2476490ef11134b65544d8f062cff96c53e941b)
1f8ce2547SRussell King /*
2f8ce2547SRussell King  *  linux/include/linux/clk.h
3f8ce2547SRussell King  *
4f8ce2547SRussell King  *  Copyright (C) 2004 ARM Limited.
5f8ce2547SRussell King  *  Written by Deep Blue Solutions Limited.
6*b2476490SMike Turquette  *  Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
7f8ce2547SRussell King  *
8f8ce2547SRussell King  * This program is free software; you can redistribute it and/or modify
9f8ce2547SRussell King  * it under the terms of the GNU General Public License version 2 as
10f8ce2547SRussell King  * published by the Free Software Foundation.
11f8ce2547SRussell King  */
12686f8c5dSTodd Poynor #ifndef __LINUX_CLK_H
13686f8c5dSTodd Poynor #define __LINUX_CLK_H
14f8ce2547SRussell King 
1540d3e0f4SRussell King #include <linux/kernel.h>
16*b2476490SMike Turquette #include <linux/notifier.h>
1740d3e0f4SRussell King 
18f8ce2547SRussell King struct device;
19f8ce2547SRussell King 
20f8ce2547SRussell King struct clk;
21f8ce2547SRussell King 
22*b2476490SMike Turquette #ifdef CONFIG_COMMON_CLK
23*b2476490SMike Turquette 
24*b2476490SMike Turquette /**
25*b2476490SMike Turquette  * DOC: clk notifier callback types
26*b2476490SMike Turquette  *
27*b2476490SMike Turquette  * PRE_RATE_CHANGE - called immediately before the clk rate is changed,
28*b2476490SMike Turquette  *     to indicate that the rate change will proceed.  Drivers must
29*b2476490SMike Turquette  *     immediately terminate any operations that will be affected by the
30*b2476490SMike Turquette  *     rate change.  Callbacks may either return NOTIFY_DONE or
31*b2476490SMike Turquette  *     NOTIFY_STOP.
32*b2476490SMike Turquette  *
33*b2476490SMike Turquette  * ABORT_RATE_CHANGE: called if the rate change failed for some reason
34*b2476490SMike Turquette  *     after PRE_RATE_CHANGE.  In this case, all registered notifiers on
35*b2476490SMike Turquette  *     the clk will be called with ABORT_RATE_CHANGE. Callbacks must
36*b2476490SMike Turquette  *     always return NOTIFY_DONE.
37*b2476490SMike Turquette  *
38*b2476490SMike Turquette  * POST_RATE_CHANGE - called after the clk rate change has successfully
39*b2476490SMike Turquette  *     completed.  Callbacks must always return NOTIFY_DONE.
40*b2476490SMike Turquette  *
41*b2476490SMike Turquette  */
42*b2476490SMike Turquette #define PRE_RATE_CHANGE			BIT(0)
43*b2476490SMike Turquette #define POST_RATE_CHANGE		BIT(1)
44*b2476490SMike Turquette #define ABORT_RATE_CHANGE		BIT(2)
45*b2476490SMike Turquette 
46*b2476490SMike Turquette /**
47*b2476490SMike Turquette  * struct clk_notifier - associate a clk with a notifier
48*b2476490SMike Turquette  * @clk: struct clk * to associate the notifier with
49*b2476490SMike Turquette  * @notifier_head: a blocking_notifier_head for this clk
50*b2476490SMike Turquette  * @node: linked list pointers
51*b2476490SMike Turquette  *
52*b2476490SMike Turquette  * A list of struct clk_notifier is maintained by the notifier code.
53*b2476490SMike Turquette  * An entry is created whenever code registers the first notifier on a
54*b2476490SMike Turquette  * particular @clk.  Future notifiers on that @clk are added to the
55*b2476490SMike Turquette  * @notifier_head.
56*b2476490SMike Turquette  */
57*b2476490SMike Turquette struct clk_notifier {
58*b2476490SMike Turquette 	struct clk			*clk;
59*b2476490SMike Turquette 	struct srcu_notifier_head	notifier_head;
60*b2476490SMike Turquette 	struct list_head		node;
61*b2476490SMike Turquette };
62*b2476490SMike Turquette 
63*b2476490SMike Turquette /**
64*b2476490SMike Turquette  * struct clk_notifier_data - rate data to pass to the notifier callback
65*b2476490SMike Turquette  * @clk: struct clk * being changed
66*b2476490SMike Turquette  * @old_rate: previous rate of this clk
67*b2476490SMike Turquette  * @new_rate: new rate of this clk
68*b2476490SMike Turquette  *
69*b2476490SMike Turquette  * For a pre-notifier, old_rate is the clk's rate before this rate
70*b2476490SMike Turquette  * change, and new_rate is what the rate will be in the future.  For a
71*b2476490SMike Turquette  * post-notifier, old_rate and new_rate are both set to the clk's
72*b2476490SMike Turquette  * current rate (this was done to optimize the implementation).
73*b2476490SMike Turquette  */
74*b2476490SMike Turquette struct clk_notifier_data {
75*b2476490SMike Turquette 	struct clk		*clk;
76*b2476490SMike Turquette 	unsigned long		old_rate;
77*b2476490SMike Turquette 	unsigned long		new_rate;
78*b2476490SMike Turquette };
79*b2476490SMike Turquette 
80*b2476490SMike Turquette int clk_notifier_register(struct clk *clk, struct notifier_block *nb);
81*b2476490SMike Turquette 
82*b2476490SMike Turquette int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb);
83*b2476490SMike Turquette 
84*b2476490SMike Turquette #endif /* !CONFIG_COMMON_CLK */
85*b2476490SMike Turquette 
86f8ce2547SRussell King /**
87f8ce2547SRussell King  * clk_get - lookup and obtain a reference to a clock producer.
88f8ce2547SRussell King  * @dev: device for clock "consumer"
89f8ce2547SRussell King  * @id: clock comsumer ID
90f8ce2547SRussell King  *
91f8ce2547SRussell King  * Returns a struct clk corresponding to the clock producer, or
92f8ce2547SRussell King  * valid IS_ERR() condition containing errno.  The implementation
93f8ce2547SRussell King  * uses @dev and @id to determine the clock consumer, and thereby
94f8ce2547SRussell King  * the clock producer.  (IOW, @id may be identical strings, but
95f8ce2547SRussell King  * clk_get may return different clock producers depending on @dev.)
96f8ce2547SRussell King  *
97f8ce2547SRussell King  * Drivers must assume that the clock source is not enabled.
98f7ad160bSAlex Raimondi  *
99f7ad160bSAlex Raimondi  * clk_get should not be called from within interrupt context.
100f8ce2547SRussell King  */
101f8ce2547SRussell King struct clk *clk_get(struct device *dev, const char *id);
102f8ce2547SRussell King 
103f8ce2547SRussell King /**
10440d3e0f4SRussell King  * clk_prepare - prepare a clock source
10540d3e0f4SRussell King  * @clk: clock source
10640d3e0f4SRussell King  *
10740d3e0f4SRussell King  * This prepares the clock source for use.
10840d3e0f4SRussell King  *
10940d3e0f4SRussell King  * Must not be called from within atomic context.
11040d3e0f4SRussell King  */
11140d3e0f4SRussell King #ifdef CONFIG_HAVE_CLK_PREPARE
11240d3e0f4SRussell King int clk_prepare(struct clk *clk);
11340d3e0f4SRussell King #else
11440d3e0f4SRussell King static inline int clk_prepare(struct clk *clk)
11540d3e0f4SRussell King {
11640d3e0f4SRussell King 	might_sleep();
11740d3e0f4SRussell King 	return 0;
11840d3e0f4SRussell King }
11940d3e0f4SRussell King #endif
12040d3e0f4SRussell King 
12140d3e0f4SRussell King /**
122f8ce2547SRussell King  * clk_enable - inform the system when the clock source should be running.
123f8ce2547SRussell King  * @clk: clock source
124f8ce2547SRussell King  *
125f8ce2547SRussell King  * If the clock can not be enabled/disabled, this should return success.
126f8ce2547SRussell King  *
12740d3e0f4SRussell King  * May be called from atomic contexts.
12840d3e0f4SRussell King  *
129f8ce2547SRussell King  * Returns success (0) or negative errno.
130f8ce2547SRussell King  */
131f8ce2547SRussell King int clk_enable(struct clk *clk);
132f8ce2547SRussell King 
133f8ce2547SRussell King /**
134f8ce2547SRussell King  * clk_disable - inform the system when the clock source is no longer required.
135f8ce2547SRussell King  * @clk: clock source
136f8ce2547SRussell King  *
137f8ce2547SRussell King  * Inform the system that a clock source is no longer required by
138f8ce2547SRussell King  * a driver and may be shut down.
139f8ce2547SRussell King  *
14040d3e0f4SRussell King  * May be called from atomic contexts.
14140d3e0f4SRussell King  *
142f8ce2547SRussell King  * Implementation detail: if the clock source is shared between
143f8ce2547SRussell King  * multiple drivers, clk_enable() calls must be balanced by the
144f8ce2547SRussell King  * same number of clk_disable() calls for the clock source to be
145f8ce2547SRussell King  * disabled.
146f8ce2547SRussell King  */
147f8ce2547SRussell King void clk_disable(struct clk *clk);
148f8ce2547SRussell King 
14940d3e0f4SRussell King 
15040d3e0f4SRussell King /**
15140d3e0f4SRussell King  * clk_unprepare - undo preparation of a clock source
15240d3e0f4SRussell King  * @clk: clock source
15340d3e0f4SRussell King  *
15440d3e0f4SRussell King  * This undoes a previously prepared clock.  The caller must balance
15540d3e0f4SRussell King  * the number of prepare and unprepare calls.
15640d3e0f4SRussell King  *
15740d3e0f4SRussell King  * Must not be called from within atomic context.
15840d3e0f4SRussell King  */
15940d3e0f4SRussell King #ifdef CONFIG_HAVE_CLK_PREPARE
16040d3e0f4SRussell King void clk_unprepare(struct clk *clk);
16140d3e0f4SRussell King #else
16240d3e0f4SRussell King static inline void clk_unprepare(struct clk *clk)
16340d3e0f4SRussell King {
16440d3e0f4SRussell King 	might_sleep();
16540d3e0f4SRussell King }
16640d3e0f4SRussell King #endif
16740d3e0f4SRussell King 
16842c5d52fSRichard Zhao /* clk_prepare_enable helps cases using clk_enable in non-atomic context. */
16942c5d52fSRichard Zhao static inline int clk_prepare_enable(struct clk *clk)
17042c5d52fSRichard Zhao {
17142c5d52fSRichard Zhao 	int ret;
17242c5d52fSRichard Zhao 
17342c5d52fSRichard Zhao 	ret = clk_prepare(clk);
17442c5d52fSRichard Zhao 	if (ret)
17542c5d52fSRichard Zhao 		return ret;
17642c5d52fSRichard Zhao 	ret = clk_enable(clk);
17742c5d52fSRichard Zhao 	if (ret)
17842c5d52fSRichard Zhao 		clk_unprepare(clk);
17942c5d52fSRichard Zhao 
18042c5d52fSRichard Zhao 	return ret;
18142c5d52fSRichard Zhao }
18242c5d52fSRichard Zhao 
18342c5d52fSRichard Zhao /* clk_disable_unprepare helps cases using clk_disable in non-atomic context. */
18442c5d52fSRichard Zhao static inline void clk_disable_unprepare(struct clk *clk)
18542c5d52fSRichard Zhao {
18642c5d52fSRichard Zhao 	clk_disable(clk);
18742c5d52fSRichard Zhao 	clk_unprepare(clk);
18842c5d52fSRichard Zhao }
18942c5d52fSRichard Zhao 
190f8ce2547SRussell King /**
191f8ce2547SRussell King  * clk_get_rate - obtain the current clock rate (in Hz) for a clock source.
192f8ce2547SRussell King  *		  This is only valid once the clock source has been enabled.
193f8ce2547SRussell King  * @clk: clock source
194f8ce2547SRussell King  */
195f8ce2547SRussell King unsigned long clk_get_rate(struct clk *clk);
196f8ce2547SRussell King 
197f8ce2547SRussell King /**
198f8ce2547SRussell King  * clk_put	- "free" the clock source
199f8ce2547SRussell King  * @clk: clock source
200f8ce2547SRussell King  *
201f8ce2547SRussell King  * Note: drivers must ensure that all clk_enable calls made on this
202f8ce2547SRussell King  * clock source are balanced by clk_disable calls prior to calling
203f8ce2547SRussell King  * this function.
204f7ad160bSAlex Raimondi  *
205f7ad160bSAlex Raimondi  * clk_put should not be called from within interrupt context.
206f8ce2547SRussell King  */
207f8ce2547SRussell King void clk_put(struct clk *clk);
208f8ce2547SRussell King 
209f8ce2547SRussell King 
210f8ce2547SRussell King /*
211f8ce2547SRussell King  * The remaining APIs are optional for machine class support.
212f8ce2547SRussell King  */
213f8ce2547SRussell King 
214f8ce2547SRussell King 
215f8ce2547SRussell King /**
216f8ce2547SRussell King  * clk_round_rate - adjust a rate to the exact rate a clock can provide
217f8ce2547SRussell King  * @clk: clock source
218f8ce2547SRussell King  * @rate: desired clock rate in Hz
219f8ce2547SRussell King  *
220f8ce2547SRussell King  * Returns rounded clock rate in Hz, or negative errno.
221f8ce2547SRussell King  */
222f8ce2547SRussell King long clk_round_rate(struct clk *clk, unsigned long rate);
223f8ce2547SRussell King 
224f8ce2547SRussell King /**
225f8ce2547SRussell King  * clk_set_rate - set the clock rate for a clock source
226f8ce2547SRussell King  * @clk: clock source
227f8ce2547SRussell King  * @rate: desired clock rate in Hz
228f8ce2547SRussell King  *
229f8ce2547SRussell King  * Returns success (0) or negative errno.
230f8ce2547SRussell King  */
231f8ce2547SRussell King int clk_set_rate(struct clk *clk, unsigned long rate);
232f8ce2547SRussell King 
233f8ce2547SRussell King /**
234f8ce2547SRussell King  * clk_set_parent - set the parent clock source for this clock
235f8ce2547SRussell King  * @clk: clock source
236f8ce2547SRussell King  * @parent: parent clock source
237f8ce2547SRussell King  *
238f8ce2547SRussell King  * Returns success (0) or negative errno.
239f8ce2547SRussell King  */
240f8ce2547SRussell King int clk_set_parent(struct clk *clk, struct clk *parent);
241f8ce2547SRussell King 
242f8ce2547SRussell King /**
243f8ce2547SRussell King  * clk_get_parent - get the parent clock source for this clock
244f8ce2547SRussell King  * @clk: clock source
245f8ce2547SRussell King  *
246f8ce2547SRussell King  * Returns struct clk corresponding to parent clock source, or
247f8ce2547SRussell King  * valid IS_ERR() condition containing errno.
248f8ce2547SRussell King  */
249f8ce2547SRussell King struct clk *clk_get_parent(struct clk *clk);
250f8ce2547SRussell King 
25105fd8e73SSascha Hauer /**
25205fd8e73SSascha Hauer  * clk_get_sys - get a clock based upon the device name
25305fd8e73SSascha Hauer  * @dev_id: device name
25405fd8e73SSascha Hauer  * @con_id: connection ID
25505fd8e73SSascha Hauer  *
25605fd8e73SSascha Hauer  * Returns a struct clk corresponding to the clock producer, or
25705fd8e73SSascha Hauer  * valid IS_ERR() condition containing errno.  The implementation
25805fd8e73SSascha Hauer  * uses @dev_id and @con_id to determine the clock consumer, and
25905fd8e73SSascha Hauer  * thereby the clock producer. In contrast to clk_get() this function
26005fd8e73SSascha Hauer  * takes the device name instead of the device itself for identification.
26105fd8e73SSascha Hauer  *
26205fd8e73SSascha Hauer  * Drivers must assume that the clock source is not enabled.
26305fd8e73SSascha Hauer  *
26405fd8e73SSascha Hauer  * clk_get_sys should not be called from within interrupt context.
26505fd8e73SSascha Hauer  */
26605fd8e73SSascha Hauer struct clk *clk_get_sys(const char *dev_id, const char *con_id);
26705fd8e73SSascha Hauer 
268c0683039STony Lindgren /**
269c0683039STony Lindgren  * clk_add_alias - add a new clock alias
270c0683039STony Lindgren  * @alias: name for clock alias
271c0683039STony Lindgren  * @alias_dev_name: device name
272c0683039STony Lindgren  * @id: platform specific clock name
273c0683039STony Lindgren  * @dev: device
274c0683039STony Lindgren  *
275c0683039STony Lindgren  * Allows using generic clock names for drivers by adding a new alias.
276c0683039STony Lindgren  * Assumes clkdev, see clkdev.h for more info.
277c0683039STony Lindgren  */
278c0683039STony Lindgren int clk_add_alias(const char *alias, const char *alias_dev_name, char *id,
279c0683039STony Lindgren 			struct device *dev);
280c0683039STony Lindgren 
281f8ce2547SRussell King #endif
282