xref: /linux/include/linux/clk.h (revision 502d45774af09f1c681c754c4b7cdfb5d7f72fd9)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  *  linux/include/linux/clk.h
4  *
5  *  Copyright (C) 2004 ARM Limited.
6  *  Written by Deep Blue Solutions Limited.
7  *  Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
8  */
9 #ifndef __LINUX_CLK_H
10 #define __LINUX_CLK_H
11 
12 #include <linux/err.h>
13 #include <linux/kernel.h>
14 #include <linux/notifier.h>
15 
16 struct device;
17 struct clk;
18 struct device_node;
19 struct of_phandle_args;
20 
21 /**
22  * DOC: clk notifier callback types
23  *
24  * PRE_RATE_CHANGE - called immediately before the clk rate is changed,
25  *     to indicate that the rate change will proceed.  Drivers must
26  *     immediately terminate any operations that will be affected by the
27  *     rate change.  Callbacks may either return NOTIFY_DONE, NOTIFY_OK,
28  *     NOTIFY_STOP or NOTIFY_BAD.
29  *
30  * ABORT_RATE_CHANGE: called if the rate change failed for some reason
31  *     after PRE_RATE_CHANGE.  In this case, all registered notifiers on
32  *     the clk will be called with ABORT_RATE_CHANGE. Callbacks must
33  *     always return NOTIFY_DONE or NOTIFY_OK.
34  *
35  * POST_RATE_CHANGE - called after the clk rate change has successfully
36  *     completed.  Callbacks must always return NOTIFY_DONE or NOTIFY_OK.
37  *
38  */
39 #define PRE_RATE_CHANGE			BIT(0)
40 #define POST_RATE_CHANGE		BIT(1)
41 #define ABORT_RATE_CHANGE		BIT(2)
42 
43 /**
44  * struct clk_notifier - associate a clk with a notifier
45  * @clk: struct clk * to associate the notifier with
46  * @notifier_head: a blocking_notifier_head for this clk
47  * @node: linked list pointers
48  *
49  * A list of struct clk_notifier is maintained by the notifier code.
50  * An entry is created whenever code registers the first notifier on a
51  * particular @clk.  Future notifiers on that @clk are added to the
52  * @notifier_head.
53  */
54 struct clk_notifier {
55 	struct clk			*clk;
56 	struct srcu_notifier_head	notifier_head;
57 	struct list_head		node;
58 };
59 
60 /**
61  * struct clk_notifier_data - rate data to pass to the notifier callback
62  * @clk: struct clk * being changed
63  * @old_rate: previous rate of this clk
64  * @new_rate: new rate of this clk
65  *
66  * For a pre-notifier, old_rate is the clk's rate before this rate
67  * change, and new_rate is what the rate will be in the future.  For a
68  * post-notifier, old_rate and new_rate are both set to the clk's
69  * current rate (this was done to optimize the implementation).
70  */
71 struct clk_notifier_data {
72 	struct clk		*clk;
73 	unsigned long		old_rate;
74 	unsigned long		new_rate;
75 };
76 
77 /**
78  * struct clk_bulk_data - Data used for bulk clk operations.
79  *
80  * @id: clock consumer ID
81  * @clk: struct clk * to store the associated clock
82  *
83  * The CLK APIs provide a series of clk_bulk_() API calls as
84  * a convenience to consumers which require multiple clks.  This
85  * structure is used to manage data for these calls.
86  */
87 struct clk_bulk_data {
88 	const char		*id;
89 	struct clk		*clk;
90 };
91 
92 #ifdef CONFIG_COMMON_CLK
93 
94 /**
95  * clk_notifier_register - register a clock rate-change notifier callback
96  * @clk: clock whose rate we are interested in
97  * @nb: notifier block with callback function pointer
98  *
99  * ProTip: debugging across notifier chains can be frustrating. Make sure that
100  * your notifier callback function prints a nice big warning in case of
101  * failure.
102  */
103 int clk_notifier_register(struct clk *clk, struct notifier_block *nb);
104 
105 /**
106  * clk_notifier_unregister - unregister a clock rate-change notifier callback
107  * @clk: clock whose rate we are no longer interested in
108  * @nb: notifier block which will be unregistered
109  */
110 int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb);
111 
112 /**
113  * devm_clk_notifier_register - register a managed rate-change notifier callback
114  * @dev: device for clock "consumer"
115  * @clk: clock whose rate we are interested in
116  * @nb: notifier block with callback function pointer
117  *
118  * Returns 0 on success, -EERROR otherwise
119  */
120 int devm_clk_notifier_register(struct device *dev, struct clk *clk,
121 			       struct notifier_block *nb);
122 
123 /**
124  * clk_get_accuracy - obtain the clock accuracy in ppb (parts per billion)
125  *		      for a clock source.
126  * @clk: clock source
127  *
128  * This gets the clock source accuracy expressed in ppb.
129  * A perfect clock returns 0.
130  */
131 long clk_get_accuracy(struct clk *clk);
132 
133 /**
134  * clk_set_phase - adjust the phase shift of a clock signal
135  * @clk: clock signal source
136  * @degrees: number of degrees the signal is shifted
137  *
138  * Shifts the phase of a clock signal by the specified degrees. Returns 0 on
139  * success, -EERROR otherwise.
140  */
141 int clk_set_phase(struct clk *clk, int degrees);
142 
143 /**
144  * clk_get_phase - return the phase shift of a clock signal
145  * @clk: clock signal source
146  *
147  * Returns the phase shift of a clock node in degrees, otherwise returns
148  * -EERROR.
149  */
150 int clk_get_phase(struct clk *clk);
151 
152 /**
153  * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
154  * @clk: clock signal source
155  * @num: numerator of the duty cycle ratio to be applied
156  * @den: denominator of the duty cycle ratio to be applied
157  *
158  * Adjust the duty cycle of a clock signal by the specified ratio. Returns 0 on
159  * success, -EERROR otherwise.
160  */
161 int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den);
162 
163 /**
164  * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal
165  * @clk: clock signal source
166  * @scale: scaling factor to be applied to represent the ratio as an integer
167  *
168  * Returns the duty cycle ratio multiplied by the scale provided, otherwise
169  * returns -EERROR.
170  */
171 int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale);
172 
173 /**
174  * clk_is_match - check if two clk's point to the same hardware clock
175  * @p: clk compared against q
176  * @q: clk compared against p
177  *
178  * Returns true if the two struct clk pointers both point to the same hardware
179  * clock node. Put differently, returns true if @p and @q
180  * share the same &struct clk_core object.
181  *
182  * Returns false otherwise. Note that two NULL clks are treated as matching.
183  */
184 bool clk_is_match(const struct clk *p, const struct clk *q);
185 
186 /**
187  * clk_rate_exclusive_get - get exclusivity over the rate control of a
188  *                          producer
189  * @clk: clock source
190  *
191  * This function allows drivers to get exclusive control over the rate of a
192  * provider. It prevents any other consumer to execute, even indirectly,
193  * opereation which could alter the rate of the provider or cause glitches
194  *
195  * If exlusivity is claimed more than once on clock, even by the same driver,
196  * the rate effectively gets locked as exclusivity can't be preempted.
197  *
198  * Must not be called from within atomic context.
199  *
200  * Returns success (0) or negative errno.
201  */
202 int clk_rate_exclusive_get(struct clk *clk);
203 
204 /**
205  * devm_clk_rate_exclusive_get - devm variant of clk_rate_exclusive_get
206  * @dev: device the exclusivity is bound to
207  * @clk: clock source
208  *
209  * Calls clk_rate_exclusive_get() on @clk and registers a devm cleanup handler
210  * on @dev to call clk_rate_exclusive_put().
211  *
212  * Must not be called from within atomic context.
213  */
214 int devm_clk_rate_exclusive_get(struct device *dev, struct clk *clk);
215 
216 /**
217  * clk_rate_exclusive_put - release exclusivity over the rate control of a
218  *                          producer
219  * @clk: clock source
220  *
221  * This function allows drivers to release the exclusivity it previously got
222  * from clk_rate_exclusive_get()
223  *
224  * The caller must balance the number of clk_rate_exclusive_get() and
225  * clk_rate_exclusive_put() calls.
226  *
227  * Must not be called from within atomic context.
228  */
229 void clk_rate_exclusive_put(struct clk *clk);
230 
231 /**
232  * clk_save_context - save clock context for poweroff
233  *
234  * Saves the context of the clock register for powerstates in which the
235  * contents of the registers will be lost. Occurs deep within the suspend
236  * code so locking is not necessary.
237  */
238 int clk_save_context(void);
239 
240 /**
241  * clk_restore_context - restore clock context after poweroff
242  *
243  * This occurs with all clocks enabled. Occurs deep within the resume code
244  * so locking is not necessary.
245  */
246 void clk_restore_context(void);
247 
248 #else /* !CONFIG_COMMON_CLK */
249 
clk_notifier_register(struct clk * clk,struct notifier_block * nb)250 static inline int clk_notifier_register(struct clk *clk,
251 					struct notifier_block *nb)
252 {
253 	return -ENOTSUPP;
254 }
255 
clk_notifier_unregister(struct clk * clk,struct notifier_block * nb)256 static inline int clk_notifier_unregister(struct clk *clk,
257 					  struct notifier_block *nb)
258 {
259 	return -ENOTSUPP;
260 }
261 
devm_clk_notifier_register(struct device * dev,struct clk * clk,struct notifier_block * nb)262 static inline int devm_clk_notifier_register(struct device *dev,
263 					     struct clk *clk,
264 					     struct notifier_block *nb)
265 {
266 	return -ENOTSUPP;
267 }
268 
clk_get_accuracy(struct clk * clk)269 static inline long clk_get_accuracy(struct clk *clk)
270 {
271 	return -ENOTSUPP;
272 }
273 
clk_set_phase(struct clk * clk,int phase)274 static inline long clk_set_phase(struct clk *clk, int phase)
275 {
276 	return -ENOTSUPP;
277 }
278 
clk_get_phase(struct clk * clk)279 static inline long clk_get_phase(struct clk *clk)
280 {
281 	return -ENOTSUPP;
282 }
283 
clk_set_duty_cycle(struct clk * clk,unsigned int num,unsigned int den)284 static inline int clk_set_duty_cycle(struct clk *clk, unsigned int num,
285 				     unsigned int den)
286 {
287 	return -ENOTSUPP;
288 }
289 
clk_get_scaled_duty_cycle(struct clk * clk,unsigned int scale)290 static inline unsigned int clk_get_scaled_duty_cycle(struct clk *clk,
291 						     unsigned int scale)
292 {
293 	return 0;
294 }
295 
clk_is_match(const struct clk * p,const struct clk * q)296 static inline bool clk_is_match(const struct clk *p, const struct clk *q)
297 {
298 	return p == q;
299 }
300 
clk_rate_exclusive_get(struct clk * clk)301 static inline int clk_rate_exclusive_get(struct clk *clk)
302 {
303 	return 0;
304 }
305 
devm_clk_rate_exclusive_get(struct device * dev,struct clk * clk)306 static inline int devm_clk_rate_exclusive_get(struct device *dev, struct clk *clk)
307 {
308 	return 0;
309 }
310 
clk_rate_exclusive_put(struct clk * clk)311 static inline void clk_rate_exclusive_put(struct clk *clk) {}
312 
clk_save_context(void)313 static inline int clk_save_context(void)
314 {
315 	return 0;
316 }
317 
clk_restore_context(void)318 static inline void clk_restore_context(void) {}
319 
320 #endif /* !CONFIG_COMMON_CLK */
321 
322 #ifdef CONFIG_HAVE_CLK_PREPARE
323 /**
324  * clk_prepare - prepare a clock source
325  * @clk: clock source
326  *
327  * This prepares the clock source for use.
328  *
329  * Must not be called from within atomic context.
330  */
331 int clk_prepare(struct clk *clk);
332 
333 /**
334  * clk_unprepare - undo preparation of a clock source
335  * @clk: clock source
336  *
337  * This undoes a previously prepared clock.  The caller must balance
338  * the number of prepare and unprepare calls.
339  *
340  * Must not be called from within atomic context.
341  */
342 void clk_unprepare(struct clk *clk);
343 
344 int __must_check clk_bulk_prepare(int num_clks,
345 				  const struct clk_bulk_data *clks);
346 void clk_bulk_unprepare(int num_clks, const struct clk_bulk_data *clks);
347 
348 /**
349  * clk_is_enabled_when_prepared - indicate if preparing a clock also enables it.
350  * @clk: clock source
351  *
352  * Returns true if clk_prepare() implicitly enables the clock, effectively
353  * making clk_enable()/clk_disable() no-ops, false otherwise.
354  *
355  * This is of interest mainly to the power management code where actually
356  * disabling the clock also requires unpreparing it to have any material
357  * effect.
358  *
359  * Regardless of the value returned here, the caller must always invoke
360  * clk_enable() or clk_prepare_enable()  and counterparts for usage counts
361  * to be right.
362  */
363 bool clk_is_enabled_when_prepared(struct clk *clk);
364 #else /* !CONFIG_HAVE_CLK_PREPARE */
clk_prepare(struct clk * clk)365 static inline int clk_prepare(struct clk *clk)
366 {
367 	might_sleep();
368 	return 0;
369 }
370 
clk_unprepare(struct clk * clk)371 static inline void clk_unprepare(struct clk *clk)
372 {
373 	might_sleep();
374 }
375 
376 static inline int __must_check
clk_bulk_prepare(int num_clks,const struct clk_bulk_data * clks)377 clk_bulk_prepare(int num_clks, const struct clk_bulk_data *clks)
378 {
379 	might_sleep();
380 	return 0;
381 }
382 
clk_bulk_unprepare(int num_clks,const struct clk_bulk_data * clks)383 static inline void clk_bulk_unprepare(int num_clks,
384 				      const struct clk_bulk_data *clks)
385 {
386 	might_sleep();
387 }
388 
clk_is_enabled_when_prepared(struct clk * clk)389 static inline bool clk_is_enabled_when_prepared(struct clk *clk)
390 {
391 	return false;
392 }
393 #endif /* !CONFIG_HAVE_CLK_PREPARE */
394 
395 #ifdef CONFIG_HAVE_CLK
396 /**
397  * clk_get - lookup and obtain a reference to a clock producer.
398  * @dev: device for clock "consumer"
399  * @id: clock consumer ID
400  *
401  * Returns a struct clk corresponding to the clock producer, or
402  * valid IS_ERR() condition containing errno.  The implementation
403  * uses @dev and @id to determine the clock consumer, and thereby
404  * the clock producer.  (IOW, @id may be identical strings, but
405  * clk_get may return different clock producers depending on @dev.)
406  *
407  * Drivers must assume that the clock source is not enabled.
408  *
409  * clk_get should not be called from within interrupt context.
410  */
411 struct clk *clk_get(struct device *dev, const char *id);
412 
413 /**
414  * clk_bulk_get - lookup and obtain a number of references to clock producer.
415  * @dev: device for clock "consumer"
416  * @num_clks: the number of clk_bulk_data
417  * @clks: the clk_bulk_data table of consumer
418  *
419  * This helper function allows drivers to get several clk consumers in one
420  * operation. If any of the clk cannot be acquired then any clks
421  * that were obtained will be freed before returning to the caller.
422  *
423  * Returns 0 if all clocks specified in clk_bulk_data table are obtained
424  * successfully, or valid IS_ERR() condition containing errno.
425  * The implementation uses @dev and @clk_bulk_data.id to determine the
426  * clock consumer, and thereby the clock producer.
427  * The clock returned is stored in each @clk_bulk_data.clk field.
428  *
429  * Drivers must assume that the clock source is not enabled.
430  *
431  * clk_bulk_get should not be called from within interrupt context.
432  */
433 int __must_check clk_bulk_get(struct device *dev, int num_clks,
434 			      struct clk_bulk_data *clks);
435 /**
436  * clk_bulk_get_all - lookup and obtain all available references to clock
437  *		      producer.
438  * @dev: device for clock "consumer"
439  * @clks: pointer to the clk_bulk_data table of consumer
440  *
441  * This helper function allows drivers to get all clk consumers in one
442  * operation. If any of the clk cannot be acquired then any clks
443  * that were obtained will be freed before returning to the caller.
444  *
445  * Returns a positive value for the number of clocks obtained while the
446  * clock references are stored in the clk_bulk_data table in @clks field.
447  * Returns 0 if there're none and a negative value if something failed.
448  *
449  * Drivers must assume that the clock source is not enabled.
450  *
451  * clk_bulk_get should not be called from within interrupt context.
452  */
453 int __must_check clk_bulk_get_all(struct device *dev,
454 				  struct clk_bulk_data **clks);
455 
456 /**
457  * clk_bulk_get_optional - lookup and obtain a number of references to clock producer
458  * @dev: device for clock "consumer"
459  * @num_clks: the number of clk_bulk_data
460  * @clks: the clk_bulk_data table of consumer
461  *
462  * Behaves the same as clk_bulk_get() except where there is no clock producer.
463  * In this case, instead of returning -ENOENT, the function returns 0 and
464  * NULL for a clk for which a clock producer could not be determined.
465  */
466 int __must_check clk_bulk_get_optional(struct device *dev, int num_clks,
467 				       struct clk_bulk_data *clks);
468 /**
469  * devm_clk_bulk_get - managed get multiple clk consumers
470  * @dev: device for clock "consumer"
471  * @num_clks: the number of clk_bulk_data
472  * @clks: the clk_bulk_data table of consumer
473  *
474  * Return 0 on success, an errno on failure.
475  *
476  * This helper function allows drivers to get several clk
477  * consumers in one operation with management, the clks will
478  * automatically be freed when the device is unbound.
479  */
480 int __must_check devm_clk_bulk_get(struct device *dev, int num_clks,
481 				   struct clk_bulk_data *clks);
482 /**
483  * devm_clk_bulk_get_optional - managed get multiple optional consumer clocks
484  * @dev: device for clock "consumer"
485  * @num_clks: the number of clk_bulk_data
486  * @clks: pointer to the clk_bulk_data table of consumer
487  *
488  * Behaves the same as devm_clk_bulk_get() except where there is no clock
489  * producer.  In this case, instead of returning -ENOENT, the function returns
490  * NULL for given clk. It is assumed all clocks in clk_bulk_data are optional.
491  *
492  * Returns 0 if all clocks specified in clk_bulk_data table are obtained
493  * successfully or for any clk there was no clk provider available, otherwise
494  * returns valid IS_ERR() condition containing errno.
495  * The implementation uses @dev and @clk_bulk_data.id to determine the
496  * clock consumer, and thereby the clock producer.
497  * The clock returned is stored in each @clk_bulk_data.clk field.
498  *
499  * Drivers must assume that the clock source is not enabled.
500  *
501  * clk_bulk_get should not be called from within interrupt context.
502  */
503 int __must_check devm_clk_bulk_get_optional(struct device *dev, int num_clks,
504 					    struct clk_bulk_data *clks);
505 /**
506  * devm_clk_bulk_get_enable - Get and enable bulk clocks (managed)
507  * @dev: device for clock "consumer"
508  * @num_clks: the number of clk_bulk_data
509  * @clks: pointer to the clk_bulk_data table of consumer
510  *
511  * Behaves the same as devm_clk_bulk_get() but also prepares and enables the
512  * clocks in one operation with management. The clks will automatically be
513  * disabled, unprepared and freed when the device is unbound.
514  *
515  * Return: 0 if all clocks specified in clk_bulk_data table are obtained and
516  * enabled successfully. Otherwise returns valid IS_ERR() condition containing
517  * errno.
518  */
519 int __must_check devm_clk_bulk_get_enable(struct device *dev, int num_clks,
520 					  struct clk_bulk_data *clks);
521 /**
522  * devm_clk_bulk_get_optional_enable - Get and enable optional bulk clocks (managed)
523  * @dev: device for clock "consumer"
524  * @num_clks: the number of clk_bulk_data
525  * @clks: pointer to the clk_bulk_data table of consumer
526  *
527  * Behaves the same as devm_clk_bulk_get_optional() but also prepares and enables
528  * the clocks in one operation with management. The clks will automatically be
529  * disabled, unprepared and freed when the device is unbound.
530  *
531  * Return: 0 if all clocks specified in clk_bulk_data table are obtained
532  * and enabled successfully, or for any clk there was no clk provider available.
533  * Otherwise returns valid IS_ERR() condition containing errno.
534  */
535 int __must_check devm_clk_bulk_get_optional_enable(struct device *dev, int num_clks,
536 						   struct clk_bulk_data *clks);
537 /**
538  * devm_clk_bulk_get_all - managed get multiple clk consumers
539  * @dev: device for clock "consumer"
540  * @clks: pointer to the clk_bulk_data table of consumer
541  *
542  * Returns a positive value for the number of clocks obtained while the
543  * clock references are stored in the clk_bulk_data table in @clks field.
544  * Returns 0 if there're none and a negative value if something failed.
545  *
546  * This helper function allows drivers to get several clk
547  * consumers in one operation with management, the clks will
548  * automatically be freed when the device is unbound.
549  */
550 
551 int __must_check devm_clk_bulk_get_all(struct device *dev,
552 				       struct clk_bulk_data **clks);
553 
554 /**
555  * devm_clk_bulk_get_all_enabled - Get and enable all clocks of the consumer (managed)
556  * @dev: device for clock "consumer"
557  * @clks: pointer to the clk_bulk_data table of consumer
558  *
559  * Returns a positive value for the number of clocks obtained while the
560  * clock references are stored in the clk_bulk_data table in @clks field.
561  * Returns 0 if there're none and a negative value if something failed.
562  *
563  * This helper function allows drivers to get all clocks of the
564  * consumer and enables them in one operation with management.
565  * The clks will automatically be disabled and freed when the device
566  * is unbound.
567  */
568 
569 int __must_check devm_clk_bulk_get_all_enabled(struct device *dev,
570 					       struct clk_bulk_data **clks);
571 
572 /**
573  * devm_clk_get - lookup and obtain a managed reference to a clock producer.
574  * @dev: device for clock "consumer"
575  * @id: clock consumer ID
576  *
577  * Context: May sleep.
578  *
579  * Return: a struct clk corresponding to the clock producer, or
580  * valid IS_ERR() condition containing errno.  The implementation
581  * uses @dev and @id to determine the clock consumer, and thereby
582  * the clock producer.  (IOW, @id may be identical strings, but
583  * clk_get may return different clock producers depending on @dev.)
584  *
585  * Drivers must assume that the clock source is neither prepared nor
586  * enabled.
587  *
588  * The clock will automatically be freed when the device is unbound
589  * from the bus.
590  */
591 struct clk *devm_clk_get(struct device *dev, const char *id);
592 
593 /**
594  * devm_clk_get_prepared - devm_clk_get() + clk_prepare()
595  * @dev: device for clock "consumer"
596  * @id: clock consumer ID
597  *
598  * Context: May sleep.
599  *
600  * Return: a struct clk corresponding to the clock producer, or
601  * valid IS_ERR() condition containing errno.  The implementation
602  * uses @dev and @id to determine the clock consumer, and thereby
603  * the clock producer.  (IOW, @id may be identical strings, but
604  * clk_get may return different clock producers depending on @dev.)
605  *
606  * The returned clk (if valid) is prepared. Drivers must however assume
607  * that the clock is not enabled.
608  *
609  * The clock will automatically be unprepared and freed when the device
610  * is unbound from the bus.
611  */
612 struct clk *devm_clk_get_prepared(struct device *dev, const char *id);
613 
614 /**
615  * devm_clk_get_enabled - devm_clk_get() + clk_prepare_enable()
616  * @dev: device for clock "consumer"
617  * @id: clock consumer ID
618  *
619  * Context: May sleep.
620  *
621  * Return: a struct clk corresponding to the clock producer, or
622  * valid IS_ERR() condition containing errno.  The implementation
623  * uses @dev and @id to determine the clock consumer, and thereby
624  * the clock producer.  (IOW, @id may be identical strings, but
625  * clk_get may return different clock producers depending on @dev.)
626  *
627  * The returned clk (if valid) is prepared and enabled.
628  *
629  * The clock will automatically be disabled, unprepared and freed
630  * when the device is unbound from the bus.
631  */
632 struct clk *devm_clk_get_enabled(struct device *dev, const char *id);
633 
634 /**
635  * devm_clk_get_optional - lookup and obtain a managed reference to an optional
636  *			   clock producer.
637  * @dev: device for clock "consumer"
638  * @id: clock consumer ID
639  *
640  * Context: May sleep.
641  *
642  * Return: a struct clk corresponding to the clock producer, or
643  * valid IS_ERR() condition containing errno.  The implementation
644  * uses @dev and @id to determine the clock consumer, and thereby
645  * the clock producer.  If no such clk is found, it returns NULL
646  * which serves as a dummy clk.  That's the only difference compared
647  * to devm_clk_get().
648  *
649  * Drivers must assume that the clock source is neither prepared nor
650  * enabled.
651  *
652  * The clock will automatically be freed when the device is unbound
653  * from the bus.
654  */
655 struct clk *devm_clk_get_optional(struct device *dev, const char *id);
656 
657 /**
658  * devm_clk_get_optional_prepared - devm_clk_get_optional() + clk_prepare()
659  * @dev: device for clock "consumer"
660  * @id: clock consumer ID
661  *
662  * Context: May sleep.
663  *
664  * Return: a struct clk corresponding to the clock producer, or
665  * valid IS_ERR() condition containing errno.  The implementation
666  * uses @dev and @id to determine the clock consumer, and thereby
667  * the clock producer.  If no such clk is found, it returns NULL
668  * which serves as a dummy clk.  That's the only difference compared
669  * to devm_clk_get_prepared().
670  *
671  * The returned clk (if valid) is prepared. Drivers must however
672  * assume that the clock is not enabled.
673  *
674  * The clock will automatically be unprepared and freed when the
675  * device is unbound from the bus.
676  */
677 struct clk *devm_clk_get_optional_prepared(struct device *dev, const char *id);
678 
679 /**
680  * devm_clk_get_optional_enabled - devm_clk_get_optional() +
681  *                                 clk_prepare_enable()
682  * @dev: device for clock "consumer"
683  * @id: clock consumer ID
684  *
685  * Context: May sleep.
686  *
687  * Return: a struct clk corresponding to the clock producer, or
688  * valid IS_ERR() condition containing errno.  The implementation
689  * uses @dev and @id to determine the clock consumer, and thereby
690  * the clock producer.  If no such clk is found, it returns NULL
691  * which serves as a dummy clk.  That's the only difference compared
692  * to devm_clk_get_enabled().
693  *
694  * The returned clk (if valid) is prepared and enabled.
695  *
696  * The clock will automatically be disabled, unprepared and freed
697  * when the device is unbound from the bus.
698  */
699 struct clk *devm_clk_get_optional_enabled(struct device *dev, const char *id);
700 
701 /**
702  * devm_clk_get_optional_enabled_with_rate - devm_clk_get_optional() +
703  *                                           clk_set_rate() +
704  *                                           clk_prepare_enable()
705  * @dev: device for clock "consumer"
706  * @id: clock consumer ID
707  * @rate: new clock rate
708  *
709  * Context: May sleep.
710  *
711  * Return: a struct clk corresponding to the clock producer, or
712  * valid IS_ERR() condition containing errno.  The implementation
713  * uses @dev and @id to determine the clock consumer, and thereby
714  * the clock producer.  If no such clk is found, it returns NULL
715  * which serves as a dummy clk.  That's the only difference compared
716  * to devm_clk_get_enabled().
717  *
718  * The returned clk (if valid) is prepared and enabled and rate was set.
719  *
720  * The clock will automatically be disabled, unprepared and freed
721  * when the device is unbound from the bus.
722  */
723 struct clk *devm_clk_get_optional_enabled_with_rate(struct device *dev,
724 						    const char *id,
725 						    unsigned long rate);
726 
727 /**
728  * devm_get_clk_from_child - lookup and obtain a managed reference to a
729  *			     clock producer from child node.
730  * @dev: device for clock "consumer"
731  * @np: pointer to clock consumer node
732  * @con_id: clock consumer ID
733  *
734  * This function parses the clocks, and uses them to look up the
735  * struct clk from the registered list of clock providers by using
736  * @np and @con_id
737  *
738  * The clock will automatically be freed when the device is unbound
739  * from the bus.
740  */
741 struct clk *devm_get_clk_from_child(struct device *dev,
742 				    struct device_node *np, const char *con_id);
743 
744 /**
745  * clk_enable - inform the system when the clock source should be running.
746  * @clk: clock source
747  *
748  * If the clock can not be enabled/disabled, this should return success.
749  *
750  * May be called from atomic contexts.
751  *
752  * Returns success (0) or negative errno.
753  */
754 int clk_enable(struct clk *clk);
755 
756 /**
757  * clk_bulk_enable - inform the system when the set of clks should be running.
758  * @num_clks: the number of clk_bulk_data
759  * @clks: the clk_bulk_data table of consumer
760  *
761  * May be called from atomic contexts.
762  *
763  * Returns success (0) or negative errno.
764  */
765 int __must_check clk_bulk_enable(int num_clks,
766 				 const struct clk_bulk_data *clks);
767 
768 /**
769  * clk_disable - inform the system when the clock source is no longer required.
770  * @clk: clock source
771  *
772  * Inform the system that a clock source is no longer required by
773  * a driver and may be shut down.
774  *
775  * May be called from atomic contexts.
776  *
777  * Implementation detail: if the clock source is shared between
778  * multiple drivers, clk_enable() calls must be balanced by the
779  * same number of clk_disable() calls for the clock source to be
780  * disabled.
781  */
782 void clk_disable(struct clk *clk);
783 
784 /**
785  * clk_bulk_disable - inform the system when the set of clks is no
786  *		      longer required.
787  * @num_clks: the number of clk_bulk_data
788  * @clks: the clk_bulk_data table of consumer
789  *
790  * Inform the system that a set of clks is no longer required by
791  * a driver and may be shut down.
792  *
793  * May be called from atomic contexts.
794  *
795  * Implementation detail: if the set of clks is shared between
796  * multiple drivers, clk_bulk_enable() calls must be balanced by the
797  * same number of clk_bulk_disable() calls for the clock source to be
798  * disabled.
799  */
800 void clk_bulk_disable(int num_clks, const struct clk_bulk_data *clks);
801 
802 /**
803  * clk_get_rate - obtain the current clock rate (in Hz) for a clock source.
804  *		  This is only valid once the clock source has been enabled.
805  * @clk: clock source
806  */
807 unsigned long clk_get_rate(struct clk *clk);
808 
809 /**
810  * clk_put	- "free" the clock source
811  * @clk: clock source
812  *
813  * Note: drivers must ensure that all clk_enable calls made on this
814  * clock source are balanced by clk_disable calls prior to calling
815  * this function.
816  *
817  * clk_put should not be called from within interrupt context.
818  */
819 void clk_put(struct clk *clk);
820 
821 /**
822  * clk_bulk_put	- "free" the clock source
823  * @num_clks: the number of clk_bulk_data
824  * @clks: the clk_bulk_data table of consumer
825  *
826  * Note: drivers must ensure that all clk_bulk_enable calls made on this
827  * clock source are balanced by clk_bulk_disable calls prior to calling
828  * this function.
829  *
830  * clk_bulk_put should not be called from within interrupt context.
831  */
832 void clk_bulk_put(int num_clks, struct clk_bulk_data *clks);
833 
834 /**
835  * clk_bulk_put_all - "free" all the clock source
836  * @num_clks: the number of clk_bulk_data
837  * @clks: the clk_bulk_data table of consumer
838  *
839  * Note: drivers must ensure that all clk_bulk_enable calls made on this
840  * clock source are balanced by clk_bulk_disable calls prior to calling
841  * this function.
842  *
843  * clk_bulk_put_all should not be called from within interrupt context.
844  */
845 void clk_bulk_put_all(int num_clks, struct clk_bulk_data *clks);
846 
847 /**
848  * devm_clk_put	- "free" a managed clock source
849  * @dev: device used to acquire the clock
850  * @clk: clock source acquired with devm_clk_get()
851  *
852  * Note: drivers must ensure that all clk_enable calls made on this
853  * clock source are balanced by clk_disable calls prior to calling
854  * this function.
855  *
856  * clk_put should not be called from within interrupt context.
857  */
858 void devm_clk_put(struct device *dev, struct clk *clk);
859 
860 /*
861  * The remaining APIs are optional for machine class support.
862  */
863 
864 
865 /**
866  * clk_round_rate - adjust a rate to the exact rate a clock can provide
867  * @clk: clock source
868  * @rate: desired clock rate in Hz
869  *
870  * This answers the question "if I were to pass @rate to clk_set_rate(),
871  * what clock rate would I end up with?" without changing the hardware
872  * in any way.  In other words:
873  *
874  *   rate = clk_round_rate(clk, r);
875  *
876  * and:
877  *
878  *   clk_set_rate(clk, r);
879  *   rate = clk_get_rate(clk);
880  *
881  * are equivalent except the former does not modify the clock hardware
882  * in any way.
883  *
884  * Returns rounded clock rate in Hz, or negative errno.
885  */
886 long clk_round_rate(struct clk *clk, unsigned long rate);
887 
888 /**
889  * clk_set_rate - set the clock rate for a clock source
890  * @clk: clock source
891  * @rate: desired clock rate in Hz
892  *
893  * Updating the rate starts at the top-most affected clock and then
894  * walks the tree down to the bottom-most clock that needs updating.
895  *
896  * Returns success (0) or negative errno.
897  */
898 int clk_set_rate(struct clk *clk, unsigned long rate);
899 
900 /**
901  * clk_set_rate_exclusive- set the clock rate and claim exclusivity over
902  *                         clock source
903  * @clk: clock source
904  * @rate: desired clock rate in Hz
905  *
906  * This helper function allows drivers to atomically set the rate of a producer
907  * and claim exclusivity over the rate control of the producer.
908  *
909  * It is essentially a combination of clk_set_rate() and
910  * clk_rate_exclusite_get(). Caller must balance this call with a call to
911  * clk_rate_exclusive_put()
912  *
913  * Returns success (0) or negative errno.
914  */
915 int clk_set_rate_exclusive(struct clk *clk, unsigned long rate);
916 
917 /**
918  * clk_has_parent - check if a clock is a possible parent for another
919  * @clk: clock source
920  * @parent: parent clock source
921  *
922  * This function can be used in drivers that need to check that a clock can be
923  * the parent of another without actually changing the parent.
924  *
925  * Returns true if @parent is a possible parent for @clk, false otherwise.
926  */
927 bool clk_has_parent(const struct clk *clk, const struct clk *parent);
928 
929 /**
930  * clk_set_rate_range - set a rate range for a clock source
931  * @clk: clock source
932  * @min: desired minimum clock rate in Hz, inclusive
933  * @max: desired maximum clock rate in Hz, inclusive
934  *
935  * Returns success (0) or negative errno.
936  */
937 int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max);
938 
939 /**
940  * clk_set_min_rate - set a minimum clock rate for a clock source
941  * @clk: clock source
942  * @rate: desired minimum clock rate in Hz, inclusive
943  *
944  * Returns success (0) or negative errno.
945  */
946 int clk_set_min_rate(struct clk *clk, unsigned long rate);
947 
948 /**
949  * clk_set_max_rate - set a maximum clock rate for a clock source
950  * @clk: clock source
951  * @rate: desired maximum clock rate in Hz, inclusive
952  *
953  * Returns success (0) or negative errno.
954  */
955 int clk_set_max_rate(struct clk *clk, unsigned long rate);
956 
957 /**
958  * clk_set_parent - set the parent clock source for this clock
959  * @clk: clock source
960  * @parent: parent clock source
961  *
962  * Returns success (0) or negative errno.
963  */
964 int clk_set_parent(struct clk *clk, struct clk *parent);
965 
966 /**
967  * clk_get_parent - get the parent clock source for this clock
968  * @clk: clock source
969  *
970  * Returns struct clk corresponding to parent clock source, or NULL
971  * if clk is NULL.
972  */
973 struct clk *clk_get_parent(struct clk *clk);
974 
975 /**
976  * clk_get_sys - get a clock based upon the device name
977  * @dev_id: device name
978  * @con_id: connection ID
979  *
980  * Returns a struct clk corresponding to the clock producer, or
981  * valid IS_ERR() condition containing errno.  The implementation
982  * uses @dev_id and @con_id to determine the clock consumer, and
983  * thereby the clock producer. In contrast to clk_get() this function
984  * takes the device name instead of the device itself for identification.
985  *
986  * Drivers must assume that the clock source is not enabled.
987  *
988  * clk_get_sys should not be called from within interrupt context.
989  */
990 struct clk *clk_get_sys(const char *dev_id, const char *con_id);
991 
992 #else /* !CONFIG_HAVE_CLK */
993 
clk_get(struct device * dev,const char * id)994 static inline struct clk *clk_get(struct device *dev, const char *id)
995 {
996 	return NULL;
997 }
998 
clk_bulk_get(struct device * dev,int num_clks,struct clk_bulk_data * clks)999 static inline int __must_check clk_bulk_get(struct device *dev, int num_clks,
1000 					    struct clk_bulk_data *clks)
1001 {
1002 	return 0;
1003 }
1004 
clk_bulk_get_optional(struct device * dev,int num_clks,struct clk_bulk_data * clks)1005 static inline int __must_check clk_bulk_get_optional(struct device *dev,
1006 				int num_clks, struct clk_bulk_data *clks)
1007 {
1008 	return 0;
1009 }
1010 
clk_bulk_get_all(struct device * dev,struct clk_bulk_data ** clks)1011 static inline int __must_check clk_bulk_get_all(struct device *dev,
1012 					 struct clk_bulk_data **clks)
1013 {
1014 	return 0;
1015 }
1016 
devm_clk_get(struct device * dev,const char * id)1017 static inline struct clk *devm_clk_get(struct device *dev, const char *id)
1018 {
1019 	return NULL;
1020 }
1021 
devm_clk_get_prepared(struct device * dev,const char * id)1022 static inline struct clk *devm_clk_get_prepared(struct device *dev,
1023 						const char *id)
1024 {
1025 	return NULL;
1026 }
1027 
devm_clk_get_enabled(struct device * dev,const char * id)1028 static inline struct clk *devm_clk_get_enabled(struct device *dev,
1029 					       const char *id)
1030 {
1031 	return NULL;
1032 }
1033 
devm_clk_get_optional(struct device * dev,const char * id)1034 static inline struct clk *devm_clk_get_optional(struct device *dev,
1035 						const char *id)
1036 {
1037 	return NULL;
1038 }
1039 
devm_clk_get_optional_prepared(struct device * dev,const char * id)1040 static inline struct clk *devm_clk_get_optional_prepared(struct device *dev,
1041 							 const char *id)
1042 {
1043 	return NULL;
1044 }
1045 
devm_clk_get_optional_enabled(struct device * dev,const char * id)1046 static inline struct clk *devm_clk_get_optional_enabled(struct device *dev,
1047 							const char *id)
1048 {
1049 	return NULL;
1050 }
1051 
1052 static inline struct clk *
devm_clk_get_optional_enabled_with_rate(struct device * dev,const char * id,unsigned long rate)1053 devm_clk_get_optional_enabled_with_rate(struct device *dev, const char *id,
1054 					unsigned long rate)
1055 {
1056 	return NULL;
1057 }
1058 
devm_clk_bulk_get(struct device * dev,int num_clks,struct clk_bulk_data * clks)1059 static inline int __must_check devm_clk_bulk_get(struct device *dev, int num_clks,
1060 						 struct clk_bulk_data *clks)
1061 {
1062 	return 0;
1063 }
1064 
devm_clk_bulk_get_optional(struct device * dev,int num_clks,struct clk_bulk_data * clks)1065 static inline int __must_check devm_clk_bulk_get_optional(struct device *dev,
1066 				int num_clks, struct clk_bulk_data *clks)
1067 {
1068 	return 0;
1069 }
1070 
devm_clk_bulk_get_enable(struct device * dev,int num_clks,struct clk_bulk_data * clks)1071 static inline int __must_check devm_clk_bulk_get_enable(struct device *dev,
1072 							int num_clks,
1073 							struct clk_bulk_data *clks)
1074 {
1075 	return 0;
1076 }
1077 
devm_clk_bulk_get_optional_enable(struct device * dev,int num_clks,struct clk_bulk_data * clks)1078 static inline int __must_check devm_clk_bulk_get_optional_enable(struct device *dev,
1079 								 int num_clks,
1080 								 struct clk_bulk_data *clks)
1081 {
1082 	return 0;
1083 }
1084 
devm_clk_bulk_get_all(struct device * dev,struct clk_bulk_data ** clks)1085 static inline int __must_check devm_clk_bulk_get_all(struct device *dev,
1086 						     struct clk_bulk_data **clks)
1087 {
1088 
1089 	return 0;
1090 }
1091 
devm_clk_bulk_get_all_enabled(struct device * dev,struct clk_bulk_data ** clks)1092 static inline int __must_check devm_clk_bulk_get_all_enabled(struct device *dev,
1093 						struct clk_bulk_data **clks)
1094 {
1095 	return 0;
1096 }
1097 
devm_get_clk_from_child(struct device * dev,struct device_node * np,const char * con_id)1098 static inline struct clk *devm_get_clk_from_child(struct device *dev,
1099 				struct device_node *np, const char *con_id)
1100 {
1101 	return NULL;
1102 }
1103 
clk_put(struct clk * clk)1104 static inline void clk_put(struct clk *clk) {}
1105 
clk_bulk_put(int num_clks,struct clk_bulk_data * clks)1106 static inline void clk_bulk_put(int num_clks, struct clk_bulk_data *clks) {}
1107 
clk_bulk_put_all(int num_clks,struct clk_bulk_data * clks)1108 static inline void clk_bulk_put_all(int num_clks, struct clk_bulk_data *clks) {}
1109 
devm_clk_put(struct device * dev,struct clk * clk)1110 static inline void devm_clk_put(struct device *dev, struct clk *clk) {}
1111 
clk_enable(struct clk * clk)1112 static inline int clk_enable(struct clk *clk)
1113 {
1114 	return 0;
1115 }
1116 
clk_bulk_enable(int num_clks,const struct clk_bulk_data * clks)1117 static inline int __must_check clk_bulk_enable(int num_clks,
1118 					       const struct clk_bulk_data *clks)
1119 {
1120 	return 0;
1121 }
1122 
clk_disable(struct clk * clk)1123 static inline void clk_disable(struct clk *clk) {}
1124 
1125 
clk_bulk_disable(int num_clks,const struct clk_bulk_data * clks)1126 static inline void clk_bulk_disable(int num_clks,
1127 				    const struct clk_bulk_data *clks) {}
1128 
clk_get_rate(struct clk * clk)1129 static inline unsigned long clk_get_rate(struct clk *clk)
1130 {
1131 	return 0;
1132 }
1133 
clk_set_rate(struct clk * clk,unsigned long rate)1134 static inline int clk_set_rate(struct clk *clk, unsigned long rate)
1135 {
1136 	return 0;
1137 }
1138 
clk_set_rate_exclusive(struct clk * clk,unsigned long rate)1139 static inline int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
1140 {
1141 	return 0;
1142 }
1143 
clk_round_rate(struct clk * clk,unsigned long rate)1144 static inline long clk_round_rate(struct clk *clk, unsigned long rate)
1145 {
1146 	return 0;
1147 }
1148 
clk_has_parent(struct clk * clk,struct clk * parent)1149 static inline bool clk_has_parent(struct clk *clk, struct clk *parent)
1150 {
1151 	return true;
1152 }
1153 
clk_set_rate_range(struct clk * clk,unsigned long min,unsigned long max)1154 static inline int clk_set_rate_range(struct clk *clk, unsigned long min,
1155 				     unsigned long max)
1156 {
1157 	return 0;
1158 }
1159 
clk_set_min_rate(struct clk * clk,unsigned long rate)1160 static inline int clk_set_min_rate(struct clk *clk, unsigned long rate)
1161 {
1162 	return 0;
1163 }
1164 
clk_set_max_rate(struct clk * clk,unsigned long rate)1165 static inline int clk_set_max_rate(struct clk *clk, unsigned long rate)
1166 {
1167 	return 0;
1168 }
1169 
clk_set_parent(struct clk * clk,struct clk * parent)1170 static inline int clk_set_parent(struct clk *clk, struct clk *parent)
1171 {
1172 	return 0;
1173 }
1174 
clk_get_parent(struct clk * clk)1175 static inline struct clk *clk_get_parent(struct clk *clk)
1176 {
1177 	return NULL;
1178 }
1179 
clk_get_sys(const char * dev_id,const char * con_id)1180 static inline struct clk *clk_get_sys(const char *dev_id, const char *con_id)
1181 {
1182 	return NULL;
1183 }
1184 
1185 #endif /* !CONFIG_HAVE_CLK */
1186 
1187 /* clk_prepare_enable helps cases using clk_enable in non-atomic context. */
clk_prepare_enable(struct clk * clk)1188 static inline int clk_prepare_enable(struct clk *clk)
1189 {
1190 	int ret;
1191 
1192 	ret = clk_prepare(clk);
1193 	if (ret)
1194 		return ret;
1195 	ret = clk_enable(clk);
1196 	if (ret)
1197 		clk_unprepare(clk);
1198 
1199 	return ret;
1200 }
1201 
1202 /* clk_disable_unprepare helps cases using clk_disable in non-atomic context. */
clk_disable_unprepare(struct clk * clk)1203 static inline void clk_disable_unprepare(struct clk *clk)
1204 {
1205 	clk_disable(clk);
1206 	clk_unprepare(clk);
1207 }
1208 
1209 static inline int __must_check
clk_bulk_prepare_enable(int num_clks,const struct clk_bulk_data * clks)1210 clk_bulk_prepare_enable(int num_clks, const struct clk_bulk_data *clks)
1211 {
1212 	int ret;
1213 
1214 	ret = clk_bulk_prepare(num_clks, clks);
1215 	if (ret)
1216 		return ret;
1217 	ret = clk_bulk_enable(num_clks, clks);
1218 	if (ret)
1219 		clk_bulk_unprepare(num_clks, clks);
1220 
1221 	return ret;
1222 }
1223 
clk_bulk_disable_unprepare(int num_clks,const struct clk_bulk_data * clks)1224 static inline void clk_bulk_disable_unprepare(int num_clks,
1225 					      const struct clk_bulk_data *clks)
1226 {
1227 	clk_bulk_disable(num_clks, clks);
1228 	clk_bulk_unprepare(num_clks, clks);
1229 }
1230 
1231 /**
1232  * clk_drop_range - Reset any range set on that clock
1233  * @clk: clock source
1234  *
1235  * Returns success (0) or negative errno.
1236  */
clk_drop_range(struct clk * clk)1237 static inline int clk_drop_range(struct clk *clk)
1238 {
1239 	return clk_set_rate_range(clk, 0, ULONG_MAX);
1240 }
1241 
1242 /**
1243  * clk_get_optional - lookup and obtain a reference to an optional clock
1244  *		      producer.
1245  * @dev: device for clock "consumer"
1246  * @id: clock consumer ID
1247  *
1248  * Behaves the same as clk_get() except where there is no clock producer. In
1249  * this case, instead of returning -ENOENT, the function returns NULL.
1250  */
clk_get_optional(struct device * dev,const char * id)1251 static inline struct clk *clk_get_optional(struct device *dev, const char *id)
1252 {
1253 	struct clk *clk = clk_get(dev, id);
1254 
1255 	if (clk == ERR_PTR(-ENOENT))
1256 		return NULL;
1257 
1258 	return clk;
1259 }
1260 
1261 #if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK)
1262 struct clk *of_clk_get(struct device_node *np, int index);
1263 struct clk *of_clk_get_by_name(struct device_node *np, const char *name);
1264 struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec);
1265 #else
of_clk_get(struct device_node * np,int index)1266 static inline struct clk *of_clk_get(struct device_node *np, int index)
1267 {
1268 	return ERR_PTR(-ENOENT);
1269 }
of_clk_get_by_name(struct device_node * np,const char * name)1270 static inline struct clk *of_clk_get_by_name(struct device_node *np,
1271 					     const char *name)
1272 {
1273 	return ERR_PTR(-ENOENT);
1274 }
of_clk_get_from_provider(struct of_phandle_args * clkspec)1275 static inline struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
1276 {
1277 	return ERR_PTR(-ENOENT);
1278 }
1279 #endif
1280 
1281 #endif
1282