xref: /linux-6.15/include/linux/clk.h (revision 6faeeea4)
1 /*
2  *  linux/include/linux/clk.h
3  *
4  *  Copyright (C) 2004 ARM Limited.
5  *  Written by Deep Blue Solutions Limited.
6  *  Copyright (C) 2011-2012 Linaro Ltd <[email protected]>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12 #ifndef __LINUX_CLK_H
13 #define __LINUX_CLK_H
14 
15 #include <linux/err.h>
16 #include <linux/kernel.h>
17 #include <linux/notifier.h>
18 
19 struct device;
20 
21 struct clk;
22 
23 #ifdef CONFIG_COMMON_CLK
24 
25 /**
26  * DOC: clk notifier callback types
27  *
28  * PRE_RATE_CHANGE - called immediately before the clk rate is changed,
29  *     to indicate that the rate change will proceed.  Drivers must
30  *     immediately terminate any operations that will be affected by the
31  *     rate change.  Callbacks may either return NOTIFY_DONE, NOTIFY_OK,
32  *     NOTIFY_STOP or NOTIFY_BAD.
33  *
34  * ABORT_RATE_CHANGE: called if the rate change failed for some reason
35  *     after PRE_RATE_CHANGE.  In this case, all registered notifiers on
36  *     the clk will be called with ABORT_RATE_CHANGE. Callbacks must
37  *     always return NOTIFY_DONE or NOTIFY_OK.
38  *
39  * POST_RATE_CHANGE - called after the clk rate change has successfully
40  *     completed.  Callbacks must always return NOTIFY_DONE or NOTIFY_OK.
41  *
42  */
43 #define PRE_RATE_CHANGE			BIT(0)
44 #define POST_RATE_CHANGE		BIT(1)
45 #define ABORT_RATE_CHANGE		BIT(2)
46 
47 /**
48  * struct clk_notifier - associate a clk with a notifier
49  * @clk: struct clk * to associate the notifier with
50  * @notifier_head: a blocking_notifier_head for this clk
51  * @node: linked list pointers
52  *
53  * A list of struct clk_notifier is maintained by the notifier code.
54  * An entry is created whenever code registers the first notifier on a
55  * particular @clk.  Future notifiers on that @clk are added to the
56  * @notifier_head.
57  */
58 struct clk_notifier {
59 	struct clk			*clk;
60 	struct srcu_notifier_head	notifier_head;
61 	struct list_head		node;
62 };
63 
64 /**
65  * struct clk_notifier_data - rate data to pass to the notifier callback
66  * @clk: struct clk * being changed
67  * @old_rate: previous rate of this clk
68  * @new_rate: new rate of this clk
69  *
70  * For a pre-notifier, old_rate is the clk's rate before this rate
71  * change, and new_rate is what the rate will be in the future.  For a
72  * post-notifier, old_rate and new_rate are both set to the clk's
73  * current rate (this was done to optimize the implementation).
74  */
75 struct clk_notifier_data {
76 	struct clk		*clk;
77 	unsigned long		old_rate;
78 	unsigned long		new_rate;
79 };
80 
81 /**
82  * clk_notifier_register: register a clock rate-change notifier callback
83  * @clk: clock whose rate we are interested in
84  * @nb: notifier block with callback function pointer
85  *
86  * ProTip: debugging across notifier chains can be frustrating. Make sure that
87  * your notifier callback function prints a nice big warning in case of
88  * failure.
89  */
90 int clk_notifier_register(struct clk *clk, struct notifier_block *nb);
91 
92 /**
93  * clk_notifier_unregister: unregister a clock rate-change notifier callback
94  * @clk: clock whose rate we are no longer interested in
95  * @nb: notifier block which will be unregistered
96  */
97 int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb);
98 
99 /**
100  * clk_get_accuracy - obtain the clock accuracy in ppb (parts per billion)
101  *		      for a clock source.
102  * @clk: clock source
103  *
104  * This gets the clock source accuracy expressed in ppb.
105  * A perfect clock returns 0.
106  */
107 long clk_get_accuracy(struct clk *clk);
108 
109 /**
110  * clk_set_phase - adjust the phase shift of a clock signal
111  * @clk: clock signal source
112  * @degrees: number of degrees the signal is shifted
113  *
114  * Shifts the phase of a clock signal by the specified degrees. Returns 0 on
115  * success, -EERROR otherwise.
116  */
117 int clk_set_phase(struct clk *clk, int degrees);
118 
119 /**
120  * clk_get_phase - return the phase shift of a clock signal
121  * @clk: clock signal source
122  *
123  * Returns the phase shift of a clock node in degrees, otherwise returns
124  * -EERROR.
125  */
126 int clk_get_phase(struct clk *clk);
127 
128 #else
129 
130 static inline long clk_get_accuracy(struct clk *clk)
131 {
132 	return -ENOTSUPP;
133 }
134 
135 static inline long clk_set_phase(struct clk *clk, int phase)
136 {
137 	return -ENOTSUPP;
138 }
139 
140 static inline long clk_get_phase(struct clk *clk)
141 {
142 	return -ENOTSUPP;
143 }
144 
145 #endif
146 
147 /**
148  * clk_prepare - prepare a clock source
149  * @clk: clock source
150  *
151  * This prepares the clock source for use.
152  *
153  * Must not be called from within atomic context.
154  */
155 #ifdef CONFIG_HAVE_CLK_PREPARE
156 int clk_prepare(struct clk *clk);
157 #else
158 static inline int clk_prepare(struct clk *clk)
159 {
160 	might_sleep();
161 	return 0;
162 }
163 #endif
164 
165 /**
166  * clk_unprepare - undo preparation of a clock source
167  * @clk: clock source
168  *
169  * This undoes a previously prepared clock.  The caller must balance
170  * the number of prepare and unprepare calls.
171  *
172  * Must not be called from within atomic context.
173  */
174 #ifdef CONFIG_HAVE_CLK_PREPARE
175 void clk_unprepare(struct clk *clk);
176 #else
177 static inline void clk_unprepare(struct clk *clk)
178 {
179 	might_sleep();
180 }
181 #endif
182 
183 #ifdef CONFIG_HAVE_CLK
184 /**
185  * clk_get - lookup and obtain a reference to a clock producer.
186  * @dev: device for clock "consumer"
187  * @id: clock consumer ID
188  *
189  * Returns a struct clk corresponding to the clock producer, or
190  * valid IS_ERR() condition containing errno.  The implementation
191  * uses @dev and @id to determine the clock consumer, and thereby
192  * the clock producer.  (IOW, @id may be identical strings, but
193  * clk_get may return different clock producers depending on @dev.)
194  *
195  * Drivers must assume that the clock source is not enabled.
196  *
197  * clk_get should not be called from within interrupt context.
198  */
199 struct clk *clk_get(struct device *dev, const char *id);
200 
201 /**
202  * devm_clk_get - lookup and obtain a managed reference to a clock producer.
203  * @dev: device for clock "consumer"
204  * @id: clock consumer ID
205  *
206  * Returns a struct clk corresponding to the clock producer, or
207  * valid IS_ERR() condition containing errno.  The implementation
208  * uses @dev and @id to determine the clock consumer, and thereby
209  * the clock producer.  (IOW, @id may be identical strings, but
210  * clk_get may return different clock producers depending on @dev.)
211  *
212  * Drivers must assume that the clock source is not enabled.
213  *
214  * devm_clk_get should not be called from within interrupt context.
215  *
216  * The clock will automatically be freed when the device is unbound
217  * from the bus.
218  */
219 struct clk *devm_clk_get(struct device *dev, const char *id);
220 
221 /**
222  * clk_enable - inform the system when the clock source should be running.
223  * @clk: clock source
224  *
225  * If the clock can not be enabled/disabled, this should return success.
226  *
227  * May be called from atomic contexts.
228  *
229  * Returns success (0) or negative errno.
230  */
231 int clk_enable(struct clk *clk);
232 
233 /**
234  * clk_disable - inform the system when the clock source is no longer required.
235  * @clk: clock source
236  *
237  * Inform the system that a clock source is no longer required by
238  * a driver and may be shut down.
239  *
240  * May be called from atomic contexts.
241  *
242  * Implementation detail: if the clock source is shared between
243  * multiple drivers, clk_enable() calls must be balanced by the
244  * same number of clk_disable() calls for the clock source to be
245  * disabled.
246  */
247 void clk_disable(struct clk *clk);
248 
249 /**
250  * clk_get_rate - obtain the current clock rate (in Hz) for a clock source.
251  *		  This is only valid once the clock source has been enabled.
252  * @clk: clock source
253  */
254 unsigned long clk_get_rate(struct clk *clk);
255 
256 /**
257  * clk_put	- "free" the clock source
258  * @clk: clock source
259  *
260  * Note: drivers must ensure that all clk_enable calls made on this
261  * clock source are balanced by clk_disable calls prior to calling
262  * this function.
263  *
264  * clk_put should not be called from within interrupt context.
265  */
266 void clk_put(struct clk *clk);
267 
268 /**
269  * devm_clk_put	- "free" a managed clock source
270  * @dev: device used to acquire the clock
271  * @clk: clock source acquired with devm_clk_get()
272  *
273  * Note: drivers must ensure that all clk_enable calls made on this
274  * clock source are balanced by clk_disable calls prior to calling
275  * this function.
276  *
277  * clk_put should not be called from within interrupt context.
278  */
279 void devm_clk_put(struct device *dev, struct clk *clk);
280 
281 /*
282  * The remaining APIs are optional for machine class support.
283  */
284 
285 
286 /**
287  * clk_round_rate - adjust a rate to the exact rate a clock can provide
288  * @clk: clock source
289  * @rate: desired clock rate in Hz
290  *
291  * Returns rounded clock rate in Hz, or negative errno.
292  */
293 long clk_round_rate(struct clk *clk, unsigned long rate);
294 
295 /**
296  * clk_set_rate - set the clock rate for a clock source
297  * @clk: clock source
298  * @rate: desired clock rate in Hz
299  *
300  * Returns success (0) or negative errno.
301  */
302 int clk_set_rate(struct clk *clk, unsigned long rate);
303 
304 /**
305  * clk_has_parent - check if a clock is a possible parent for another
306  * @clk: clock source
307  * @parent: parent clock source
308  *
309  * This function can be used in drivers that need to check that a clock can be
310  * the parent of another without actually changing the parent.
311  *
312  * Returns true if @parent is a possible parent for @clk, false otherwise.
313  */
314 bool clk_has_parent(struct clk *clk, struct clk *parent);
315 
316 /**
317  * clk_set_rate_range - set a rate range for a clock source
318  * @clk: clock source
319  * @min: desired minimum clock rate in Hz, inclusive
320  * @max: desired maximum clock rate in Hz, inclusive
321  *
322  * Returns success (0) or negative errno.
323  */
324 int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max);
325 
326 /**
327  * clk_set_min_rate - set a minimum clock rate for a clock source
328  * @clk: clock source
329  * @rate: desired minimum clock rate in Hz, inclusive
330  *
331  * Returns success (0) or negative errno.
332  */
333 int clk_set_min_rate(struct clk *clk, unsigned long rate);
334 
335 /**
336  * clk_set_max_rate - set a maximum clock rate for a clock source
337  * @clk: clock source
338  * @rate: desired maximum clock rate in Hz, inclusive
339  *
340  * Returns success (0) or negative errno.
341  */
342 int clk_set_max_rate(struct clk *clk, unsigned long rate);
343 
344 /**
345  * clk_set_parent - set the parent clock source for this clock
346  * @clk: clock source
347  * @parent: parent clock source
348  *
349  * Returns success (0) or negative errno.
350  */
351 int clk_set_parent(struct clk *clk, struct clk *parent);
352 
353 /**
354  * clk_get_parent - get the parent clock source for this clock
355  * @clk: clock source
356  *
357  * Returns struct clk corresponding to parent clock source, or
358  * valid IS_ERR() condition containing errno.
359  */
360 struct clk *clk_get_parent(struct clk *clk);
361 
362 /**
363  * clk_get_sys - get a clock based upon the device name
364  * @dev_id: device name
365  * @con_id: connection ID
366  *
367  * Returns a struct clk corresponding to the clock producer, or
368  * valid IS_ERR() condition containing errno.  The implementation
369  * uses @dev_id and @con_id to determine the clock consumer, and
370  * thereby the clock producer. In contrast to clk_get() this function
371  * takes the device name instead of the device itself for identification.
372  *
373  * Drivers must assume that the clock source is not enabled.
374  *
375  * clk_get_sys should not be called from within interrupt context.
376  */
377 struct clk *clk_get_sys(const char *dev_id, const char *con_id);
378 
379 #else /* !CONFIG_HAVE_CLK */
380 
381 static inline struct clk *clk_get(struct device *dev, const char *id)
382 {
383 	return NULL;
384 }
385 
386 static inline struct clk *devm_clk_get(struct device *dev, const char *id)
387 {
388 	return NULL;
389 }
390 
391 static inline void clk_put(struct clk *clk) {}
392 
393 static inline void devm_clk_put(struct device *dev, struct clk *clk) {}
394 
395 static inline int clk_enable(struct clk *clk)
396 {
397 	return 0;
398 }
399 
400 static inline void clk_disable(struct clk *clk) {}
401 
402 static inline unsigned long clk_get_rate(struct clk *clk)
403 {
404 	return 0;
405 }
406 
407 static inline int clk_set_rate(struct clk *clk, unsigned long rate)
408 {
409 	return 0;
410 }
411 
412 static inline long clk_round_rate(struct clk *clk, unsigned long rate)
413 {
414 	return 0;
415 }
416 
417 static inline bool clk_has_parent(struct clk *clk, struct clk *parent)
418 {
419 	return true;
420 }
421 
422 static inline int clk_set_parent(struct clk *clk, struct clk *parent)
423 {
424 	return 0;
425 }
426 
427 static inline struct clk *clk_get_parent(struct clk *clk)
428 {
429 	return NULL;
430 }
431 
432 #endif
433 
434 /* clk_prepare_enable helps cases using clk_enable in non-atomic context. */
435 static inline int clk_prepare_enable(struct clk *clk)
436 {
437 	int ret;
438 
439 	ret = clk_prepare(clk);
440 	if (ret)
441 		return ret;
442 	ret = clk_enable(clk);
443 	if (ret)
444 		clk_unprepare(clk);
445 
446 	return ret;
447 }
448 
449 /* clk_disable_unprepare helps cases using clk_disable in non-atomic context. */
450 static inline void clk_disable_unprepare(struct clk *clk)
451 {
452 	clk_disable(clk);
453 	clk_unprepare(clk);
454 }
455 
456 /**
457  * clk_add_alias - add a new clock alias
458  * @alias: name for clock alias
459  * @alias_dev_name: device name
460  * @id: platform specific clock name
461  * @dev: device
462  *
463  * Allows using generic clock names for drivers by adding a new alias.
464  * Assumes clkdev, see clkdev.h for more info.
465  */
466 int clk_add_alias(const char *alias, const char *alias_dev_name, char *id,
467 			struct device *dev);
468 
469 struct device_node;
470 struct of_phandle_args;
471 
472 #if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK)
473 struct clk *of_clk_get(struct device_node *np, int index);
474 struct clk *of_clk_get_by_name(struct device_node *np, const char *name);
475 struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec);
476 #else
477 static inline struct clk *of_clk_get(struct device_node *np, int index)
478 {
479 	return ERR_PTR(-ENOENT);
480 }
481 static inline struct clk *of_clk_get_by_name(struct device_node *np,
482 					     const char *name)
483 {
484 	return ERR_PTR(-ENOENT);
485 }
486 #endif
487 
488 #endif
489