xref: /openbmc/linux/include/linux/clk.h (revision 6d30d50d037dfa092f9d5d1fffa348ab4abb7163)
1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
2f8ce2547SRussell King /*
3f8ce2547SRussell King  *  linux/include/linux/clk.h
4f8ce2547SRussell King  *
5f8ce2547SRussell King  *  Copyright (C) 2004 ARM Limited.
6f8ce2547SRussell King  *  Written by Deep Blue Solutions Limited.
7b2476490SMike Turquette  *  Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
8f8ce2547SRussell King  */
9686f8c5dSTodd Poynor #ifndef __LINUX_CLK_H
10686f8c5dSTodd Poynor #define __LINUX_CLK_H
11f8ce2547SRussell King 
129f1612d3SShawn Guo #include <linux/err.h>
1340d3e0f4SRussell King #include <linux/kernel.h>
14b2476490SMike Turquette #include <linux/notifier.h>
1540d3e0f4SRussell King 
16f8ce2547SRussell King struct device;
17f8ce2547SRussell King struct clk;
1871a2f115SKuninori Morimoto struct device_node;
1971a2f115SKuninori Morimoto struct of_phandle_args;
20f8ce2547SRussell King 
21b2476490SMike Turquette /**
22b2476490SMike Turquette  * DOC: clk notifier callback types
23b2476490SMike Turquette  *
24b2476490SMike Turquette  * PRE_RATE_CHANGE - called immediately before the clk rate is changed,
25b2476490SMike Turquette  *     to indicate that the rate change will proceed.  Drivers must
26b2476490SMike Turquette  *     immediately terminate any operations that will be affected by the
27fb72a059SSoren Brinkmann  *     rate change.  Callbacks may either return NOTIFY_DONE, NOTIFY_OK,
28fb72a059SSoren Brinkmann  *     NOTIFY_STOP or NOTIFY_BAD.
29b2476490SMike Turquette  *
30b2476490SMike Turquette  * ABORT_RATE_CHANGE: called if the rate change failed for some reason
31b2476490SMike Turquette  *     after PRE_RATE_CHANGE.  In this case, all registered notifiers on
32b2476490SMike Turquette  *     the clk will be called with ABORT_RATE_CHANGE. Callbacks must
33fb72a059SSoren Brinkmann  *     always return NOTIFY_DONE or NOTIFY_OK.
34b2476490SMike Turquette  *
35b2476490SMike Turquette  * POST_RATE_CHANGE - called after the clk rate change has successfully
36fb72a059SSoren Brinkmann  *     completed.  Callbacks must always return NOTIFY_DONE or NOTIFY_OK.
37b2476490SMike Turquette  *
38b2476490SMike Turquette  */
39b2476490SMike Turquette #define PRE_RATE_CHANGE			BIT(0)
40b2476490SMike Turquette #define POST_RATE_CHANGE		BIT(1)
41b2476490SMike Turquette #define ABORT_RATE_CHANGE		BIT(2)
42b2476490SMike Turquette 
43b2476490SMike Turquette /**
44b2476490SMike Turquette  * struct clk_notifier - associate a clk with a notifier
45b2476490SMike Turquette  * @clk: struct clk * to associate the notifier with
46b2476490SMike Turquette  * @notifier_head: a blocking_notifier_head for this clk
47b2476490SMike Turquette  * @node: linked list pointers
48b2476490SMike Turquette  *
49b2476490SMike Turquette  * A list of struct clk_notifier is maintained by the notifier code.
50b2476490SMike Turquette  * An entry is created whenever code registers the first notifier on a
51b2476490SMike Turquette  * particular @clk.  Future notifiers on that @clk are added to the
52b2476490SMike Turquette  * @notifier_head.
53b2476490SMike Turquette  */
54b2476490SMike Turquette struct clk_notifier {
55b2476490SMike Turquette 	struct clk			*clk;
56b2476490SMike Turquette 	struct srcu_notifier_head	notifier_head;
57b2476490SMike Turquette 	struct list_head		node;
58b2476490SMike Turquette };
59b2476490SMike Turquette 
60b2476490SMike Turquette /**
61b2476490SMike Turquette  * struct clk_notifier_data - rate data to pass to the notifier callback
62b2476490SMike Turquette  * @clk: struct clk * being changed
63b2476490SMike Turquette  * @old_rate: previous rate of this clk
64b2476490SMike Turquette  * @new_rate: new rate of this clk
65b2476490SMike Turquette  *
66b2476490SMike Turquette  * For a pre-notifier, old_rate is the clk's rate before this rate
67b2476490SMike Turquette  * change, and new_rate is what the rate will be in the future.  For a
68b2476490SMike Turquette  * post-notifier, old_rate and new_rate are both set to the clk's
69b2476490SMike Turquette  * current rate (this was done to optimize the implementation).
70b2476490SMike Turquette  */
71b2476490SMike Turquette struct clk_notifier_data {
72b2476490SMike Turquette 	struct clk		*clk;
73b2476490SMike Turquette 	unsigned long		old_rate;
74b2476490SMike Turquette 	unsigned long		new_rate;
75b2476490SMike Turquette };
76b2476490SMike Turquette 
77266e4e9dSDong Aisheng /**
78266e4e9dSDong Aisheng  * struct clk_bulk_data - Data used for bulk clk operations.
79266e4e9dSDong Aisheng  *
80266e4e9dSDong Aisheng  * @id: clock consumer ID
81266e4e9dSDong Aisheng  * @clk: struct clk * to store the associated clock
82266e4e9dSDong Aisheng  *
83266e4e9dSDong Aisheng  * The CLK APIs provide a series of clk_bulk_() API calls as
84266e4e9dSDong Aisheng  * a convenience to consumers which require multiple clks.  This
85266e4e9dSDong Aisheng  * structure is used to manage data for these calls.
86266e4e9dSDong Aisheng  */
87266e4e9dSDong Aisheng struct clk_bulk_data {
88266e4e9dSDong Aisheng 	const char		*id;
89266e4e9dSDong Aisheng 	struct clk		*clk;
90266e4e9dSDong Aisheng };
91266e4e9dSDong Aisheng 
92e81b87d2SKrzysztof Kozlowski #ifdef CONFIG_COMMON_CLK
93e81b87d2SKrzysztof Kozlowski 
9486bcfa2eSMike Turquette /**
9586bcfa2eSMike Turquette  * clk_notifier_register: register a clock rate-change notifier callback
9686bcfa2eSMike Turquette  * @clk: clock whose rate we are interested in
9786bcfa2eSMike Turquette  * @nb: notifier block with callback function pointer
9886bcfa2eSMike Turquette  *
9986bcfa2eSMike Turquette  * ProTip: debugging across notifier chains can be frustrating. Make sure that
10086bcfa2eSMike Turquette  * your notifier callback function prints a nice big warning in case of
10186bcfa2eSMike Turquette  * failure.
10286bcfa2eSMike Turquette  */
103b2476490SMike Turquette int clk_notifier_register(struct clk *clk, struct notifier_block *nb);
104b2476490SMike Turquette 
10586bcfa2eSMike Turquette /**
10686bcfa2eSMike Turquette  * clk_notifier_unregister: unregister a clock rate-change notifier callback
10786bcfa2eSMike Turquette  * @clk: clock whose rate we are no longer interested in
10886bcfa2eSMike Turquette  * @nb: notifier block which will be unregistered
10986bcfa2eSMike Turquette  */
110b2476490SMike Turquette int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb);
111b2476490SMike Turquette 
1125279fc40SBoris BREZILLON /**
113*6d30d50dSJerome Brunet  * devm_clk_notifier_register - register a managed rate-change notifier callback
114*6d30d50dSJerome Brunet  * @dev: device for clock "consumer"
115*6d30d50dSJerome Brunet  * @clk: clock whose rate we are interested in
116*6d30d50dSJerome Brunet  * @nb: notifier block with callback function pointer
117*6d30d50dSJerome Brunet  *
118*6d30d50dSJerome Brunet  * Returns 0 on success, -EERROR otherwise
119*6d30d50dSJerome Brunet  */
120*6d30d50dSJerome Brunet int devm_clk_notifier_register(struct device *dev, struct clk *clk, struct notifier_block *nb);
121*6d30d50dSJerome Brunet 
122*6d30d50dSJerome Brunet /**
1235279fc40SBoris BREZILLON  * clk_get_accuracy - obtain the clock accuracy in ppb (parts per billion)
1245279fc40SBoris BREZILLON  *		      for a clock source.
1255279fc40SBoris BREZILLON  * @clk: clock source
1265279fc40SBoris BREZILLON  *
1275279fc40SBoris BREZILLON  * This gets the clock source accuracy expressed in ppb.
1285279fc40SBoris BREZILLON  * A perfect clock returns 0.
1295279fc40SBoris BREZILLON  */
1305279fc40SBoris BREZILLON long clk_get_accuracy(struct clk *clk);
1315279fc40SBoris BREZILLON 
132e59c5371SMike Turquette /**
133e59c5371SMike Turquette  * clk_set_phase - adjust the phase shift of a clock signal
134e59c5371SMike Turquette  * @clk: clock signal source
135e59c5371SMike Turquette  * @degrees: number of degrees the signal is shifted
136e59c5371SMike Turquette  *
137e59c5371SMike Turquette  * Shifts the phase of a clock signal by the specified degrees. Returns 0 on
138e59c5371SMike Turquette  * success, -EERROR otherwise.
139e59c5371SMike Turquette  */
140e59c5371SMike Turquette int clk_set_phase(struct clk *clk, int degrees);
141e59c5371SMike Turquette 
142e59c5371SMike Turquette /**
143e59c5371SMike Turquette  * clk_get_phase - return the phase shift of a clock signal
144e59c5371SMike Turquette  * @clk: clock signal source
145e59c5371SMike Turquette  *
146e59c5371SMike Turquette  * Returns the phase shift of a clock node in degrees, otherwise returns
147e59c5371SMike Turquette  * -EERROR.
148e59c5371SMike Turquette  */
149e59c5371SMike Turquette int clk_get_phase(struct clk *clk);
150e59c5371SMike Turquette 
1513d3801efSMichael Turquette /**
1529fba738aSJerome Brunet  * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
1539fba738aSJerome Brunet  * @clk: clock signal source
1549fba738aSJerome Brunet  * @num: numerator of the duty cycle ratio to be applied
1559fba738aSJerome Brunet  * @den: denominator of the duty cycle ratio to be applied
1569fba738aSJerome Brunet  *
1579fba738aSJerome Brunet  * Adjust the duty cycle of a clock signal by the specified ratio. Returns 0 on
1589fba738aSJerome Brunet  * success, -EERROR otherwise.
1599fba738aSJerome Brunet  */
1609fba738aSJerome Brunet int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den);
1619fba738aSJerome Brunet 
1629fba738aSJerome Brunet /**
1639fba738aSJerome Brunet  * clk_get_duty_cycle - return the duty cycle ratio of a clock signal
1649fba738aSJerome Brunet  * @clk: clock signal source
1659fba738aSJerome Brunet  * @scale: scaling factor to be applied to represent the ratio as an integer
1669fba738aSJerome Brunet  *
1679fba738aSJerome Brunet  * Returns the duty cycle ratio multiplied by the scale provided, otherwise
1689fba738aSJerome Brunet  * returns -EERROR.
1699fba738aSJerome Brunet  */
1709fba738aSJerome Brunet int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale);
1719fba738aSJerome Brunet 
1729fba738aSJerome Brunet /**
1733d3801efSMichael Turquette  * clk_is_match - check if two clk's point to the same hardware clock
1743d3801efSMichael Turquette  * @p: clk compared against q
1753d3801efSMichael Turquette  * @q: clk compared against p
1763d3801efSMichael Turquette  *
1773d3801efSMichael Turquette  * Returns true if the two struct clk pointers both point to the same hardware
1780e056eb5Smchehab@s-opensource.com  * clock node. Put differently, returns true if @p and @q
1790e056eb5Smchehab@s-opensource.com  * share the same &struct clk_core object.
1803d3801efSMichael Turquette  *
1813d3801efSMichael Turquette  * Returns false otherwise. Note that two NULL clks are treated as matching.
1823d3801efSMichael Turquette  */
1833d3801efSMichael Turquette bool clk_is_match(const struct clk *p, const struct clk *q);
1843d3801efSMichael Turquette 
1855279fc40SBoris BREZILLON #else
1865279fc40SBoris BREZILLON 
187e81b87d2SKrzysztof Kozlowski static inline int clk_notifier_register(struct clk *clk,
188e81b87d2SKrzysztof Kozlowski 					struct notifier_block *nb)
189e81b87d2SKrzysztof Kozlowski {
190e81b87d2SKrzysztof Kozlowski 	return -ENOTSUPP;
191e81b87d2SKrzysztof Kozlowski }
192e81b87d2SKrzysztof Kozlowski 
193e81b87d2SKrzysztof Kozlowski static inline int clk_notifier_unregister(struct clk *clk,
194e81b87d2SKrzysztof Kozlowski 					  struct notifier_block *nb)
195e81b87d2SKrzysztof Kozlowski {
196e81b87d2SKrzysztof Kozlowski 	return -ENOTSUPP;
197e81b87d2SKrzysztof Kozlowski }
198e81b87d2SKrzysztof Kozlowski 
1995279fc40SBoris BREZILLON static inline long clk_get_accuracy(struct clk *clk)
2005279fc40SBoris BREZILLON {
2015279fc40SBoris BREZILLON 	return -ENOTSUPP;
2025279fc40SBoris BREZILLON }
2035279fc40SBoris BREZILLON 
204e59c5371SMike Turquette static inline long clk_set_phase(struct clk *clk, int phase)
205e59c5371SMike Turquette {
206e59c5371SMike Turquette 	return -ENOTSUPP;
207e59c5371SMike Turquette }
208e59c5371SMike Turquette 
209e59c5371SMike Turquette static inline long clk_get_phase(struct clk *clk)
210e59c5371SMike Turquette {
211e59c5371SMike Turquette 	return -ENOTSUPP;
212e59c5371SMike Turquette }
213e59c5371SMike Turquette 
2149fba738aSJerome Brunet static inline int clk_set_duty_cycle(struct clk *clk, unsigned int num,
2159fba738aSJerome Brunet 				     unsigned int den)
2169fba738aSJerome Brunet {
2179fba738aSJerome Brunet 	return -ENOTSUPP;
2189fba738aSJerome Brunet }
2199fba738aSJerome Brunet 
2209fba738aSJerome Brunet static inline unsigned int clk_get_scaled_duty_cycle(struct clk *clk,
2219fba738aSJerome Brunet 						     unsigned int scale)
2229fba738aSJerome Brunet {
2239fba738aSJerome Brunet 	return 0;
2249fba738aSJerome Brunet }
2259fba738aSJerome Brunet 
2263d3801efSMichael Turquette static inline bool clk_is_match(const struct clk *p, const struct clk *q)
2273d3801efSMichael Turquette {
2283d3801efSMichael Turquette 	return p == q;
2293d3801efSMichael Turquette }
2303d3801efSMichael Turquette 
2317e87aed9SMark Brown #endif
232b2476490SMike Turquette 
233f8ce2547SRussell King /**
23493abe8e4SViresh Kumar  * clk_prepare - prepare a clock source
23593abe8e4SViresh Kumar  * @clk: clock source
23693abe8e4SViresh Kumar  *
23793abe8e4SViresh Kumar  * This prepares the clock source for use.
23893abe8e4SViresh Kumar  *
23993abe8e4SViresh Kumar  * Must not be called from within atomic context.
24093abe8e4SViresh Kumar  */
24193abe8e4SViresh Kumar #ifdef CONFIG_HAVE_CLK_PREPARE
24293abe8e4SViresh Kumar int clk_prepare(struct clk *clk);
243266e4e9dSDong Aisheng int __must_check clk_bulk_prepare(int num_clks,
244266e4e9dSDong Aisheng 				  const struct clk_bulk_data *clks);
24593abe8e4SViresh Kumar #else
24693abe8e4SViresh Kumar static inline int clk_prepare(struct clk *clk)
24793abe8e4SViresh Kumar {
24893abe8e4SViresh Kumar 	might_sleep();
24993abe8e4SViresh Kumar 	return 0;
25093abe8e4SViresh Kumar }
251266e4e9dSDong Aisheng 
252570aaec7SAndrey Smirnov static inline int __must_check
253570aaec7SAndrey Smirnov clk_bulk_prepare(int num_clks, const struct clk_bulk_data *clks)
254266e4e9dSDong Aisheng {
255266e4e9dSDong Aisheng 	might_sleep();
256266e4e9dSDong Aisheng 	return 0;
257266e4e9dSDong Aisheng }
25893abe8e4SViresh Kumar #endif
25993abe8e4SViresh Kumar 
26093abe8e4SViresh Kumar /**
26193abe8e4SViresh Kumar  * clk_unprepare - undo preparation of a clock source
26293abe8e4SViresh Kumar  * @clk: clock source
26393abe8e4SViresh Kumar  *
26493abe8e4SViresh Kumar  * This undoes a previously prepared clock.  The caller must balance
26593abe8e4SViresh Kumar  * the number of prepare and unprepare calls.
26693abe8e4SViresh Kumar  *
26793abe8e4SViresh Kumar  * Must not be called from within atomic context.
26893abe8e4SViresh Kumar  */
26993abe8e4SViresh Kumar #ifdef CONFIG_HAVE_CLK_PREPARE
27093abe8e4SViresh Kumar void clk_unprepare(struct clk *clk);
271266e4e9dSDong Aisheng void clk_bulk_unprepare(int num_clks, const struct clk_bulk_data *clks);
27293abe8e4SViresh Kumar #else
27393abe8e4SViresh Kumar static inline void clk_unprepare(struct clk *clk)
27493abe8e4SViresh Kumar {
27593abe8e4SViresh Kumar 	might_sleep();
27693abe8e4SViresh Kumar }
277570aaec7SAndrey Smirnov static inline void clk_bulk_unprepare(int num_clks,
278570aaec7SAndrey Smirnov 				      const struct clk_bulk_data *clks)
279266e4e9dSDong Aisheng {
280266e4e9dSDong Aisheng 	might_sleep();
281266e4e9dSDong Aisheng }
28293abe8e4SViresh Kumar #endif
28393abe8e4SViresh Kumar 
28493abe8e4SViresh Kumar #ifdef CONFIG_HAVE_CLK
28593abe8e4SViresh Kumar /**
286f8ce2547SRussell King  * clk_get - lookup and obtain a reference to a clock producer.
287f8ce2547SRussell King  * @dev: device for clock "consumer"
288a58b3a4aSJan-Simon Möller  * @id: clock consumer ID
289f8ce2547SRussell King  *
290f8ce2547SRussell King  * Returns a struct clk corresponding to the clock producer, or
291f8ce2547SRussell King  * valid IS_ERR() condition containing errno.  The implementation
292f8ce2547SRussell King  * uses @dev and @id to determine the clock consumer, and thereby
293f8ce2547SRussell King  * the clock producer.  (IOW, @id may be identical strings, but
294f8ce2547SRussell King  * clk_get may return different clock producers depending on @dev.)
295f8ce2547SRussell King  *
296f8ce2547SRussell King  * Drivers must assume that the clock source is not enabled.
297f7ad160bSAlex Raimondi  *
298f7ad160bSAlex Raimondi  * clk_get should not be called from within interrupt context.
299f8ce2547SRussell King  */
300f8ce2547SRussell King struct clk *clk_get(struct device *dev, const char *id);
301f8ce2547SRussell King 
302f8ce2547SRussell King /**
303266e4e9dSDong Aisheng  * clk_bulk_get - lookup and obtain a number of references to clock producer.
304266e4e9dSDong Aisheng  * @dev: device for clock "consumer"
305266e4e9dSDong Aisheng  * @num_clks: the number of clk_bulk_data
306266e4e9dSDong Aisheng  * @clks: the clk_bulk_data table of consumer
307266e4e9dSDong Aisheng  *
308266e4e9dSDong Aisheng  * This helper function allows drivers to get several clk consumers in one
309266e4e9dSDong Aisheng  * operation. If any of the clk cannot be acquired then any clks
310266e4e9dSDong Aisheng  * that were obtained will be freed before returning to the caller.
311266e4e9dSDong Aisheng  *
312266e4e9dSDong Aisheng  * Returns 0 if all clocks specified in clk_bulk_data table are obtained
313266e4e9dSDong Aisheng  * successfully, or valid IS_ERR() condition containing errno.
314266e4e9dSDong Aisheng  * The implementation uses @dev and @clk_bulk_data.id to determine the
315266e4e9dSDong Aisheng  * clock consumer, and thereby the clock producer.
316266e4e9dSDong Aisheng  * The clock returned is stored in each @clk_bulk_data.clk field.
317266e4e9dSDong Aisheng  *
318266e4e9dSDong Aisheng  * Drivers must assume that the clock source is not enabled.
319266e4e9dSDong Aisheng  *
320266e4e9dSDong Aisheng  * clk_bulk_get should not be called from within interrupt context.
321266e4e9dSDong Aisheng  */
322266e4e9dSDong Aisheng int __must_check clk_bulk_get(struct device *dev, int num_clks,
323266e4e9dSDong Aisheng 			      struct clk_bulk_data *clks);
324616e45dfSDong Aisheng /**
325616e45dfSDong Aisheng  * clk_bulk_get_all - lookup and obtain all available references to clock
326616e45dfSDong Aisheng  *		      producer.
327616e45dfSDong Aisheng  * @dev: device for clock "consumer"
328616e45dfSDong Aisheng  * @clks: pointer to the clk_bulk_data table of consumer
329616e45dfSDong Aisheng  *
330616e45dfSDong Aisheng  * This helper function allows drivers to get all clk consumers in one
331616e45dfSDong Aisheng  * operation. If any of the clk cannot be acquired then any clks
332616e45dfSDong Aisheng  * that were obtained will be freed before returning to the caller.
333616e45dfSDong Aisheng  *
334616e45dfSDong Aisheng  * Returns a positive value for the number of clocks obtained while the
335616e45dfSDong Aisheng  * clock references are stored in the clk_bulk_data table in @clks field.
336616e45dfSDong Aisheng  * Returns 0 if there're none and a negative value if something failed.
337616e45dfSDong Aisheng  *
338616e45dfSDong Aisheng  * Drivers must assume that the clock source is not enabled.
339616e45dfSDong Aisheng  *
340616e45dfSDong Aisheng  * clk_bulk_get should not be called from within interrupt context.
341616e45dfSDong Aisheng  */
342616e45dfSDong Aisheng int __must_check clk_bulk_get_all(struct device *dev,
343616e45dfSDong Aisheng 				  struct clk_bulk_data **clks);
3442f25528eSSylwester Nawrocki 
3452f25528eSSylwester Nawrocki /**
3462f25528eSSylwester Nawrocki  * clk_bulk_get_optional - lookup and obtain a number of references to clock producer
3472f25528eSSylwester Nawrocki  * @dev: device for clock "consumer"
3482f25528eSSylwester Nawrocki  * @num_clks: the number of clk_bulk_data
3492f25528eSSylwester Nawrocki  * @clks: the clk_bulk_data table of consumer
3502f25528eSSylwester Nawrocki  *
3512f25528eSSylwester Nawrocki  * Behaves the same as clk_bulk_get() except where there is no clock producer.
3522f25528eSSylwester Nawrocki  * In this case, instead of returning -ENOENT, the function returns 0 and
3532f25528eSSylwester Nawrocki  * NULL for a clk for which a clock producer could not be determined.
3542f25528eSSylwester Nawrocki  */
3552f25528eSSylwester Nawrocki int __must_check clk_bulk_get_optional(struct device *dev, int num_clks,
3562f25528eSSylwester Nawrocki 				       struct clk_bulk_data *clks);
357266e4e9dSDong Aisheng /**
358618aee02SDong Aisheng  * devm_clk_bulk_get - managed get multiple clk consumers
359618aee02SDong Aisheng  * @dev: device for clock "consumer"
360618aee02SDong Aisheng  * @num_clks: the number of clk_bulk_data
361618aee02SDong Aisheng  * @clks: the clk_bulk_data table of consumer
362618aee02SDong Aisheng  *
363618aee02SDong Aisheng  * Return 0 on success, an errno on failure.
364618aee02SDong Aisheng  *
365618aee02SDong Aisheng  * This helper function allows drivers to get several clk
366618aee02SDong Aisheng  * consumers in one operation with management, the clks will
367618aee02SDong Aisheng  * automatically be freed when the device is unbound.
368618aee02SDong Aisheng  */
369618aee02SDong Aisheng int __must_check devm_clk_bulk_get(struct device *dev, int num_clks,
370618aee02SDong Aisheng 				   struct clk_bulk_data *clks);
371f08c2e28SDong Aisheng /**
3729bd5ef0bSSylwester Nawrocki  * devm_clk_bulk_get_optional - managed get multiple optional consumer clocks
3739bd5ef0bSSylwester Nawrocki  * @dev: device for clock "consumer"
3746ee82ef0SSylwester Nawrocki  * @num_clks: the number of clk_bulk_data
3759bd5ef0bSSylwester Nawrocki  * @clks: pointer to the clk_bulk_data table of consumer
3769bd5ef0bSSylwester Nawrocki  *
3779bd5ef0bSSylwester Nawrocki  * Behaves the same as devm_clk_bulk_get() except where there is no clock
3789bd5ef0bSSylwester Nawrocki  * producer.  In this case, instead of returning -ENOENT, the function returns
3799bd5ef0bSSylwester Nawrocki  * NULL for given clk. It is assumed all clocks in clk_bulk_data are optional.
3809bd5ef0bSSylwester Nawrocki  *
3819bd5ef0bSSylwester Nawrocki  * Returns 0 if all clocks specified in clk_bulk_data table are obtained
3829bd5ef0bSSylwester Nawrocki  * successfully or for any clk there was no clk provider available, otherwise
3839bd5ef0bSSylwester Nawrocki  * returns valid IS_ERR() condition containing errno.
3849bd5ef0bSSylwester Nawrocki  * The implementation uses @dev and @clk_bulk_data.id to determine the
3859bd5ef0bSSylwester Nawrocki  * clock consumer, and thereby the clock producer.
3869bd5ef0bSSylwester Nawrocki  * The clock returned is stored in each @clk_bulk_data.clk field.
3879bd5ef0bSSylwester Nawrocki  *
3889bd5ef0bSSylwester Nawrocki  * Drivers must assume that the clock source is not enabled.
3899bd5ef0bSSylwester Nawrocki  *
3909bd5ef0bSSylwester Nawrocki  * clk_bulk_get should not be called from within interrupt context.
3919bd5ef0bSSylwester Nawrocki  */
3929bd5ef0bSSylwester Nawrocki int __must_check devm_clk_bulk_get_optional(struct device *dev, int num_clks,
3939bd5ef0bSSylwester Nawrocki 					    struct clk_bulk_data *clks);
3949bd5ef0bSSylwester Nawrocki /**
395f08c2e28SDong Aisheng  * devm_clk_bulk_get_all - managed get multiple clk consumers
396f08c2e28SDong Aisheng  * @dev: device for clock "consumer"
397f08c2e28SDong Aisheng  * @clks: pointer to the clk_bulk_data table of consumer
398f08c2e28SDong Aisheng  *
399f08c2e28SDong Aisheng  * Returns a positive value for the number of clocks obtained while the
400f08c2e28SDong Aisheng  * clock references are stored in the clk_bulk_data table in @clks field.
401f08c2e28SDong Aisheng  * Returns 0 if there're none and a negative value if something failed.
402f08c2e28SDong Aisheng  *
403f08c2e28SDong Aisheng  * This helper function allows drivers to get several clk
404f08c2e28SDong Aisheng  * consumers in one operation with management, the clks will
405f08c2e28SDong Aisheng  * automatically be freed when the device is unbound.
406f08c2e28SDong Aisheng  */
407f08c2e28SDong Aisheng 
408f08c2e28SDong Aisheng int __must_check devm_clk_bulk_get_all(struct device *dev,
409f08c2e28SDong Aisheng 				       struct clk_bulk_data **clks);
410618aee02SDong Aisheng 
411618aee02SDong Aisheng /**
412a8a97db9SMark Brown  * devm_clk_get - lookup and obtain a managed reference to a clock producer.
413a8a97db9SMark Brown  * @dev: device for clock "consumer"
414a58b3a4aSJan-Simon Möller  * @id: clock consumer ID
415a8a97db9SMark Brown  *
416a8a97db9SMark Brown  * Returns a struct clk corresponding to the clock producer, or
417a8a97db9SMark Brown  * valid IS_ERR() condition containing errno.  The implementation
418a8a97db9SMark Brown  * uses @dev and @id to determine the clock consumer, and thereby
419a8a97db9SMark Brown  * the clock producer.  (IOW, @id may be identical strings, but
420a8a97db9SMark Brown  * clk_get may return different clock producers depending on @dev.)
421a8a97db9SMark Brown  *
422a8a97db9SMark Brown  * Drivers must assume that the clock source is not enabled.
423a8a97db9SMark Brown  *
424a8a97db9SMark Brown  * devm_clk_get should not be called from within interrupt context.
425a8a97db9SMark Brown  *
426a8a97db9SMark Brown  * The clock will automatically be freed when the device is unbound
427a8a97db9SMark Brown  * from the bus.
428a8a97db9SMark Brown  */
429a8a97db9SMark Brown struct clk *devm_clk_get(struct device *dev, const char *id);
430a8a97db9SMark Brown 
431a8a97db9SMark Brown /**
43260b8f0ddSPhil Edworthy  * devm_clk_get_optional - lookup and obtain a managed reference to an optional
43360b8f0ddSPhil Edworthy  *			   clock producer.
43460b8f0ddSPhil Edworthy  * @dev: device for clock "consumer"
43560b8f0ddSPhil Edworthy  * @id: clock consumer ID
43660b8f0ddSPhil Edworthy  *
43760b8f0ddSPhil Edworthy  * Behaves the same as devm_clk_get() except where there is no clock producer.
43860b8f0ddSPhil Edworthy  * In this case, instead of returning -ENOENT, the function returns NULL.
43960b8f0ddSPhil Edworthy  */
44060b8f0ddSPhil Edworthy struct clk *devm_clk_get_optional(struct device *dev, const char *id);
44160b8f0ddSPhil Edworthy 
44260b8f0ddSPhil Edworthy /**
44371a2f115SKuninori Morimoto  * devm_get_clk_from_child - lookup and obtain a managed reference to a
44471a2f115SKuninori Morimoto  *			     clock producer from child node.
44571a2f115SKuninori Morimoto  * @dev: device for clock "consumer"
44671a2f115SKuninori Morimoto  * @np: pointer to clock consumer node
44771a2f115SKuninori Morimoto  * @con_id: clock consumer ID
44871a2f115SKuninori Morimoto  *
44971a2f115SKuninori Morimoto  * This function parses the clocks, and uses them to look up the
45071a2f115SKuninori Morimoto  * struct clk from the registered list of clock providers by using
45171a2f115SKuninori Morimoto  * @np and @con_id
45271a2f115SKuninori Morimoto  *
45371a2f115SKuninori Morimoto  * The clock will automatically be freed when the device is unbound
45471a2f115SKuninori Morimoto  * from the bus.
45571a2f115SKuninori Morimoto  */
45671a2f115SKuninori Morimoto struct clk *devm_get_clk_from_child(struct device *dev,
45771a2f115SKuninori Morimoto 				    struct device_node *np, const char *con_id);
45855e9b8b7SJerome Brunet /**
45955e9b8b7SJerome Brunet  * clk_rate_exclusive_get - get exclusivity over the rate control of a
46055e9b8b7SJerome Brunet  *                          producer
46155e9b8b7SJerome Brunet  * @clk: clock source
46255e9b8b7SJerome Brunet  *
46355e9b8b7SJerome Brunet  * This function allows drivers to get exclusive control over the rate of a
46455e9b8b7SJerome Brunet  * provider. It prevents any other consumer to execute, even indirectly,
46555e9b8b7SJerome Brunet  * opereation which could alter the rate of the provider or cause glitches
46655e9b8b7SJerome Brunet  *
46755e9b8b7SJerome Brunet  * If exlusivity is claimed more than once on clock, even by the same driver,
46855e9b8b7SJerome Brunet  * the rate effectively gets locked as exclusivity can't be preempted.
46955e9b8b7SJerome Brunet  *
47055e9b8b7SJerome Brunet  * Must not be called from within atomic context.
47155e9b8b7SJerome Brunet  *
47255e9b8b7SJerome Brunet  * Returns success (0) or negative errno.
47355e9b8b7SJerome Brunet  */
47455e9b8b7SJerome Brunet int clk_rate_exclusive_get(struct clk *clk);
47555e9b8b7SJerome Brunet 
47655e9b8b7SJerome Brunet /**
47755e9b8b7SJerome Brunet  * clk_rate_exclusive_put - release exclusivity over the rate control of a
47855e9b8b7SJerome Brunet  *                          producer
47955e9b8b7SJerome Brunet  * @clk: clock source
48055e9b8b7SJerome Brunet  *
48155e9b8b7SJerome Brunet  * This function allows drivers to release the exclusivity it previously got
48255e9b8b7SJerome Brunet  * from clk_rate_exclusive_get()
48355e9b8b7SJerome Brunet  *
48455e9b8b7SJerome Brunet  * The caller must balance the number of clk_rate_exclusive_get() and
48555e9b8b7SJerome Brunet  * clk_rate_exclusive_put() calls.
48655e9b8b7SJerome Brunet  *
48755e9b8b7SJerome Brunet  * Must not be called from within atomic context.
48855e9b8b7SJerome Brunet  */
48955e9b8b7SJerome Brunet void clk_rate_exclusive_put(struct clk *clk);
49071a2f115SKuninori Morimoto 
49171a2f115SKuninori Morimoto /**
492f8ce2547SRussell King  * clk_enable - inform the system when the clock source should be running.
493f8ce2547SRussell King  * @clk: clock source
494f8ce2547SRussell King  *
495f8ce2547SRussell King  * If the clock can not be enabled/disabled, this should return success.
496f8ce2547SRussell King  *
49740d3e0f4SRussell King  * May be called from atomic contexts.
49840d3e0f4SRussell King  *
499f8ce2547SRussell King  * Returns success (0) or negative errno.
500f8ce2547SRussell King  */
501f8ce2547SRussell King int clk_enable(struct clk *clk);
502f8ce2547SRussell King 
503f8ce2547SRussell King /**
504266e4e9dSDong Aisheng  * clk_bulk_enable - inform the system when the set of clks should be running.
505266e4e9dSDong Aisheng  * @num_clks: the number of clk_bulk_data
506266e4e9dSDong Aisheng  * @clks: the clk_bulk_data table of consumer
507266e4e9dSDong Aisheng  *
508266e4e9dSDong Aisheng  * May be called from atomic contexts.
509266e4e9dSDong Aisheng  *
510266e4e9dSDong Aisheng  * Returns success (0) or negative errno.
511266e4e9dSDong Aisheng  */
512266e4e9dSDong Aisheng int __must_check clk_bulk_enable(int num_clks,
513266e4e9dSDong Aisheng 				 const struct clk_bulk_data *clks);
514266e4e9dSDong Aisheng 
515266e4e9dSDong Aisheng /**
516f8ce2547SRussell King  * clk_disable - inform the system when the clock source is no longer required.
517f8ce2547SRussell King  * @clk: clock source
518f8ce2547SRussell King  *
519f8ce2547SRussell King  * Inform the system that a clock source is no longer required by
520f8ce2547SRussell King  * a driver and may be shut down.
521f8ce2547SRussell King  *
52240d3e0f4SRussell King  * May be called from atomic contexts.
52340d3e0f4SRussell King  *
524f8ce2547SRussell King  * Implementation detail: if the clock source is shared between
525f8ce2547SRussell King  * multiple drivers, clk_enable() calls must be balanced by the
526f8ce2547SRussell King  * same number of clk_disable() calls for the clock source to be
527f8ce2547SRussell King  * disabled.
528f8ce2547SRussell King  */
529f8ce2547SRussell King void clk_disable(struct clk *clk);
530f8ce2547SRussell King 
531f8ce2547SRussell King /**
532266e4e9dSDong Aisheng  * clk_bulk_disable - inform the system when the set of clks is no
533266e4e9dSDong Aisheng  *		      longer required.
534266e4e9dSDong Aisheng  * @num_clks: the number of clk_bulk_data
535266e4e9dSDong Aisheng  * @clks: the clk_bulk_data table of consumer
536266e4e9dSDong Aisheng  *
537266e4e9dSDong Aisheng  * Inform the system that a set of clks is no longer required by
538266e4e9dSDong Aisheng  * a driver and may be shut down.
539266e4e9dSDong Aisheng  *
540266e4e9dSDong Aisheng  * May be called from atomic contexts.
541266e4e9dSDong Aisheng  *
542266e4e9dSDong Aisheng  * Implementation detail: if the set of clks is shared between
543266e4e9dSDong Aisheng  * multiple drivers, clk_bulk_enable() calls must be balanced by the
544266e4e9dSDong Aisheng  * same number of clk_bulk_disable() calls for the clock source to be
545266e4e9dSDong Aisheng  * disabled.
546266e4e9dSDong Aisheng  */
547266e4e9dSDong Aisheng void clk_bulk_disable(int num_clks, const struct clk_bulk_data *clks);
548266e4e9dSDong Aisheng 
549266e4e9dSDong Aisheng /**
550f8ce2547SRussell King  * clk_get_rate - obtain the current clock rate (in Hz) for a clock source.
551f8ce2547SRussell King  *		  This is only valid once the clock source has been enabled.
552f8ce2547SRussell King  * @clk: clock source
553f8ce2547SRussell King  */
554f8ce2547SRussell King unsigned long clk_get_rate(struct clk *clk);
555f8ce2547SRussell King 
556f8ce2547SRussell King /**
557f8ce2547SRussell King  * clk_put	- "free" the clock source
558f8ce2547SRussell King  * @clk: clock source
559f8ce2547SRussell King  *
560f8ce2547SRussell King  * Note: drivers must ensure that all clk_enable calls made on this
561f8ce2547SRussell King  * clock source are balanced by clk_disable calls prior to calling
562f8ce2547SRussell King  * this function.
563f7ad160bSAlex Raimondi  *
564f7ad160bSAlex Raimondi  * clk_put should not be called from within interrupt context.
565f8ce2547SRussell King  */
566f8ce2547SRussell King void clk_put(struct clk *clk);
567f8ce2547SRussell King 
568a8a97db9SMark Brown /**
569266e4e9dSDong Aisheng  * clk_bulk_put	- "free" the clock source
570266e4e9dSDong Aisheng  * @num_clks: the number of clk_bulk_data
571266e4e9dSDong Aisheng  * @clks: the clk_bulk_data table of consumer
572266e4e9dSDong Aisheng  *
573266e4e9dSDong Aisheng  * Note: drivers must ensure that all clk_bulk_enable calls made on this
574266e4e9dSDong Aisheng  * clock source are balanced by clk_bulk_disable calls prior to calling
575266e4e9dSDong Aisheng  * this function.
576266e4e9dSDong Aisheng  *
577266e4e9dSDong Aisheng  * clk_bulk_put should not be called from within interrupt context.
578266e4e9dSDong Aisheng  */
579266e4e9dSDong Aisheng void clk_bulk_put(int num_clks, struct clk_bulk_data *clks);
580266e4e9dSDong Aisheng 
581266e4e9dSDong Aisheng /**
582616e45dfSDong Aisheng  * clk_bulk_put_all - "free" all the clock source
583616e45dfSDong Aisheng  * @num_clks: the number of clk_bulk_data
584616e45dfSDong Aisheng  * @clks: the clk_bulk_data table of consumer
585616e45dfSDong Aisheng  *
586616e45dfSDong Aisheng  * Note: drivers must ensure that all clk_bulk_enable calls made on this
587616e45dfSDong Aisheng  * clock source are balanced by clk_bulk_disable calls prior to calling
588616e45dfSDong Aisheng  * this function.
589616e45dfSDong Aisheng  *
590616e45dfSDong Aisheng  * clk_bulk_put_all should not be called from within interrupt context.
591616e45dfSDong Aisheng  */
592616e45dfSDong Aisheng void clk_bulk_put_all(int num_clks, struct clk_bulk_data *clks);
593616e45dfSDong Aisheng 
594616e45dfSDong Aisheng /**
595a8a97db9SMark Brown  * devm_clk_put	- "free" a managed clock source
596da3dae54SMasanari Iida  * @dev: device used to acquire the clock
597a8a97db9SMark Brown  * @clk: clock source acquired with devm_clk_get()
598a8a97db9SMark Brown  *
599a8a97db9SMark Brown  * Note: drivers must ensure that all clk_enable calls made on this
600a8a97db9SMark Brown  * clock source are balanced by clk_disable calls prior to calling
601a8a97db9SMark Brown  * this function.
602a8a97db9SMark Brown  *
603a8a97db9SMark Brown  * clk_put should not be called from within interrupt context.
604a8a97db9SMark Brown  */
605a8a97db9SMark Brown void devm_clk_put(struct device *dev, struct clk *clk);
606f8ce2547SRussell King 
607f8ce2547SRussell King /*
608f8ce2547SRussell King  * The remaining APIs are optional for machine class support.
609f8ce2547SRussell King  */
610f8ce2547SRussell King 
611f8ce2547SRussell King 
612f8ce2547SRussell King /**
613f8ce2547SRussell King  * clk_round_rate - adjust a rate to the exact rate a clock can provide
614f8ce2547SRussell King  * @clk: clock source
615f8ce2547SRussell King  * @rate: desired clock rate in Hz
616f8ce2547SRussell King  *
617d2d14a77SRussell King  * This answers the question "if I were to pass @rate to clk_set_rate(),
618d2d14a77SRussell King  * what clock rate would I end up with?" without changing the hardware
619d2d14a77SRussell King  * in any way.  In other words:
620d2d14a77SRussell King  *
621d2d14a77SRussell King  *   rate = clk_round_rate(clk, r);
622d2d14a77SRussell King  *
623d2d14a77SRussell King  * and:
624d2d14a77SRussell King  *
625d2d14a77SRussell King  *   clk_set_rate(clk, r);
626d2d14a77SRussell King  *   rate = clk_get_rate(clk);
627d2d14a77SRussell King  *
628d2d14a77SRussell King  * are equivalent except the former does not modify the clock hardware
629d2d14a77SRussell King  * in any way.
630d2d14a77SRussell King  *
631f8ce2547SRussell King  * Returns rounded clock rate in Hz, or negative errno.
632f8ce2547SRussell King  */
633f8ce2547SRussell King long clk_round_rate(struct clk *clk, unsigned long rate);
634f8ce2547SRussell King 
635f8ce2547SRussell King /**
636f8ce2547SRussell King  * clk_set_rate - set the clock rate for a clock source
637f8ce2547SRussell King  * @clk: clock source
638f8ce2547SRussell King  * @rate: desired clock rate in Hz
639f8ce2547SRussell King  *
64064c76b31SMartin Blumenstingl  * Updating the rate starts at the top-most affected clock and then
64164c76b31SMartin Blumenstingl  * walks the tree down to the bottom-most clock that needs updating.
64264c76b31SMartin Blumenstingl  *
643f8ce2547SRussell King  * Returns success (0) or negative errno.
644f8ce2547SRussell King  */
645f8ce2547SRussell King int clk_set_rate(struct clk *clk, unsigned long rate);
646f8ce2547SRussell King 
647f8ce2547SRussell King /**
64855e9b8b7SJerome Brunet  * clk_set_rate_exclusive- set the clock rate and claim exclusivity over
64955e9b8b7SJerome Brunet  *                         clock source
65055e9b8b7SJerome Brunet  * @clk: clock source
65155e9b8b7SJerome Brunet  * @rate: desired clock rate in Hz
65255e9b8b7SJerome Brunet  *
65355e9b8b7SJerome Brunet  * This helper function allows drivers to atomically set the rate of a producer
65455e9b8b7SJerome Brunet  * and claim exclusivity over the rate control of the producer.
65555e9b8b7SJerome Brunet  *
65655e9b8b7SJerome Brunet  * It is essentially a combination of clk_set_rate() and
65755e9b8b7SJerome Brunet  * clk_rate_exclusite_get(). Caller must balance this call with a call to
65855e9b8b7SJerome Brunet  * clk_rate_exclusive_put()
65955e9b8b7SJerome Brunet  *
66055e9b8b7SJerome Brunet  * Returns success (0) or negative errno.
66155e9b8b7SJerome Brunet  */
66255e9b8b7SJerome Brunet int clk_set_rate_exclusive(struct clk *clk, unsigned long rate);
66355e9b8b7SJerome Brunet 
66455e9b8b7SJerome Brunet /**
6654e88f3deSThierry Reding  * clk_has_parent - check if a clock is a possible parent for another
6664e88f3deSThierry Reding  * @clk: clock source
6674e88f3deSThierry Reding  * @parent: parent clock source
6684e88f3deSThierry Reding  *
6694e88f3deSThierry Reding  * This function can be used in drivers that need to check that a clock can be
6704e88f3deSThierry Reding  * the parent of another without actually changing the parent.
6714e88f3deSThierry Reding  *
6724e88f3deSThierry Reding  * Returns true if @parent is a possible parent for @clk, false otherwise.
6734e88f3deSThierry Reding  */
6744e88f3deSThierry Reding bool clk_has_parent(struct clk *clk, struct clk *parent);
6754e88f3deSThierry Reding 
6764e88f3deSThierry Reding /**
6771c8e6004STomeu Vizoso  * clk_set_rate_range - set a rate range for a clock source
6781c8e6004STomeu Vizoso  * @clk: clock source
6791c8e6004STomeu Vizoso  * @min: desired minimum clock rate in Hz, inclusive
6801c8e6004STomeu Vizoso  * @max: desired maximum clock rate in Hz, inclusive
6811c8e6004STomeu Vizoso  *
6821c8e6004STomeu Vizoso  * Returns success (0) or negative errno.
6831c8e6004STomeu Vizoso  */
6841c8e6004STomeu Vizoso int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max);
6851c8e6004STomeu Vizoso 
6861c8e6004STomeu Vizoso /**
6871c8e6004STomeu Vizoso  * clk_set_min_rate - set a minimum clock rate for a clock source
6881c8e6004STomeu Vizoso  * @clk: clock source
6891c8e6004STomeu Vizoso  * @rate: desired minimum clock rate in Hz, inclusive
6901c8e6004STomeu Vizoso  *
6911c8e6004STomeu Vizoso  * Returns success (0) or negative errno.
6921c8e6004STomeu Vizoso  */
6931c8e6004STomeu Vizoso int clk_set_min_rate(struct clk *clk, unsigned long rate);
6941c8e6004STomeu Vizoso 
6951c8e6004STomeu Vizoso /**
6961c8e6004STomeu Vizoso  * clk_set_max_rate - set a maximum clock rate for a clock source
6971c8e6004STomeu Vizoso  * @clk: clock source
6981c8e6004STomeu Vizoso  * @rate: desired maximum clock rate in Hz, inclusive
6991c8e6004STomeu Vizoso  *
7001c8e6004STomeu Vizoso  * Returns success (0) or negative errno.
7011c8e6004STomeu Vizoso  */
7021c8e6004STomeu Vizoso int clk_set_max_rate(struct clk *clk, unsigned long rate);
7031c8e6004STomeu Vizoso 
7041c8e6004STomeu Vizoso /**
705f8ce2547SRussell King  * clk_set_parent - set the parent clock source for this clock
706f8ce2547SRussell King  * @clk: clock source
707f8ce2547SRussell King  * @parent: parent clock source
708f8ce2547SRussell King  *
709f8ce2547SRussell King  * Returns success (0) or negative errno.
710f8ce2547SRussell King  */
711f8ce2547SRussell King int clk_set_parent(struct clk *clk, struct clk *parent);
712f8ce2547SRussell King 
713f8ce2547SRussell King /**
714f8ce2547SRussell King  * clk_get_parent - get the parent clock source for this clock
715f8ce2547SRussell King  * @clk: clock source
716f8ce2547SRussell King  *
717f8ce2547SRussell King  * Returns struct clk corresponding to parent clock source, or
718f8ce2547SRussell King  * valid IS_ERR() condition containing errno.
719f8ce2547SRussell King  */
720f8ce2547SRussell King struct clk *clk_get_parent(struct clk *clk);
721f8ce2547SRussell King 
72205fd8e73SSascha Hauer /**
72305fd8e73SSascha Hauer  * clk_get_sys - get a clock based upon the device name
72405fd8e73SSascha Hauer  * @dev_id: device name
72505fd8e73SSascha Hauer  * @con_id: connection ID
72605fd8e73SSascha Hauer  *
72705fd8e73SSascha Hauer  * Returns a struct clk corresponding to the clock producer, or
72805fd8e73SSascha Hauer  * valid IS_ERR() condition containing errno.  The implementation
72905fd8e73SSascha Hauer  * uses @dev_id and @con_id to determine the clock consumer, and
73005fd8e73SSascha Hauer  * thereby the clock producer. In contrast to clk_get() this function
73105fd8e73SSascha Hauer  * takes the device name instead of the device itself for identification.
73205fd8e73SSascha Hauer  *
73305fd8e73SSascha Hauer  * Drivers must assume that the clock source is not enabled.
73405fd8e73SSascha Hauer  *
73505fd8e73SSascha Hauer  * clk_get_sys should not be called from within interrupt context.
73605fd8e73SSascha Hauer  */
73705fd8e73SSascha Hauer struct clk *clk_get_sys(const char *dev_id, const char *con_id);
73805fd8e73SSascha Hauer 
7398b95d1ceSRuss Dill /**
7408b95d1ceSRuss Dill  * clk_save_context - save clock context for poweroff
7418b95d1ceSRuss Dill  *
7428b95d1ceSRuss Dill  * Saves the context of the clock register for powerstates in which the
7438b95d1ceSRuss Dill  * contents of the registers will be lost. Occurs deep within the suspend
7448b95d1ceSRuss Dill  * code so locking is not necessary.
7458b95d1ceSRuss Dill  */
7468b95d1ceSRuss Dill int clk_save_context(void);
7478b95d1ceSRuss Dill 
7488b95d1ceSRuss Dill /**
7498b95d1ceSRuss Dill  * clk_restore_context - restore clock context after poweroff
7508b95d1ceSRuss Dill  *
7518b95d1ceSRuss Dill  * This occurs with all clocks enabled. Occurs deep within the resume code
7528b95d1ceSRuss Dill  * so locking is not necessary.
7538b95d1ceSRuss Dill  */
7548b95d1ceSRuss Dill void clk_restore_context(void);
7558b95d1ceSRuss Dill 
75693abe8e4SViresh Kumar #else /* !CONFIG_HAVE_CLK */
75793abe8e4SViresh Kumar 
75893abe8e4SViresh Kumar static inline struct clk *clk_get(struct device *dev, const char *id)
75993abe8e4SViresh Kumar {
76093abe8e4SViresh Kumar 	return NULL;
76193abe8e4SViresh Kumar }
76293abe8e4SViresh Kumar 
7636e0d4ff4SDong Aisheng static inline int __must_check clk_bulk_get(struct device *dev, int num_clks,
764266e4e9dSDong Aisheng 					    struct clk_bulk_data *clks)
765266e4e9dSDong Aisheng {
766266e4e9dSDong Aisheng 	return 0;
767266e4e9dSDong Aisheng }
768266e4e9dSDong Aisheng 
7692f25528eSSylwester Nawrocki static inline int __must_check clk_bulk_get_optional(struct device *dev,
7702f25528eSSylwester Nawrocki 				int num_clks, struct clk_bulk_data *clks)
7712f25528eSSylwester Nawrocki {
7722f25528eSSylwester Nawrocki 	return 0;
7732f25528eSSylwester Nawrocki }
7742f25528eSSylwester Nawrocki 
775616e45dfSDong Aisheng static inline int __must_check clk_bulk_get_all(struct device *dev,
776616e45dfSDong Aisheng 					 struct clk_bulk_data **clks)
777616e45dfSDong Aisheng {
778616e45dfSDong Aisheng 	return 0;
779616e45dfSDong Aisheng }
780616e45dfSDong Aisheng 
78193abe8e4SViresh Kumar static inline struct clk *devm_clk_get(struct device *dev, const char *id)
78293abe8e4SViresh Kumar {
78393abe8e4SViresh Kumar 	return NULL;
78493abe8e4SViresh Kumar }
78593abe8e4SViresh Kumar 
78660b8f0ddSPhil Edworthy static inline struct clk *devm_clk_get_optional(struct device *dev,
78760b8f0ddSPhil Edworthy 						const char *id)
78860b8f0ddSPhil Edworthy {
78960b8f0ddSPhil Edworthy 	return NULL;
79060b8f0ddSPhil Edworthy }
79160b8f0ddSPhil Edworthy 
7926e0d4ff4SDong Aisheng static inline int __must_check devm_clk_bulk_get(struct device *dev, int num_clks,
793618aee02SDong Aisheng 						 struct clk_bulk_data *clks)
794618aee02SDong Aisheng {
795618aee02SDong Aisheng 	return 0;
796618aee02SDong Aisheng }
797618aee02SDong Aisheng 
7989bd5ef0bSSylwester Nawrocki static inline int __must_check devm_clk_bulk_get_optional(struct device *dev,
7999bd5ef0bSSylwester Nawrocki 				int num_clks, struct clk_bulk_data *clks)
8009bd5ef0bSSylwester Nawrocki {
8019bd5ef0bSSylwester Nawrocki 	return 0;
8029bd5ef0bSSylwester Nawrocki }
8039bd5ef0bSSylwester Nawrocki 
804f08c2e28SDong Aisheng static inline int __must_check devm_clk_bulk_get_all(struct device *dev,
805f08c2e28SDong Aisheng 						     struct clk_bulk_data **clks)
806f08c2e28SDong Aisheng {
807f08c2e28SDong Aisheng 
808f08c2e28SDong Aisheng 	return 0;
809f08c2e28SDong Aisheng }
810f08c2e28SDong Aisheng 
81171a2f115SKuninori Morimoto static inline struct clk *devm_get_clk_from_child(struct device *dev,
81271a2f115SKuninori Morimoto 				struct device_node *np, const char *con_id)
81371a2f115SKuninori Morimoto {
81471a2f115SKuninori Morimoto 	return NULL;
81571a2f115SKuninori Morimoto }
81671a2f115SKuninori Morimoto 
81793abe8e4SViresh Kumar static inline void clk_put(struct clk *clk) {}
81893abe8e4SViresh Kumar 
819266e4e9dSDong Aisheng static inline void clk_bulk_put(int num_clks, struct clk_bulk_data *clks) {}
820266e4e9dSDong Aisheng 
821616e45dfSDong Aisheng static inline void clk_bulk_put_all(int num_clks, struct clk_bulk_data *clks) {}
822616e45dfSDong Aisheng 
82393abe8e4SViresh Kumar static inline void devm_clk_put(struct device *dev, struct clk *clk) {}
82493abe8e4SViresh Kumar 
82555e9b8b7SJerome Brunet 
82655e9b8b7SJerome Brunet static inline int clk_rate_exclusive_get(struct clk *clk)
82755e9b8b7SJerome Brunet {
82855e9b8b7SJerome Brunet 	return 0;
82955e9b8b7SJerome Brunet }
83055e9b8b7SJerome Brunet 
83155e9b8b7SJerome Brunet static inline void clk_rate_exclusive_put(struct clk *clk) {}
83255e9b8b7SJerome Brunet 
83393abe8e4SViresh Kumar static inline int clk_enable(struct clk *clk)
83493abe8e4SViresh Kumar {
83593abe8e4SViresh Kumar 	return 0;
83693abe8e4SViresh Kumar }
83793abe8e4SViresh Kumar 
838570aaec7SAndrey Smirnov static inline int __must_check clk_bulk_enable(int num_clks,
839570aaec7SAndrey Smirnov 					       const struct clk_bulk_data *clks)
840266e4e9dSDong Aisheng {
841266e4e9dSDong Aisheng 	return 0;
842266e4e9dSDong Aisheng }
843266e4e9dSDong Aisheng 
84493abe8e4SViresh Kumar static inline void clk_disable(struct clk *clk) {}
84593abe8e4SViresh Kumar 
846266e4e9dSDong Aisheng 
847266e4e9dSDong Aisheng static inline void clk_bulk_disable(int num_clks,
848570aaec7SAndrey Smirnov 				    const struct clk_bulk_data *clks) {}
849266e4e9dSDong Aisheng 
85093abe8e4SViresh Kumar static inline unsigned long clk_get_rate(struct clk *clk)
85193abe8e4SViresh Kumar {
85293abe8e4SViresh Kumar 	return 0;
85393abe8e4SViresh Kumar }
85493abe8e4SViresh Kumar 
85593abe8e4SViresh Kumar static inline int clk_set_rate(struct clk *clk, unsigned long rate)
85693abe8e4SViresh Kumar {
85793abe8e4SViresh Kumar 	return 0;
85893abe8e4SViresh Kumar }
85993abe8e4SViresh Kumar 
86055e9b8b7SJerome Brunet static inline int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
86155e9b8b7SJerome Brunet {
86255e9b8b7SJerome Brunet 	return 0;
86355e9b8b7SJerome Brunet }
86455e9b8b7SJerome Brunet 
86593abe8e4SViresh Kumar static inline long clk_round_rate(struct clk *clk, unsigned long rate)
86693abe8e4SViresh Kumar {
86793abe8e4SViresh Kumar 	return 0;
86893abe8e4SViresh Kumar }
86993abe8e4SViresh Kumar 
8704e88f3deSThierry Reding static inline bool clk_has_parent(struct clk *clk, struct clk *parent)
8714e88f3deSThierry Reding {
8724e88f3deSThierry Reding 	return true;
8734e88f3deSThierry Reding }
8744e88f3deSThierry Reding 
875b88c9f41SDmitry Osipenko static inline int clk_set_rate_range(struct clk *clk, unsigned long min,
876b88c9f41SDmitry Osipenko 				     unsigned long max)
877b88c9f41SDmitry Osipenko {
878b88c9f41SDmitry Osipenko 	return 0;
879b88c9f41SDmitry Osipenko }
880b88c9f41SDmitry Osipenko 
881b88c9f41SDmitry Osipenko static inline int clk_set_min_rate(struct clk *clk, unsigned long rate)
882b88c9f41SDmitry Osipenko {
883b88c9f41SDmitry Osipenko 	return 0;
884b88c9f41SDmitry Osipenko }
885b88c9f41SDmitry Osipenko 
886b88c9f41SDmitry Osipenko static inline int clk_set_max_rate(struct clk *clk, unsigned long rate)
887b88c9f41SDmitry Osipenko {
888b88c9f41SDmitry Osipenko 	return 0;
889b88c9f41SDmitry Osipenko }
890b88c9f41SDmitry Osipenko 
89193abe8e4SViresh Kumar static inline int clk_set_parent(struct clk *clk, struct clk *parent)
89293abe8e4SViresh Kumar {
89393abe8e4SViresh Kumar 	return 0;
89493abe8e4SViresh Kumar }
89593abe8e4SViresh Kumar 
89693abe8e4SViresh Kumar static inline struct clk *clk_get_parent(struct clk *clk)
89793abe8e4SViresh Kumar {
89893abe8e4SViresh Kumar 	return NULL;
89993abe8e4SViresh Kumar }
90093abe8e4SViresh Kumar 
901b81ea968SDaniel Lezcano static inline struct clk *clk_get_sys(const char *dev_id, const char *con_id)
902b81ea968SDaniel Lezcano {
903b81ea968SDaniel Lezcano 	return NULL;
904b81ea968SDaniel Lezcano }
9058b95d1ceSRuss Dill 
9068b95d1ceSRuss Dill static inline int clk_save_context(void)
9078b95d1ceSRuss Dill {
9088b95d1ceSRuss Dill 	return 0;
9098b95d1ceSRuss Dill }
9108b95d1ceSRuss Dill 
9118b95d1ceSRuss Dill static inline void clk_restore_context(void) {}
9128b95d1ceSRuss Dill 
91393abe8e4SViresh Kumar #endif
91493abe8e4SViresh Kumar 
91593abe8e4SViresh Kumar /* clk_prepare_enable helps cases using clk_enable in non-atomic context. */
91693abe8e4SViresh Kumar static inline int clk_prepare_enable(struct clk *clk)
91793abe8e4SViresh Kumar {
91893abe8e4SViresh Kumar 	int ret;
91993abe8e4SViresh Kumar 
92093abe8e4SViresh Kumar 	ret = clk_prepare(clk);
92193abe8e4SViresh Kumar 	if (ret)
92293abe8e4SViresh Kumar 		return ret;
92393abe8e4SViresh Kumar 	ret = clk_enable(clk);
92493abe8e4SViresh Kumar 	if (ret)
92593abe8e4SViresh Kumar 		clk_unprepare(clk);
92693abe8e4SViresh Kumar 
92793abe8e4SViresh Kumar 	return ret;
92893abe8e4SViresh Kumar }
92993abe8e4SViresh Kumar 
93093abe8e4SViresh Kumar /* clk_disable_unprepare helps cases using clk_disable in non-atomic context. */
93193abe8e4SViresh Kumar static inline void clk_disable_unprepare(struct clk *clk)
93293abe8e4SViresh Kumar {
93393abe8e4SViresh Kumar 	clk_disable(clk);
93493abe8e4SViresh Kumar 	clk_unprepare(clk);
93593abe8e4SViresh Kumar }
93693abe8e4SViresh Kumar 
937570aaec7SAndrey Smirnov static inline int __must_check
938570aaec7SAndrey Smirnov clk_bulk_prepare_enable(int num_clks, const struct clk_bulk_data *clks)
9393c48d86cSBjorn Andersson {
9403c48d86cSBjorn Andersson 	int ret;
9413c48d86cSBjorn Andersson 
9423c48d86cSBjorn Andersson 	ret = clk_bulk_prepare(num_clks, clks);
9433c48d86cSBjorn Andersson 	if (ret)
9443c48d86cSBjorn Andersson 		return ret;
9453c48d86cSBjorn Andersson 	ret = clk_bulk_enable(num_clks, clks);
9463c48d86cSBjorn Andersson 	if (ret)
9473c48d86cSBjorn Andersson 		clk_bulk_unprepare(num_clks, clks);
9483c48d86cSBjorn Andersson 
9493c48d86cSBjorn Andersson 	return ret;
9503c48d86cSBjorn Andersson }
9513c48d86cSBjorn Andersson 
9523c48d86cSBjorn Andersson static inline void clk_bulk_disable_unprepare(int num_clks,
953570aaec7SAndrey Smirnov 					      const struct clk_bulk_data *clks)
9543c48d86cSBjorn Andersson {
9553c48d86cSBjorn Andersson 	clk_bulk_disable(num_clks, clks);
9563c48d86cSBjorn Andersson 	clk_bulk_unprepare(num_clks, clks);
9573c48d86cSBjorn Andersson }
9583c48d86cSBjorn Andersson 
95960b8f0ddSPhil Edworthy /**
96060b8f0ddSPhil Edworthy  * clk_get_optional - lookup and obtain a reference to an optional clock
96160b8f0ddSPhil Edworthy  *		      producer.
96260b8f0ddSPhil Edworthy  * @dev: device for clock "consumer"
96360b8f0ddSPhil Edworthy  * @id: clock consumer ID
96460b8f0ddSPhil Edworthy  *
96560b8f0ddSPhil Edworthy  * Behaves the same as clk_get() except where there is no clock producer. In
96660b8f0ddSPhil Edworthy  * this case, instead of returning -ENOENT, the function returns NULL.
96760b8f0ddSPhil Edworthy  */
96860b8f0ddSPhil Edworthy static inline struct clk *clk_get_optional(struct device *dev, const char *id)
96960b8f0ddSPhil Edworthy {
97060b8f0ddSPhil Edworthy 	struct clk *clk = clk_get(dev, id);
97160b8f0ddSPhil Edworthy 
97260b8f0ddSPhil Edworthy 	if (clk == ERR_PTR(-ENOENT))
97360b8f0ddSPhil Edworthy 		return NULL;
97460b8f0ddSPhil Edworthy 
97560b8f0ddSPhil Edworthy 	return clk;
97660b8f0ddSPhil Edworthy }
97760b8f0ddSPhil Edworthy 
978137f8a72SRob Herring #if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK)
979766e6a4eSGrant Likely struct clk *of_clk_get(struct device_node *np, int index);
980766e6a4eSGrant Likely struct clk *of_clk_get_by_name(struct device_node *np, const char *name);
981766e6a4eSGrant Likely struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec);
982766e6a4eSGrant Likely #else
983766e6a4eSGrant Likely static inline struct clk *of_clk_get(struct device_node *np, int index)
984766e6a4eSGrant Likely {
9859f1612d3SShawn Guo 	return ERR_PTR(-ENOENT);
986766e6a4eSGrant Likely }
987766e6a4eSGrant Likely static inline struct clk *of_clk_get_by_name(struct device_node *np,
988766e6a4eSGrant Likely 					     const char *name)
989766e6a4eSGrant Likely {
9909f1612d3SShawn Guo 	return ERR_PTR(-ENOENT);
991766e6a4eSGrant Likely }
992428c9de5SGeert Uytterhoeven static inline struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
993428c9de5SGeert Uytterhoeven {
994428c9de5SGeert Uytterhoeven 	return ERR_PTR(-ENOENT);
995428c9de5SGeert Uytterhoeven }
996766e6a4eSGrant Likely #endif
997766e6a4eSGrant Likely 
998f8ce2547SRussell King #endif
999