xref: /openbmc/linux/include/linux/clk.h (revision e6fb7aee486c7fbd4d94f4894feaa6f0424c1740)
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 /**
1136d30d50dSJerome Brunet  * devm_clk_notifier_register - register a managed rate-change notifier callback
1146d30d50dSJerome Brunet  * @dev: device for clock "consumer"
1156d30d50dSJerome Brunet  * @clk: clock whose rate we are interested in
1166d30d50dSJerome Brunet  * @nb: notifier block with callback function pointer
1176d30d50dSJerome Brunet  *
1186d30d50dSJerome Brunet  * Returns 0 on success, -EERROR otherwise
1196d30d50dSJerome Brunet  */
120*e6fb7aeeSJerome Brunet int devm_clk_notifier_register(struct device *dev, struct clk *clk,
121*e6fb7aeeSJerome Brunet 			       struct notifier_block *nb);
1226d30d50dSJerome Brunet 
1236d30d50dSJerome Brunet /**
1245279fc40SBoris BREZILLON  * clk_get_accuracy - obtain the clock accuracy in ppb (parts per billion)
1255279fc40SBoris BREZILLON  *		      for a clock source.
1265279fc40SBoris BREZILLON  * @clk: clock source
1275279fc40SBoris BREZILLON  *
1285279fc40SBoris BREZILLON  * This gets the clock source accuracy expressed in ppb.
1295279fc40SBoris BREZILLON  * A perfect clock returns 0.
1305279fc40SBoris BREZILLON  */
1315279fc40SBoris BREZILLON long clk_get_accuracy(struct clk *clk);
1325279fc40SBoris BREZILLON 
133e59c5371SMike Turquette /**
134e59c5371SMike Turquette  * clk_set_phase - adjust the phase shift of a clock signal
135e59c5371SMike Turquette  * @clk: clock signal source
136e59c5371SMike Turquette  * @degrees: number of degrees the signal is shifted
137e59c5371SMike Turquette  *
138e59c5371SMike Turquette  * Shifts the phase of a clock signal by the specified degrees. Returns 0 on
139e59c5371SMike Turquette  * success, -EERROR otherwise.
140e59c5371SMike Turquette  */
141e59c5371SMike Turquette int clk_set_phase(struct clk *clk, int degrees);
142e59c5371SMike Turquette 
143e59c5371SMike Turquette /**
144e59c5371SMike Turquette  * clk_get_phase - return the phase shift of a clock signal
145e59c5371SMike Turquette  * @clk: clock signal source
146e59c5371SMike Turquette  *
147e59c5371SMike Turquette  * Returns the phase shift of a clock node in degrees, otherwise returns
148e59c5371SMike Turquette  * -EERROR.
149e59c5371SMike Turquette  */
150e59c5371SMike Turquette int clk_get_phase(struct clk *clk);
151e59c5371SMike Turquette 
1523d3801efSMichael Turquette /**
1539fba738aSJerome Brunet  * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
1549fba738aSJerome Brunet  * @clk: clock signal source
1559fba738aSJerome Brunet  * @num: numerator of the duty cycle ratio to be applied
1569fba738aSJerome Brunet  * @den: denominator of the duty cycle ratio to be applied
1579fba738aSJerome Brunet  *
1589fba738aSJerome Brunet  * Adjust the duty cycle of a clock signal by the specified ratio. Returns 0 on
1599fba738aSJerome Brunet  * success, -EERROR otherwise.
1609fba738aSJerome Brunet  */
1619fba738aSJerome Brunet int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den);
1629fba738aSJerome Brunet 
1639fba738aSJerome Brunet /**
1649fba738aSJerome Brunet  * clk_get_duty_cycle - return the duty cycle ratio of a clock signal
1659fba738aSJerome Brunet  * @clk: clock signal source
1669fba738aSJerome Brunet  * @scale: scaling factor to be applied to represent the ratio as an integer
1679fba738aSJerome Brunet  *
1689fba738aSJerome Brunet  * Returns the duty cycle ratio multiplied by the scale provided, otherwise
1699fba738aSJerome Brunet  * returns -EERROR.
1709fba738aSJerome Brunet  */
1719fba738aSJerome Brunet int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale);
1729fba738aSJerome Brunet 
1739fba738aSJerome Brunet /**
1743d3801efSMichael Turquette  * clk_is_match - check if two clk's point to the same hardware clock
1753d3801efSMichael Turquette  * @p: clk compared against q
1763d3801efSMichael Turquette  * @q: clk compared against p
1773d3801efSMichael Turquette  *
1783d3801efSMichael Turquette  * Returns true if the two struct clk pointers both point to the same hardware
1790e056eb5Smchehab@s-opensource.com  * clock node. Put differently, returns true if @p and @q
1800e056eb5Smchehab@s-opensource.com  * share the same &struct clk_core object.
1813d3801efSMichael Turquette  *
1823d3801efSMichael Turquette  * Returns false otherwise. Note that two NULL clks are treated as matching.
1833d3801efSMichael Turquette  */
1843d3801efSMichael Turquette bool clk_is_match(const struct clk *p, const struct clk *q);
1853d3801efSMichael Turquette 
1865279fc40SBoris BREZILLON #else
1875279fc40SBoris BREZILLON 
188e81b87d2SKrzysztof Kozlowski static inline int clk_notifier_register(struct clk *clk,
189e81b87d2SKrzysztof Kozlowski 					struct notifier_block *nb)
190e81b87d2SKrzysztof Kozlowski {
191e81b87d2SKrzysztof Kozlowski 	return -ENOTSUPP;
192e81b87d2SKrzysztof Kozlowski }
193e81b87d2SKrzysztof Kozlowski 
194e81b87d2SKrzysztof Kozlowski static inline int clk_notifier_unregister(struct clk *clk,
195e81b87d2SKrzysztof Kozlowski 					  struct notifier_block *nb)
196e81b87d2SKrzysztof Kozlowski {
197e81b87d2SKrzysztof Kozlowski 	return -ENOTSUPP;
198e81b87d2SKrzysztof Kozlowski }
199e81b87d2SKrzysztof Kozlowski 
200*e6fb7aeeSJerome Brunet static inline int devm_clk_notifier_register(struct device *dev,
201*e6fb7aeeSJerome Brunet 					     struct clk *clk,
202*e6fb7aeeSJerome Brunet 					     struct notifier_block *nb)
203*e6fb7aeeSJerome Brunet {
204*e6fb7aeeSJerome Brunet 	return -ENOTSUPP;
205*e6fb7aeeSJerome Brunet }
206*e6fb7aeeSJerome Brunet 
2075279fc40SBoris BREZILLON static inline long clk_get_accuracy(struct clk *clk)
2085279fc40SBoris BREZILLON {
2095279fc40SBoris BREZILLON 	return -ENOTSUPP;
2105279fc40SBoris BREZILLON }
2115279fc40SBoris BREZILLON 
212e59c5371SMike Turquette static inline long clk_set_phase(struct clk *clk, int phase)
213e59c5371SMike Turquette {
214e59c5371SMike Turquette 	return -ENOTSUPP;
215e59c5371SMike Turquette }
216e59c5371SMike Turquette 
217e59c5371SMike Turquette static inline long clk_get_phase(struct clk *clk)
218e59c5371SMike Turquette {
219e59c5371SMike Turquette 	return -ENOTSUPP;
220e59c5371SMike Turquette }
221e59c5371SMike Turquette 
2229fba738aSJerome Brunet static inline int clk_set_duty_cycle(struct clk *clk, unsigned int num,
2239fba738aSJerome Brunet 				     unsigned int den)
2249fba738aSJerome Brunet {
2259fba738aSJerome Brunet 	return -ENOTSUPP;
2269fba738aSJerome Brunet }
2279fba738aSJerome Brunet 
2289fba738aSJerome Brunet static inline unsigned int clk_get_scaled_duty_cycle(struct clk *clk,
2299fba738aSJerome Brunet 						     unsigned int scale)
2309fba738aSJerome Brunet {
2319fba738aSJerome Brunet 	return 0;
2329fba738aSJerome Brunet }
2339fba738aSJerome Brunet 
2343d3801efSMichael Turquette static inline bool clk_is_match(const struct clk *p, const struct clk *q)
2353d3801efSMichael Turquette {
2363d3801efSMichael Turquette 	return p == q;
2373d3801efSMichael Turquette }
2383d3801efSMichael Turquette 
2397e87aed9SMark Brown #endif
240b2476490SMike Turquette 
241f8ce2547SRussell King /**
24293abe8e4SViresh Kumar  * clk_prepare - prepare a clock source
24393abe8e4SViresh Kumar  * @clk: clock source
24493abe8e4SViresh Kumar  *
24593abe8e4SViresh Kumar  * This prepares the clock source for use.
24693abe8e4SViresh Kumar  *
24793abe8e4SViresh Kumar  * Must not be called from within atomic context.
24893abe8e4SViresh Kumar  */
24993abe8e4SViresh Kumar #ifdef CONFIG_HAVE_CLK_PREPARE
25093abe8e4SViresh Kumar int clk_prepare(struct clk *clk);
251266e4e9dSDong Aisheng int __must_check clk_bulk_prepare(int num_clks,
252266e4e9dSDong Aisheng 				  const struct clk_bulk_data *clks);
25393abe8e4SViresh Kumar #else
25493abe8e4SViresh Kumar static inline int clk_prepare(struct clk *clk)
25593abe8e4SViresh Kumar {
25693abe8e4SViresh Kumar 	might_sleep();
25793abe8e4SViresh Kumar 	return 0;
25893abe8e4SViresh Kumar }
259266e4e9dSDong Aisheng 
260570aaec7SAndrey Smirnov static inline int __must_check
261570aaec7SAndrey Smirnov clk_bulk_prepare(int num_clks, const struct clk_bulk_data *clks)
262266e4e9dSDong Aisheng {
263266e4e9dSDong Aisheng 	might_sleep();
264266e4e9dSDong Aisheng 	return 0;
265266e4e9dSDong Aisheng }
26693abe8e4SViresh Kumar #endif
26793abe8e4SViresh Kumar 
26893abe8e4SViresh Kumar /**
26993abe8e4SViresh Kumar  * clk_unprepare - undo preparation of a clock source
27093abe8e4SViresh Kumar  * @clk: clock source
27193abe8e4SViresh Kumar  *
27293abe8e4SViresh Kumar  * This undoes a previously prepared clock.  The caller must balance
27393abe8e4SViresh Kumar  * the number of prepare and unprepare calls.
27493abe8e4SViresh Kumar  *
27593abe8e4SViresh Kumar  * Must not be called from within atomic context.
27693abe8e4SViresh Kumar  */
27793abe8e4SViresh Kumar #ifdef CONFIG_HAVE_CLK_PREPARE
27893abe8e4SViresh Kumar void clk_unprepare(struct clk *clk);
279266e4e9dSDong Aisheng void clk_bulk_unprepare(int num_clks, const struct clk_bulk_data *clks);
28093abe8e4SViresh Kumar #else
28193abe8e4SViresh Kumar static inline void clk_unprepare(struct clk *clk)
28293abe8e4SViresh Kumar {
28393abe8e4SViresh Kumar 	might_sleep();
28493abe8e4SViresh Kumar }
285570aaec7SAndrey Smirnov static inline void clk_bulk_unprepare(int num_clks,
286570aaec7SAndrey Smirnov 				      const struct clk_bulk_data *clks)
287266e4e9dSDong Aisheng {
288266e4e9dSDong Aisheng 	might_sleep();
289266e4e9dSDong Aisheng }
29093abe8e4SViresh Kumar #endif
29193abe8e4SViresh Kumar 
29293abe8e4SViresh Kumar #ifdef CONFIG_HAVE_CLK
29393abe8e4SViresh Kumar /**
294f8ce2547SRussell King  * clk_get - lookup and obtain a reference to a clock producer.
295f8ce2547SRussell King  * @dev: device for clock "consumer"
296a58b3a4aSJan-Simon Möller  * @id: clock consumer ID
297f8ce2547SRussell King  *
298f8ce2547SRussell King  * Returns a struct clk corresponding to the clock producer, or
299f8ce2547SRussell King  * valid IS_ERR() condition containing errno.  The implementation
300f8ce2547SRussell King  * uses @dev and @id to determine the clock consumer, and thereby
301f8ce2547SRussell King  * the clock producer.  (IOW, @id may be identical strings, but
302f8ce2547SRussell King  * clk_get may return different clock producers depending on @dev.)
303f8ce2547SRussell King  *
304f8ce2547SRussell King  * Drivers must assume that the clock source is not enabled.
305f7ad160bSAlex Raimondi  *
306f7ad160bSAlex Raimondi  * clk_get should not be called from within interrupt context.
307f8ce2547SRussell King  */
308f8ce2547SRussell King struct clk *clk_get(struct device *dev, const char *id);
309f8ce2547SRussell King 
310f8ce2547SRussell King /**
311266e4e9dSDong Aisheng  * clk_bulk_get - lookup and obtain a number of references to clock producer.
312266e4e9dSDong Aisheng  * @dev: device for clock "consumer"
313266e4e9dSDong Aisheng  * @num_clks: the number of clk_bulk_data
314266e4e9dSDong Aisheng  * @clks: the clk_bulk_data table of consumer
315266e4e9dSDong Aisheng  *
316266e4e9dSDong Aisheng  * This helper function allows drivers to get several clk consumers in one
317266e4e9dSDong Aisheng  * operation. If any of the clk cannot be acquired then any clks
318266e4e9dSDong Aisheng  * that were obtained will be freed before returning to the caller.
319266e4e9dSDong Aisheng  *
320266e4e9dSDong Aisheng  * Returns 0 if all clocks specified in clk_bulk_data table are obtained
321266e4e9dSDong Aisheng  * successfully, or valid IS_ERR() condition containing errno.
322266e4e9dSDong Aisheng  * The implementation uses @dev and @clk_bulk_data.id to determine the
323266e4e9dSDong Aisheng  * clock consumer, and thereby the clock producer.
324266e4e9dSDong Aisheng  * The clock returned is stored in each @clk_bulk_data.clk field.
325266e4e9dSDong Aisheng  *
326266e4e9dSDong Aisheng  * Drivers must assume that the clock source is not enabled.
327266e4e9dSDong Aisheng  *
328266e4e9dSDong Aisheng  * clk_bulk_get should not be called from within interrupt context.
329266e4e9dSDong Aisheng  */
330266e4e9dSDong Aisheng int __must_check clk_bulk_get(struct device *dev, int num_clks,
331266e4e9dSDong Aisheng 			      struct clk_bulk_data *clks);
332616e45dfSDong Aisheng /**
333616e45dfSDong Aisheng  * clk_bulk_get_all - lookup and obtain all available references to clock
334616e45dfSDong Aisheng  *		      producer.
335616e45dfSDong Aisheng  * @dev: device for clock "consumer"
336616e45dfSDong Aisheng  * @clks: pointer to the clk_bulk_data table of consumer
337616e45dfSDong Aisheng  *
338616e45dfSDong Aisheng  * This helper function allows drivers to get all clk consumers in one
339616e45dfSDong Aisheng  * operation. If any of the clk cannot be acquired then any clks
340616e45dfSDong Aisheng  * that were obtained will be freed before returning to the caller.
341616e45dfSDong Aisheng  *
342616e45dfSDong Aisheng  * Returns a positive value for the number of clocks obtained while the
343616e45dfSDong Aisheng  * clock references are stored in the clk_bulk_data table in @clks field.
344616e45dfSDong Aisheng  * Returns 0 if there're none and a negative value if something failed.
345616e45dfSDong Aisheng  *
346616e45dfSDong Aisheng  * Drivers must assume that the clock source is not enabled.
347616e45dfSDong Aisheng  *
348616e45dfSDong Aisheng  * clk_bulk_get should not be called from within interrupt context.
349616e45dfSDong Aisheng  */
350616e45dfSDong Aisheng int __must_check clk_bulk_get_all(struct device *dev,
351616e45dfSDong Aisheng 				  struct clk_bulk_data **clks);
3522f25528eSSylwester Nawrocki 
3532f25528eSSylwester Nawrocki /**
3542f25528eSSylwester Nawrocki  * clk_bulk_get_optional - lookup and obtain a number of references to clock producer
3552f25528eSSylwester Nawrocki  * @dev: device for clock "consumer"
3562f25528eSSylwester Nawrocki  * @num_clks: the number of clk_bulk_data
3572f25528eSSylwester Nawrocki  * @clks: the clk_bulk_data table of consumer
3582f25528eSSylwester Nawrocki  *
3592f25528eSSylwester Nawrocki  * Behaves the same as clk_bulk_get() except where there is no clock producer.
3602f25528eSSylwester Nawrocki  * In this case, instead of returning -ENOENT, the function returns 0 and
3612f25528eSSylwester Nawrocki  * NULL for a clk for which a clock producer could not be determined.
3622f25528eSSylwester Nawrocki  */
3632f25528eSSylwester Nawrocki int __must_check clk_bulk_get_optional(struct device *dev, int num_clks,
3642f25528eSSylwester Nawrocki 				       struct clk_bulk_data *clks);
365266e4e9dSDong Aisheng /**
366618aee02SDong Aisheng  * devm_clk_bulk_get - managed get multiple clk consumers
367618aee02SDong Aisheng  * @dev: device for clock "consumer"
368618aee02SDong Aisheng  * @num_clks: the number of clk_bulk_data
369618aee02SDong Aisheng  * @clks: the clk_bulk_data table of consumer
370618aee02SDong Aisheng  *
371618aee02SDong Aisheng  * Return 0 on success, an errno on failure.
372618aee02SDong Aisheng  *
373618aee02SDong Aisheng  * This helper function allows drivers to get several clk
374618aee02SDong Aisheng  * consumers in one operation with management, the clks will
375618aee02SDong Aisheng  * automatically be freed when the device is unbound.
376618aee02SDong Aisheng  */
377618aee02SDong Aisheng int __must_check devm_clk_bulk_get(struct device *dev, int num_clks,
378618aee02SDong Aisheng 				   struct clk_bulk_data *clks);
379f08c2e28SDong Aisheng /**
3809bd5ef0bSSylwester Nawrocki  * devm_clk_bulk_get_optional - managed get multiple optional consumer clocks
3819bd5ef0bSSylwester Nawrocki  * @dev: device for clock "consumer"
3826ee82ef0SSylwester Nawrocki  * @num_clks: the number of clk_bulk_data
3839bd5ef0bSSylwester Nawrocki  * @clks: pointer to the clk_bulk_data table of consumer
3849bd5ef0bSSylwester Nawrocki  *
3859bd5ef0bSSylwester Nawrocki  * Behaves the same as devm_clk_bulk_get() except where there is no clock
3869bd5ef0bSSylwester Nawrocki  * producer.  In this case, instead of returning -ENOENT, the function returns
3879bd5ef0bSSylwester Nawrocki  * NULL for given clk. It is assumed all clocks in clk_bulk_data are optional.
3889bd5ef0bSSylwester Nawrocki  *
3899bd5ef0bSSylwester Nawrocki  * Returns 0 if all clocks specified in clk_bulk_data table are obtained
3909bd5ef0bSSylwester Nawrocki  * successfully or for any clk there was no clk provider available, otherwise
3919bd5ef0bSSylwester Nawrocki  * returns valid IS_ERR() condition containing errno.
3929bd5ef0bSSylwester Nawrocki  * The implementation uses @dev and @clk_bulk_data.id to determine the
3939bd5ef0bSSylwester Nawrocki  * clock consumer, and thereby the clock producer.
3949bd5ef0bSSylwester Nawrocki  * The clock returned is stored in each @clk_bulk_data.clk field.
3959bd5ef0bSSylwester Nawrocki  *
3969bd5ef0bSSylwester Nawrocki  * Drivers must assume that the clock source is not enabled.
3979bd5ef0bSSylwester Nawrocki  *
3989bd5ef0bSSylwester Nawrocki  * clk_bulk_get should not be called from within interrupt context.
3999bd5ef0bSSylwester Nawrocki  */
4009bd5ef0bSSylwester Nawrocki int __must_check devm_clk_bulk_get_optional(struct device *dev, int num_clks,
4019bd5ef0bSSylwester Nawrocki 					    struct clk_bulk_data *clks);
4029bd5ef0bSSylwester Nawrocki /**
403f08c2e28SDong Aisheng  * devm_clk_bulk_get_all - managed get multiple clk consumers
404f08c2e28SDong Aisheng  * @dev: device for clock "consumer"
405f08c2e28SDong Aisheng  * @clks: pointer to the clk_bulk_data table of consumer
406f08c2e28SDong Aisheng  *
407f08c2e28SDong Aisheng  * Returns a positive value for the number of clocks obtained while the
408f08c2e28SDong Aisheng  * clock references are stored in the clk_bulk_data table in @clks field.
409f08c2e28SDong Aisheng  * Returns 0 if there're none and a negative value if something failed.
410f08c2e28SDong Aisheng  *
411f08c2e28SDong Aisheng  * This helper function allows drivers to get several clk
412f08c2e28SDong Aisheng  * consumers in one operation with management, the clks will
413f08c2e28SDong Aisheng  * automatically be freed when the device is unbound.
414f08c2e28SDong Aisheng  */
415f08c2e28SDong Aisheng 
416f08c2e28SDong Aisheng int __must_check devm_clk_bulk_get_all(struct device *dev,
417f08c2e28SDong Aisheng 				       struct clk_bulk_data **clks);
418618aee02SDong Aisheng 
419618aee02SDong Aisheng /**
420a8a97db9SMark Brown  * devm_clk_get - lookup and obtain a managed reference to a clock producer.
421a8a97db9SMark Brown  * @dev: device for clock "consumer"
422a58b3a4aSJan-Simon Möller  * @id: clock consumer ID
423a8a97db9SMark Brown  *
424a8a97db9SMark Brown  * Returns a struct clk corresponding to the clock producer, or
425a8a97db9SMark Brown  * valid IS_ERR() condition containing errno.  The implementation
426a8a97db9SMark Brown  * uses @dev and @id to determine the clock consumer, and thereby
427a8a97db9SMark Brown  * the clock producer.  (IOW, @id may be identical strings, but
428a8a97db9SMark Brown  * clk_get may return different clock producers depending on @dev.)
429a8a97db9SMark Brown  *
430a8a97db9SMark Brown  * Drivers must assume that the clock source is not enabled.
431a8a97db9SMark Brown  *
432a8a97db9SMark Brown  * devm_clk_get should not be called from within interrupt context.
433a8a97db9SMark Brown  *
434a8a97db9SMark Brown  * The clock will automatically be freed when the device is unbound
435a8a97db9SMark Brown  * from the bus.
436a8a97db9SMark Brown  */
437a8a97db9SMark Brown struct clk *devm_clk_get(struct device *dev, const char *id);
438a8a97db9SMark Brown 
439a8a97db9SMark Brown /**
44060b8f0ddSPhil Edworthy  * devm_clk_get_optional - lookup and obtain a managed reference to an optional
44160b8f0ddSPhil Edworthy  *			   clock producer.
44260b8f0ddSPhil Edworthy  * @dev: device for clock "consumer"
44360b8f0ddSPhil Edworthy  * @id: clock consumer ID
44460b8f0ddSPhil Edworthy  *
44560b8f0ddSPhil Edworthy  * Behaves the same as devm_clk_get() except where there is no clock producer.
44660b8f0ddSPhil Edworthy  * In this case, instead of returning -ENOENT, the function returns NULL.
44760b8f0ddSPhil Edworthy  */
44860b8f0ddSPhil Edworthy struct clk *devm_clk_get_optional(struct device *dev, const char *id);
44960b8f0ddSPhil Edworthy 
45060b8f0ddSPhil Edworthy /**
45171a2f115SKuninori Morimoto  * devm_get_clk_from_child - lookup and obtain a managed reference to a
45271a2f115SKuninori Morimoto  *			     clock producer from child node.
45371a2f115SKuninori Morimoto  * @dev: device for clock "consumer"
45471a2f115SKuninori Morimoto  * @np: pointer to clock consumer node
45571a2f115SKuninori Morimoto  * @con_id: clock consumer ID
45671a2f115SKuninori Morimoto  *
45771a2f115SKuninori Morimoto  * This function parses the clocks, and uses them to look up the
45871a2f115SKuninori Morimoto  * struct clk from the registered list of clock providers by using
45971a2f115SKuninori Morimoto  * @np and @con_id
46071a2f115SKuninori Morimoto  *
46171a2f115SKuninori Morimoto  * The clock will automatically be freed when the device is unbound
46271a2f115SKuninori Morimoto  * from the bus.
46371a2f115SKuninori Morimoto  */
46471a2f115SKuninori Morimoto struct clk *devm_get_clk_from_child(struct device *dev,
46571a2f115SKuninori Morimoto 				    struct device_node *np, const char *con_id);
46655e9b8b7SJerome Brunet /**
46755e9b8b7SJerome Brunet  * clk_rate_exclusive_get - get exclusivity over the rate control of a
46855e9b8b7SJerome Brunet  *                          producer
46955e9b8b7SJerome Brunet  * @clk: clock source
47055e9b8b7SJerome Brunet  *
47155e9b8b7SJerome Brunet  * This function allows drivers to get exclusive control over the rate of a
47255e9b8b7SJerome Brunet  * provider. It prevents any other consumer to execute, even indirectly,
47355e9b8b7SJerome Brunet  * opereation which could alter the rate of the provider or cause glitches
47455e9b8b7SJerome Brunet  *
47555e9b8b7SJerome Brunet  * If exlusivity is claimed more than once on clock, even by the same driver,
47655e9b8b7SJerome Brunet  * the rate effectively gets locked as exclusivity can't be preempted.
47755e9b8b7SJerome Brunet  *
47855e9b8b7SJerome Brunet  * Must not be called from within atomic context.
47955e9b8b7SJerome Brunet  *
48055e9b8b7SJerome Brunet  * Returns success (0) or negative errno.
48155e9b8b7SJerome Brunet  */
48255e9b8b7SJerome Brunet int clk_rate_exclusive_get(struct clk *clk);
48355e9b8b7SJerome Brunet 
48455e9b8b7SJerome Brunet /**
48555e9b8b7SJerome Brunet  * clk_rate_exclusive_put - release exclusivity over the rate control of a
48655e9b8b7SJerome Brunet  *                          producer
48755e9b8b7SJerome Brunet  * @clk: clock source
48855e9b8b7SJerome Brunet  *
48955e9b8b7SJerome Brunet  * This function allows drivers to release the exclusivity it previously got
49055e9b8b7SJerome Brunet  * from clk_rate_exclusive_get()
49155e9b8b7SJerome Brunet  *
49255e9b8b7SJerome Brunet  * The caller must balance the number of clk_rate_exclusive_get() and
49355e9b8b7SJerome Brunet  * clk_rate_exclusive_put() calls.
49455e9b8b7SJerome Brunet  *
49555e9b8b7SJerome Brunet  * Must not be called from within atomic context.
49655e9b8b7SJerome Brunet  */
49755e9b8b7SJerome Brunet void clk_rate_exclusive_put(struct clk *clk);
49871a2f115SKuninori Morimoto 
49971a2f115SKuninori Morimoto /**
500f8ce2547SRussell King  * clk_enable - inform the system when the clock source should be running.
501f8ce2547SRussell King  * @clk: clock source
502f8ce2547SRussell King  *
503f8ce2547SRussell King  * If the clock can not be enabled/disabled, this should return success.
504f8ce2547SRussell King  *
50540d3e0f4SRussell King  * May be called from atomic contexts.
50640d3e0f4SRussell King  *
507f8ce2547SRussell King  * Returns success (0) or negative errno.
508f8ce2547SRussell King  */
509f8ce2547SRussell King int clk_enable(struct clk *clk);
510f8ce2547SRussell King 
511f8ce2547SRussell King /**
512266e4e9dSDong Aisheng  * clk_bulk_enable - inform the system when the set of clks should be running.
513266e4e9dSDong Aisheng  * @num_clks: the number of clk_bulk_data
514266e4e9dSDong Aisheng  * @clks: the clk_bulk_data table of consumer
515266e4e9dSDong Aisheng  *
516266e4e9dSDong Aisheng  * May be called from atomic contexts.
517266e4e9dSDong Aisheng  *
518266e4e9dSDong Aisheng  * Returns success (0) or negative errno.
519266e4e9dSDong Aisheng  */
520266e4e9dSDong Aisheng int __must_check clk_bulk_enable(int num_clks,
521266e4e9dSDong Aisheng 				 const struct clk_bulk_data *clks);
522266e4e9dSDong Aisheng 
523266e4e9dSDong Aisheng /**
524f8ce2547SRussell King  * clk_disable - inform the system when the clock source is no longer required.
525f8ce2547SRussell King  * @clk: clock source
526f8ce2547SRussell King  *
527f8ce2547SRussell King  * Inform the system that a clock source is no longer required by
528f8ce2547SRussell King  * a driver and may be shut down.
529f8ce2547SRussell King  *
53040d3e0f4SRussell King  * May be called from atomic contexts.
53140d3e0f4SRussell King  *
532f8ce2547SRussell King  * Implementation detail: if the clock source is shared between
533f8ce2547SRussell King  * multiple drivers, clk_enable() calls must be balanced by the
534f8ce2547SRussell King  * same number of clk_disable() calls for the clock source to be
535f8ce2547SRussell King  * disabled.
536f8ce2547SRussell King  */
537f8ce2547SRussell King void clk_disable(struct clk *clk);
538f8ce2547SRussell King 
539f8ce2547SRussell King /**
540266e4e9dSDong Aisheng  * clk_bulk_disable - inform the system when the set of clks is no
541266e4e9dSDong Aisheng  *		      longer required.
542266e4e9dSDong Aisheng  * @num_clks: the number of clk_bulk_data
543266e4e9dSDong Aisheng  * @clks: the clk_bulk_data table of consumer
544266e4e9dSDong Aisheng  *
545266e4e9dSDong Aisheng  * Inform the system that a set of clks is no longer required by
546266e4e9dSDong Aisheng  * a driver and may be shut down.
547266e4e9dSDong Aisheng  *
548266e4e9dSDong Aisheng  * May be called from atomic contexts.
549266e4e9dSDong Aisheng  *
550266e4e9dSDong Aisheng  * Implementation detail: if the set of clks is shared between
551266e4e9dSDong Aisheng  * multiple drivers, clk_bulk_enable() calls must be balanced by the
552266e4e9dSDong Aisheng  * same number of clk_bulk_disable() calls for the clock source to be
553266e4e9dSDong Aisheng  * disabled.
554266e4e9dSDong Aisheng  */
555266e4e9dSDong Aisheng void clk_bulk_disable(int num_clks, const struct clk_bulk_data *clks);
556266e4e9dSDong Aisheng 
557266e4e9dSDong Aisheng /**
558f8ce2547SRussell King  * clk_get_rate - obtain the current clock rate (in Hz) for a clock source.
559f8ce2547SRussell King  *		  This is only valid once the clock source has been enabled.
560f8ce2547SRussell King  * @clk: clock source
561f8ce2547SRussell King  */
562f8ce2547SRussell King unsigned long clk_get_rate(struct clk *clk);
563f8ce2547SRussell King 
564f8ce2547SRussell King /**
565f8ce2547SRussell King  * clk_put	- "free" the clock source
566f8ce2547SRussell King  * @clk: clock source
567f8ce2547SRussell King  *
568f8ce2547SRussell King  * Note: drivers must ensure that all clk_enable calls made on this
569f8ce2547SRussell King  * clock source are balanced by clk_disable calls prior to calling
570f8ce2547SRussell King  * this function.
571f7ad160bSAlex Raimondi  *
572f7ad160bSAlex Raimondi  * clk_put should not be called from within interrupt context.
573f8ce2547SRussell King  */
574f8ce2547SRussell King void clk_put(struct clk *clk);
575f8ce2547SRussell King 
576a8a97db9SMark Brown /**
577266e4e9dSDong Aisheng  * clk_bulk_put	- "free" the clock source
578266e4e9dSDong Aisheng  * @num_clks: the number of clk_bulk_data
579266e4e9dSDong Aisheng  * @clks: the clk_bulk_data table of consumer
580266e4e9dSDong Aisheng  *
581266e4e9dSDong Aisheng  * Note: drivers must ensure that all clk_bulk_enable calls made on this
582266e4e9dSDong Aisheng  * clock source are balanced by clk_bulk_disable calls prior to calling
583266e4e9dSDong Aisheng  * this function.
584266e4e9dSDong Aisheng  *
585266e4e9dSDong Aisheng  * clk_bulk_put should not be called from within interrupt context.
586266e4e9dSDong Aisheng  */
587266e4e9dSDong Aisheng void clk_bulk_put(int num_clks, struct clk_bulk_data *clks);
588266e4e9dSDong Aisheng 
589266e4e9dSDong Aisheng /**
590616e45dfSDong Aisheng  * clk_bulk_put_all - "free" all the clock source
591616e45dfSDong Aisheng  * @num_clks: the number of clk_bulk_data
592616e45dfSDong Aisheng  * @clks: the clk_bulk_data table of consumer
593616e45dfSDong Aisheng  *
594616e45dfSDong Aisheng  * Note: drivers must ensure that all clk_bulk_enable calls made on this
595616e45dfSDong Aisheng  * clock source are balanced by clk_bulk_disable calls prior to calling
596616e45dfSDong Aisheng  * this function.
597616e45dfSDong Aisheng  *
598616e45dfSDong Aisheng  * clk_bulk_put_all should not be called from within interrupt context.
599616e45dfSDong Aisheng  */
600616e45dfSDong Aisheng void clk_bulk_put_all(int num_clks, struct clk_bulk_data *clks);
601616e45dfSDong Aisheng 
602616e45dfSDong Aisheng /**
603a8a97db9SMark Brown  * devm_clk_put	- "free" a managed clock source
604da3dae54SMasanari Iida  * @dev: device used to acquire the clock
605a8a97db9SMark Brown  * @clk: clock source acquired with devm_clk_get()
606a8a97db9SMark Brown  *
607a8a97db9SMark Brown  * Note: drivers must ensure that all clk_enable calls made on this
608a8a97db9SMark Brown  * clock source are balanced by clk_disable calls prior to calling
609a8a97db9SMark Brown  * this function.
610a8a97db9SMark Brown  *
611a8a97db9SMark Brown  * clk_put should not be called from within interrupt context.
612a8a97db9SMark Brown  */
613a8a97db9SMark Brown void devm_clk_put(struct device *dev, struct clk *clk);
614f8ce2547SRussell King 
615f8ce2547SRussell King /*
616f8ce2547SRussell King  * The remaining APIs are optional for machine class support.
617f8ce2547SRussell King  */
618f8ce2547SRussell King 
619f8ce2547SRussell King 
620f8ce2547SRussell King /**
621f8ce2547SRussell King  * clk_round_rate - adjust a rate to the exact rate a clock can provide
622f8ce2547SRussell King  * @clk: clock source
623f8ce2547SRussell King  * @rate: desired clock rate in Hz
624f8ce2547SRussell King  *
625d2d14a77SRussell King  * This answers the question "if I were to pass @rate to clk_set_rate(),
626d2d14a77SRussell King  * what clock rate would I end up with?" without changing the hardware
627d2d14a77SRussell King  * in any way.  In other words:
628d2d14a77SRussell King  *
629d2d14a77SRussell King  *   rate = clk_round_rate(clk, r);
630d2d14a77SRussell King  *
631d2d14a77SRussell King  * and:
632d2d14a77SRussell King  *
633d2d14a77SRussell King  *   clk_set_rate(clk, r);
634d2d14a77SRussell King  *   rate = clk_get_rate(clk);
635d2d14a77SRussell King  *
636d2d14a77SRussell King  * are equivalent except the former does not modify the clock hardware
637d2d14a77SRussell King  * in any way.
638d2d14a77SRussell King  *
639f8ce2547SRussell King  * Returns rounded clock rate in Hz, or negative errno.
640f8ce2547SRussell King  */
641f8ce2547SRussell King long clk_round_rate(struct clk *clk, unsigned long rate);
642f8ce2547SRussell King 
643f8ce2547SRussell King /**
644f8ce2547SRussell King  * clk_set_rate - set the clock rate for a clock source
645f8ce2547SRussell King  * @clk: clock source
646f8ce2547SRussell King  * @rate: desired clock rate in Hz
647f8ce2547SRussell King  *
64864c76b31SMartin Blumenstingl  * Updating the rate starts at the top-most affected clock and then
64964c76b31SMartin Blumenstingl  * walks the tree down to the bottom-most clock that needs updating.
65064c76b31SMartin Blumenstingl  *
651f8ce2547SRussell King  * Returns success (0) or negative errno.
652f8ce2547SRussell King  */
653f8ce2547SRussell King int clk_set_rate(struct clk *clk, unsigned long rate);
654f8ce2547SRussell King 
655f8ce2547SRussell King /**
65655e9b8b7SJerome Brunet  * clk_set_rate_exclusive- set the clock rate and claim exclusivity over
65755e9b8b7SJerome Brunet  *                         clock source
65855e9b8b7SJerome Brunet  * @clk: clock source
65955e9b8b7SJerome Brunet  * @rate: desired clock rate in Hz
66055e9b8b7SJerome Brunet  *
66155e9b8b7SJerome Brunet  * This helper function allows drivers to atomically set the rate of a producer
66255e9b8b7SJerome Brunet  * and claim exclusivity over the rate control of the producer.
66355e9b8b7SJerome Brunet  *
66455e9b8b7SJerome Brunet  * It is essentially a combination of clk_set_rate() and
66555e9b8b7SJerome Brunet  * clk_rate_exclusite_get(). Caller must balance this call with a call to
66655e9b8b7SJerome Brunet  * clk_rate_exclusive_put()
66755e9b8b7SJerome Brunet  *
66855e9b8b7SJerome Brunet  * Returns success (0) or negative errno.
66955e9b8b7SJerome Brunet  */
67055e9b8b7SJerome Brunet int clk_set_rate_exclusive(struct clk *clk, unsigned long rate);
67155e9b8b7SJerome Brunet 
67255e9b8b7SJerome Brunet /**
6734e88f3deSThierry Reding  * clk_has_parent - check if a clock is a possible parent for another
6744e88f3deSThierry Reding  * @clk: clock source
6754e88f3deSThierry Reding  * @parent: parent clock source
6764e88f3deSThierry Reding  *
6774e88f3deSThierry Reding  * This function can be used in drivers that need to check that a clock can be
6784e88f3deSThierry Reding  * the parent of another without actually changing the parent.
6794e88f3deSThierry Reding  *
6804e88f3deSThierry Reding  * Returns true if @parent is a possible parent for @clk, false otherwise.
6814e88f3deSThierry Reding  */
6824e88f3deSThierry Reding bool clk_has_parent(struct clk *clk, struct clk *parent);
6834e88f3deSThierry Reding 
6844e88f3deSThierry Reding /**
6851c8e6004STomeu Vizoso  * clk_set_rate_range - set a rate range for a clock source
6861c8e6004STomeu Vizoso  * @clk: clock source
6871c8e6004STomeu Vizoso  * @min: desired minimum clock rate in Hz, inclusive
6881c8e6004STomeu Vizoso  * @max: desired maximum clock rate in Hz, inclusive
6891c8e6004STomeu Vizoso  *
6901c8e6004STomeu Vizoso  * Returns success (0) or negative errno.
6911c8e6004STomeu Vizoso  */
6921c8e6004STomeu Vizoso int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max);
6931c8e6004STomeu Vizoso 
6941c8e6004STomeu Vizoso /**
6951c8e6004STomeu Vizoso  * clk_set_min_rate - set a minimum clock rate for a clock source
6961c8e6004STomeu Vizoso  * @clk: clock source
6971c8e6004STomeu Vizoso  * @rate: desired minimum clock rate in Hz, inclusive
6981c8e6004STomeu Vizoso  *
6991c8e6004STomeu Vizoso  * Returns success (0) or negative errno.
7001c8e6004STomeu Vizoso  */
7011c8e6004STomeu Vizoso int clk_set_min_rate(struct clk *clk, unsigned long rate);
7021c8e6004STomeu Vizoso 
7031c8e6004STomeu Vizoso /**
7041c8e6004STomeu Vizoso  * clk_set_max_rate - set a maximum clock rate for a clock source
7051c8e6004STomeu Vizoso  * @clk: clock source
7061c8e6004STomeu Vizoso  * @rate: desired maximum clock rate in Hz, inclusive
7071c8e6004STomeu Vizoso  *
7081c8e6004STomeu Vizoso  * Returns success (0) or negative errno.
7091c8e6004STomeu Vizoso  */
7101c8e6004STomeu Vizoso int clk_set_max_rate(struct clk *clk, unsigned long rate);
7111c8e6004STomeu Vizoso 
7121c8e6004STomeu Vizoso /**
713f8ce2547SRussell King  * clk_set_parent - set the parent clock source for this clock
714f8ce2547SRussell King  * @clk: clock source
715f8ce2547SRussell King  * @parent: parent clock source
716f8ce2547SRussell King  *
717f8ce2547SRussell King  * Returns success (0) or negative errno.
718f8ce2547SRussell King  */
719f8ce2547SRussell King int clk_set_parent(struct clk *clk, struct clk *parent);
720f8ce2547SRussell King 
721f8ce2547SRussell King /**
722f8ce2547SRussell King  * clk_get_parent - get the parent clock source for this clock
723f8ce2547SRussell King  * @clk: clock source
724f8ce2547SRussell King  *
725f8ce2547SRussell King  * Returns struct clk corresponding to parent clock source, or
726f8ce2547SRussell King  * valid IS_ERR() condition containing errno.
727f8ce2547SRussell King  */
728f8ce2547SRussell King struct clk *clk_get_parent(struct clk *clk);
729f8ce2547SRussell King 
73005fd8e73SSascha Hauer /**
73105fd8e73SSascha Hauer  * clk_get_sys - get a clock based upon the device name
73205fd8e73SSascha Hauer  * @dev_id: device name
73305fd8e73SSascha Hauer  * @con_id: connection ID
73405fd8e73SSascha Hauer  *
73505fd8e73SSascha Hauer  * Returns a struct clk corresponding to the clock producer, or
73605fd8e73SSascha Hauer  * valid IS_ERR() condition containing errno.  The implementation
73705fd8e73SSascha Hauer  * uses @dev_id and @con_id to determine the clock consumer, and
73805fd8e73SSascha Hauer  * thereby the clock producer. In contrast to clk_get() this function
73905fd8e73SSascha Hauer  * takes the device name instead of the device itself for identification.
74005fd8e73SSascha Hauer  *
74105fd8e73SSascha Hauer  * Drivers must assume that the clock source is not enabled.
74205fd8e73SSascha Hauer  *
74305fd8e73SSascha Hauer  * clk_get_sys should not be called from within interrupt context.
74405fd8e73SSascha Hauer  */
74505fd8e73SSascha Hauer struct clk *clk_get_sys(const char *dev_id, const char *con_id);
74605fd8e73SSascha Hauer 
7478b95d1ceSRuss Dill /**
7488b95d1ceSRuss Dill  * clk_save_context - save clock context for poweroff
7498b95d1ceSRuss Dill  *
7508b95d1ceSRuss Dill  * Saves the context of the clock register for powerstates in which the
7518b95d1ceSRuss Dill  * contents of the registers will be lost. Occurs deep within the suspend
7528b95d1ceSRuss Dill  * code so locking is not necessary.
7538b95d1ceSRuss Dill  */
7548b95d1ceSRuss Dill int clk_save_context(void);
7558b95d1ceSRuss Dill 
7568b95d1ceSRuss Dill /**
7578b95d1ceSRuss Dill  * clk_restore_context - restore clock context after poweroff
7588b95d1ceSRuss Dill  *
7598b95d1ceSRuss Dill  * This occurs with all clocks enabled. Occurs deep within the resume code
7608b95d1ceSRuss Dill  * so locking is not necessary.
7618b95d1ceSRuss Dill  */
7628b95d1ceSRuss Dill void clk_restore_context(void);
7638b95d1ceSRuss Dill 
76493abe8e4SViresh Kumar #else /* !CONFIG_HAVE_CLK */
76593abe8e4SViresh Kumar 
76693abe8e4SViresh Kumar static inline struct clk *clk_get(struct device *dev, const char *id)
76793abe8e4SViresh Kumar {
76893abe8e4SViresh Kumar 	return NULL;
76993abe8e4SViresh Kumar }
77093abe8e4SViresh Kumar 
7716e0d4ff4SDong Aisheng static inline int __must_check clk_bulk_get(struct device *dev, int num_clks,
772266e4e9dSDong Aisheng 					    struct clk_bulk_data *clks)
773266e4e9dSDong Aisheng {
774266e4e9dSDong Aisheng 	return 0;
775266e4e9dSDong Aisheng }
776266e4e9dSDong Aisheng 
7772f25528eSSylwester Nawrocki static inline int __must_check clk_bulk_get_optional(struct device *dev,
7782f25528eSSylwester Nawrocki 				int num_clks, struct clk_bulk_data *clks)
7792f25528eSSylwester Nawrocki {
7802f25528eSSylwester Nawrocki 	return 0;
7812f25528eSSylwester Nawrocki }
7822f25528eSSylwester Nawrocki 
783616e45dfSDong Aisheng static inline int __must_check clk_bulk_get_all(struct device *dev,
784616e45dfSDong Aisheng 					 struct clk_bulk_data **clks)
785616e45dfSDong Aisheng {
786616e45dfSDong Aisheng 	return 0;
787616e45dfSDong Aisheng }
788616e45dfSDong Aisheng 
78993abe8e4SViresh Kumar static inline struct clk *devm_clk_get(struct device *dev, const char *id)
79093abe8e4SViresh Kumar {
79193abe8e4SViresh Kumar 	return NULL;
79293abe8e4SViresh Kumar }
79393abe8e4SViresh Kumar 
79460b8f0ddSPhil Edworthy static inline struct clk *devm_clk_get_optional(struct device *dev,
79560b8f0ddSPhil Edworthy 						const char *id)
79660b8f0ddSPhil Edworthy {
79760b8f0ddSPhil Edworthy 	return NULL;
79860b8f0ddSPhil Edworthy }
79960b8f0ddSPhil Edworthy 
8006e0d4ff4SDong Aisheng static inline int __must_check devm_clk_bulk_get(struct device *dev, int num_clks,
801618aee02SDong Aisheng 						 struct clk_bulk_data *clks)
802618aee02SDong Aisheng {
803618aee02SDong Aisheng 	return 0;
804618aee02SDong Aisheng }
805618aee02SDong Aisheng 
8069bd5ef0bSSylwester Nawrocki static inline int __must_check devm_clk_bulk_get_optional(struct device *dev,
8079bd5ef0bSSylwester Nawrocki 				int num_clks, struct clk_bulk_data *clks)
8089bd5ef0bSSylwester Nawrocki {
8099bd5ef0bSSylwester Nawrocki 	return 0;
8109bd5ef0bSSylwester Nawrocki }
8119bd5ef0bSSylwester Nawrocki 
812f08c2e28SDong Aisheng static inline int __must_check devm_clk_bulk_get_all(struct device *dev,
813f08c2e28SDong Aisheng 						     struct clk_bulk_data **clks)
814f08c2e28SDong Aisheng {
815f08c2e28SDong Aisheng 
816f08c2e28SDong Aisheng 	return 0;
817f08c2e28SDong Aisheng }
818f08c2e28SDong Aisheng 
81971a2f115SKuninori Morimoto static inline struct clk *devm_get_clk_from_child(struct device *dev,
82071a2f115SKuninori Morimoto 				struct device_node *np, const char *con_id)
82171a2f115SKuninori Morimoto {
82271a2f115SKuninori Morimoto 	return NULL;
82371a2f115SKuninori Morimoto }
82471a2f115SKuninori Morimoto 
82593abe8e4SViresh Kumar static inline void clk_put(struct clk *clk) {}
82693abe8e4SViresh Kumar 
827266e4e9dSDong Aisheng static inline void clk_bulk_put(int num_clks, struct clk_bulk_data *clks) {}
828266e4e9dSDong Aisheng 
829616e45dfSDong Aisheng static inline void clk_bulk_put_all(int num_clks, struct clk_bulk_data *clks) {}
830616e45dfSDong Aisheng 
83193abe8e4SViresh Kumar static inline void devm_clk_put(struct device *dev, struct clk *clk) {}
83293abe8e4SViresh Kumar 
83355e9b8b7SJerome Brunet 
83455e9b8b7SJerome Brunet static inline int clk_rate_exclusive_get(struct clk *clk)
83555e9b8b7SJerome Brunet {
83655e9b8b7SJerome Brunet 	return 0;
83755e9b8b7SJerome Brunet }
83855e9b8b7SJerome Brunet 
83955e9b8b7SJerome Brunet static inline void clk_rate_exclusive_put(struct clk *clk) {}
84055e9b8b7SJerome Brunet 
84193abe8e4SViresh Kumar static inline int clk_enable(struct clk *clk)
84293abe8e4SViresh Kumar {
84393abe8e4SViresh Kumar 	return 0;
84493abe8e4SViresh Kumar }
84593abe8e4SViresh Kumar 
846570aaec7SAndrey Smirnov static inline int __must_check clk_bulk_enable(int num_clks,
847570aaec7SAndrey Smirnov 					       const struct clk_bulk_data *clks)
848266e4e9dSDong Aisheng {
849266e4e9dSDong Aisheng 	return 0;
850266e4e9dSDong Aisheng }
851266e4e9dSDong Aisheng 
85293abe8e4SViresh Kumar static inline void clk_disable(struct clk *clk) {}
85393abe8e4SViresh Kumar 
854266e4e9dSDong Aisheng 
855266e4e9dSDong Aisheng static inline void clk_bulk_disable(int num_clks,
856570aaec7SAndrey Smirnov 				    const struct clk_bulk_data *clks) {}
857266e4e9dSDong Aisheng 
85893abe8e4SViresh Kumar static inline unsigned long clk_get_rate(struct clk *clk)
85993abe8e4SViresh Kumar {
86093abe8e4SViresh Kumar 	return 0;
86193abe8e4SViresh Kumar }
86293abe8e4SViresh Kumar 
86393abe8e4SViresh Kumar static inline int clk_set_rate(struct clk *clk, unsigned long rate)
86493abe8e4SViresh Kumar {
86593abe8e4SViresh Kumar 	return 0;
86693abe8e4SViresh Kumar }
86793abe8e4SViresh Kumar 
86855e9b8b7SJerome Brunet static inline int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
86955e9b8b7SJerome Brunet {
87055e9b8b7SJerome Brunet 	return 0;
87155e9b8b7SJerome Brunet }
87255e9b8b7SJerome Brunet 
87393abe8e4SViresh Kumar static inline long clk_round_rate(struct clk *clk, unsigned long rate)
87493abe8e4SViresh Kumar {
87593abe8e4SViresh Kumar 	return 0;
87693abe8e4SViresh Kumar }
87793abe8e4SViresh Kumar 
8784e88f3deSThierry Reding static inline bool clk_has_parent(struct clk *clk, struct clk *parent)
8794e88f3deSThierry Reding {
8804e88f3deSThierry Reding 	return true;
8814e88f3deSThierry Reding }
8824e88f3deSThierry Reding 
883b88c9f41SDmitry Osipenko static inline int clk_set_rate_range(struct clk *clk, unsigned long min,
884b88c9f41SDmitry Osipenko 				     unsigned long max)
885b88c9f41SDmitry Osipenko {
886b88c9f41SDmitry Osipenko 	return 0;
887b88c9f41SDmitry Osipenko }
888b88c9f41SDmitry Osipenko 
889b88c9f41SDmitry Osipenko static inline int clk_set_min_rate(struct clk *clk, unsigned long rate)
890b88c9f41SDmitry Osipenko {
891b88c9f41SDmitry Osipenko 	return 0;
892b88c9f41SDmitry Osipenko }
893b88c9f41SDmitry Osipenko 
894b88c9f41SDmitry Osipenko static inline int clk_set_max_rate(struct clk *clk, unsigned long rate)
895b88c9f41SDmitry Osipenko {
896b88c9f41SDmitry Osipenko 	return 0;
897b88c9f41SDmitry Osipenko }
898b88c9f41SDmitry Osipenko 
89993abe8e4SViresh Kumar static inline int clk_set_parent(struct clk *clk, struct clk *parent)
90093abe8e4SViresh Kumar {
90193abe8e4SViresh Kumar 	return 0;
90293abe8e4SViresh Kumar }
90393abe8e4SViresh Kumar 
90493abe8e4SViresh Kumar static inline struct clk *clk_get_parent(struct clk *clk)
90593abe8e4SViresh Kumar {
90693abe8e4SViresh Kumar 	return NULL;
90793abe8e4SViresh Kumar }
90893abe8e4SViresh Kumar 
909b81ea968SDaniel Lezcano static inline struct clk *clk_get_sys(const char *dev_id, const char *con_id)
910b81ea968SDaniel Lezcano {
911b81ea968SDaniel Lezcano 	return NULL;
912b81ea968SDaniel Lezcano }
9138b95d1ceSRuss Dill 
9148b95d1ceSRuss Dill static inline int clk_save_context(void)
9158b95d1ceSRuss Dill {
9168b95d1ceSRuss Dill 	return 0;
9178b95d1ceSRuss Dill }
9188b95d1ceSRuss Dill 
9198b95d1ceSRuss Dill static inline void clk_restore_context(void) {}
9208b95d1ceSRuss Dill 
92193abe8e4SViresh Kumar #endif
92293abe8e4SViresh Kumar 
92393abe8e4SViresh Kumar /* clk_prepare_enable helps cases using clk_enable in non-atomic context. */
92493abe8e4SViresh Kumar static inline int clk_prepare_enable(struct clk *clk)
92593abe8e4SViresh Kumar {
92693abe8e4SViresh Kumar 	int ret;
92793abe8e4SViresh Kumar 
92893abe8e4SViresh Kumar 	ret = clk_prepare(clk);
92993abe8e4SViresh Kumar 	if (ret)
93093abe8e4SViresh Kumar 		return ret;
93193abe8e4SViresh Kumar 	ret = clk_enable(clk);
93293abe8e4SViresh Kumar 	if (ret)
93393abe8e4SViresh Kumar 		clk_unprepare(clk);
93493abe8e4SViresh Kumar 
93593abe8e4SViresh Kumar 	return ret;
93693abe8e4SViresh Kumar }
93793abe8e4SViresh Kumar 
93893abe8e4SViresh Kumar /* clk_disable_unprepare helps cases using clk_disable in non-atomic context. */
93993abe8e4SViresh Kumar static inline void clk_disable_unprepare(struct clk *clk)
94093abe8e4SViresh Kumar {
94193abe8e4SViresh Kumar 	clk_disable(clk);
94293abe8e4SViresh Kumar 	clk_unprepare(clk);
94393abe8e4SViresh Kumar }
94493abe8e4SViresh Kumar 
945570aaec7SAndrey Smirnov static inline int __must_check
946570aaec7SAndrey Smirnov clk_bulk_prepare_enable(int num_clks, const struct clk_bulk_data *clks)
9473c48d86cSBjorn Andersson {
9483c48d86cSBjorn Andersson 	int ret;
9493c48d86cSBjorn Andersson 
9503c48d86cSBjorn Andersson 	ret = clk_bulk_prepare(num_clks, clks);
9513c48d86cSBjorn Andersson 	if (ret)
9523c48d86cSBjorn Andersson 		return ret;
9533c48d86cSBjorn Andersson 	ret = clk_bulk_enable(num_clks, clks);
9543c48d86cSBjorn Andersson 	if (ret)
9553c48d86cSBjorn Andersson 		clk_bulk_unprepare(num_clks, clks);
9563c48d86cSBjorn Andersson 
9573c48d86cSBjorn Andersson 	return ret;
9583c48d86cSBjorn Andersson }
9593c48d86cSBjorn Andersson 
9603c48d86cSBjorn Andersson static inline void clk_bulk_disable_unprepare(int num_clks,
961570aaec7SAndrey Smirnov 					      const struct clk_bulk_data *clks)
9623c48d86cSBjorn Andersson {
9633c48d86cSBjorn Andersson 	clk_bulk_disable(num_clks, clks);
9643c48d86cSBjorn Andersson 	clk_bulk_unprepare(num_clks, clks);
9653c48d86cSBjorn Andersson }
9663c48d86cSBjorn Andersson 
96760b8f0ddSPhil Edworthy /**
96860b8f0ddSPhil Edworthy  * clk_get_optional - lookup and obtain a reference to an optional clock
96960b8f0ddSPhil Edworthy  *		      producer.
97060b8f0ddSPhil Edworthy  * @dev: device for clock "consumer"
97160b8f0ddSPhil Edworthy  * @id: clock consumer ID
97260b8f0ddSPhil Edworthy  *
97360b8f0ddSPhil Edworthy  * Behaves the same as clk_get() except where there is no clock producer. In
97460b8f0ddSPhil Edworthy  * this case, instead of returning -ENOENT, the function returns NULL.
97560b8f0ddSPhil Edworthy  */
97660b8f0ddSPhil Edworthy static inline struct clk *clk_get_optional(struct device *dev, const char *id)
97760b8f0ddSPhil Edworthy {
97860b8f0ddSPhil Edworthy 	struct clk *clk = clk_get(dev, id);
97960b8f0ddSPhil Edworthy 
98060b8f0ddSPhil Edworthy 	if (clk == ERR_PTR(-ENOENT))
98160b8f0ddSPhil Edworthy 		return NULL;
98260b8f0ddSPhil Edworthy 
98360b8f0ddSPhil Edworthy 	return clk;
98460b8f0ddSPhil Edworthy }
98560b8f0ddSPhil Edworthy 
986137f8a72SRob Herring #if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK)
987766e6a4eSGrant Likely struct clk *of_clk_get(struct device_node *np, int index);
988766e6a4eSGrant Likely struct clk *of_clk_get_by_name(struct device_node *np, const char *name);
989766e6a4eSGrant Likely struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec);
990766e6a4eSGrant Likely #else
991766e6a4eSGrant Likely static inline struct clk *of_clk_get(struct device_node *np, int index)
992766e6a4eSGrant Likely {
9939f1612d3SShawn Guo 	return ERR_PTR(-ENOENT);
994766e6a4eSGrant Likely }
995766e6a4eSGrant Likely static inline struct clk *of_clk_get_by_name(struct device_node *np,
996766e6a4eSGrant Likely 					     const char *name)
997766e6a4eSGrant Likely {
9989f1612d3SShawn Guo 	return ERR_PTR(-ENOENT);
999766e6a4eSGrant Likely }
1000428c9de5SGeert Uytterhoeven static inline struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
1001428c9de5SGeert Uytterhoeven {
1002428c9de5SGeert Uytterhoeven 	return ERR_PTR(-ENOENT);
1003428c9de5SGeert Uytterhoeven }
1004766e6a4eSGrant Likely #endif
1005766e6a4eSGrant Likely 
1006f8ce2547SRussell King #endif
1007