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_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 * clk_rate_exclusive_put - release exclusivity over the rate control of a
206 * producer
207 * @clk: clock source
208 *
209 * This function allows drivers to release the exclusivity it previously got
210 * from clk_rate_exclusive_get()
211 *
212 * The caller must balance the number of clk_rate_exclusive_get() and
213 * clk_rate_exclusive_put() calls.
214 *
215 * Must not be called from within atomic context.
216 */
217 void clk_rate_exclusive_put(struct clk *clk);
218
219 #else
220
clk_notifier_register(struct clk * clk,struct notifier_block * nb)221 static inline int clk_notifier_register(struct clk *clk,
222 struct notifier_block *nb)
223 {
224 return -ENOTSUPP;
225 }
226
clk_notifier_unregister(struct clk * clk,struct notifier_block * nb)227 static inline int clk_notifier_unregister(struct clk *clk,
228 struct notifier_block *nb)
229 {
230 return -ENOTSUPP;
231 }
232
devm_clk_notifier_register(struct device * dev,struct clk * clk,struct notifier_block * nb)233 static inline int devm_clk_notifier_register(struct device *dev,
234 struct clk *clk,
235 struct notifier_block *nb)
236 {
237 return -ENOTSUPP;
238 }
239
clk_get_accuracy(struct clk * clk)240 static inline long clk_get_accuracy(struct clk *clk)
241 {
242 return -ENOTSUPP;
243 }
244
clk_set_phase(struct clk * clk,int phase)245 static inline long clk_set_phase(struct clk *clk, int phase)
246 {
247 return -ENOTSUPP;
248 }
249
clk_get_phase(struct clk * clk)250 static inline long clk_get_phase(struct clk *clk)
251 {
252 return -ENOTSUPP;
253 }
254
clk_set_duty_cycle(struct clk * clk,unsigned int num,unsigned int den)255 static inline int clk_set_duty_cycle(struct clk *clk, unsigned int num,
256 unsigned int den)
257 {
258 return -ENOTSUPP;
259 }
260
clk_get_scaled_duty_cycle(struct clk * clk,unsigned int scale)261 static inline unsigned int clk_get_scaled_duty_cycle(struct clk *clk,
262 unsigned int scale)
263 {
264 return 0;
265 }
266
clk_is_match(const struct clk * p,const struct clk * q)267 static inline bool clk_is_match(const struct clk *p, const struct clk *q)
268 {
269 return p == q;
270 }
271
clk_rate_exclusive_get(struct clk * clk)272 static inline int clk_rate_exclusive_get(struct clk *clk)
273 {
274 return 0;
275 }
276
clk_rate_exclusive_put(struct clk * clk)277 static inline void clk_rate_exclusive_put(struct clk *clk) {}
278
279 #endif
280
281 /**
282 * clk_prepare - prepare a clock source
283 * @clk: clock source
284 *
285 * This prepares the clock source for use.
286 *
287 * Must not be called from within atomic context.
288 */
289 #ifdef CONFIG_HAVE_CLK_PREPARE
290 int clk_prepare(struct clk *clk);
291 int __must_check clk_bulk_prepare(int num_clks,
292 const struct clk_bulk_data *clks);
293 #else
clk_prepare(struct clk * clk)294 static inline int clk_prepare(struct clk *clk)
295 {
296 might_sleep();
297 return 0;
298 }
299
300 static inline int __must_check
clk_bulk_prepare(int num_clks,const struct clk_bulk_data * clks)301 clk_bulk_prepare(int num_clks, const struct clk_bulk_data *clks)
302 {
303 might_sleep();
304 return 0;
305 }
306 #endif
307
308 /**
309 * clk_unprepare - undo preparation of a clock source
310 * @clk: clock source
311 *
312 * This undoes a previously prepared clock. The caller must balance
313 * the number of prepare and unprepare calls.
314 *
315 * Must not be called from within atomic context.
316 */
317 #ifdef CONFIG_HAVE_CLK_PREPARE
318 void clk_unprepare(struct clk *clk);
319 void clk_bulk_unprepare(int num_clks, const struct clk_bulk_data *clks);
320 #else
clk_unprepare(struct clk * clk)321 static inline void clk_unprepare(struct clk *clk)
322 {
323 might_sleep();
324 }
clk_bulk_unprepare(int num_clks,const struct clk_bulk_data * clks)325 static inline void clk_bulk_unprepare(int num_clks,
326 const struct clk_bulk_data *clks)
327 {
328 might_sleep();
329 }
330 #endif
331
332 #ifdef CONFIG_HAVE_CLK
333 /**
334 * clk_get - lookup and obtain a reference to a clock producer.
335 * @dev: device for clock "consumer"
336 * @id: clock consumer ID
337 *
338 * Returns a struct clk corresponding to the clock producer, or
339 * valid IS_ERR() condition containing errno. The implementation
340 * uses @dev and @id to determine the clock consumer, and thereby
341 * the clock producer. (IOW, @id may be identical strings, but
342 * clk_get may return different clock producers depending on @dev.)
343 *
344 * Drivers must assume that the clock source is not enabled.
345 *
346 * clk_get should not be called from within interrupt context.
347 */
348 struct clk *clk_get(struct device *dev, const char *id);
349
350 /**
351 * clk_bulk_get - lookup and obtain a number of references to clock producer.
352 * @dev: device for clock "consumer"
353 * @num_clks: the number of clk_bulk_data
354 * @clks: the clk_bulk_data table of consumer
355 *
356 * This helper function allows drivers to get several clk consumers in one
357 * operation. If any of the clk cannot be acquired then any clks
358 * that were obtained will be freed before returning to the caller.
359 *
360 * Returns 0 if all clocks specified in clk_bulk_data table are obtained
361 * successfully, or valid IS_ERR() condition containing errno.
362 * The implementation uses @dev and @clk_bulk_data.id to determine the
363 * clock consumer, and thereby the clock producer.
364 * The clock returned is stored in each @clk_bulk_data.clk field.
365 *
366 * Drivers must assume that the clock source is not enabled.
367 *
368 * clk_bulk_get should not be called from within interrupt context.
369 */
370 int __must_check clk_bulk_get(struct device *dev, int num_clks,
371 struct clk_bulk_data *clks);
372 /**
373 * clk_bulk_get_all - lookup and obtain all available references to clock
374 * producer.
375 * @dev: device for clock "consumer"
376 * @clks: pointer to the clk_bulk_data table of consumer
377 *
378 * This helper function allows drivers to get all clk consumers in one
379 * operation. If any of the clk cannot be acquired then any clks
380 * that were obtained will be freed before returning to the caller.
381 *
382 * Returns a positive value for the number of clocks obtained while the
383 * clock references are stored in the clk_bulk_data table in @clks field.
384 * Returns 0 if there're none and a negative value if something failed.
385 *
386 * Drivers must assume that the clock source is not enabled.
387 *
388 * clk_bulk_get should not be called from within interrupt context.
389 */
390 int __must_check clk_bulk_get_all(struct device *dev,
391 struct clk_bulk_data **clks);
392
393 /**
394 * clk_bulk_get_optional - lookup and obtain a number of references to clock producer
395 * @dev: device for clock "consumer"
396 * @num_clks: the number of clk_bulk_data
397 * @clks: the clk_bulk_data table of consumer
398 *
399 * Behaves the same as clk_bulk_get() except where there is no clock producer.
400 * In this case, instead of returning -ENOENT, the function returns 0 and
401 * NULL for a clk for which a clock producer could not be determined.
402 */
403 int __must_check clk_bulk_get_optional(struct device *dev, int num_clks,
404 struct clk_bulk_data *clks);
405 /**
406 * devm_clk_bulk_get - managed get multiple clk consumers
407 * @dev: device for clock "consumer"
408 * @num_clks: the number of clk_bulk_data
409 * @clks: the clk_bulk_data table of consumer
410 *
411 * Return 0 on success, an errno on failure.
412 *
413 * This helper function allows drivers to get several clk
414 * consumers in one operation with management, the clks will
415 * automatically be freed when the device is unbound.
416 */
417 int __must_check devm_clk_bulk_get(struct device *dev, int num_clks,
418 struct clk_bulk_data *clks);
419 /**
420 * devm_clk_bulk_get_optional - managed get multiple optional consumer clocks
421 * @dev: device for clock "consumer"
422 * @num_clks: the number of clk_bulk_data
423 * @clks: pointer to the clk_bulk_data table of consumer
424 *
425 * Behaves the same as devm_clk_bulk_get() except where there is no clock
426 * producer. In this case, instead of returning -ENOENT, the function returns
427 * NULL for given clk. It is assumed all clocks in clk_bulk_data are optional.
428 *
429 * Returns 0 if all clocks specified in clk_bulk_data table are obtained
430 * successfully or for any clk there was no clk provider available, otherwise
431 * returns valid IS_ERR() condition containing errno.
432 * The implementation uses @dev and @clk_bulk_data.id to determine the
433 * clock consumer, and thereby the clock producer.
434 * The clock returned is stored in each @clk_bulk_data.clk field.
435 *
436 * Drivers must assume that the clock source is not enabled.
437 *
438 * clk_bulk_get should not be called from within interrupt context.
439 */
440 int __must_check devm_clk_bulk_get_optional(struct device *dev, int num_clks,
441 struct clk_bulk_data *clks);
442 /**
443 * devm_clk_bulk_get_all - managed get multiple clk consumers
444 * @dev: device for clock "consumer"
445 * @clks: pointer to the clk_bulk_data table of consumer
446 *
447 * Returns a positive value for the number of clocks obtained while the
448 * clock references are stored in the clk_bulk_data table in @clks field.
449 * Returns 0 if there're none and a negative value if something failed.
450 *
451 * This helper function allows drivers to get several clk
452 * consumers in one operation with management, the clks will
453 * automatically be freed when the device is unbound.
454 */
455
456 int __must_check devm_clk_bulk_get_all(struct device *dev,
457 struct clk_bulk_data **clks);
458
459 /**
460 * devm_clk_get - lookup and obtain a managed reference to a clock producer.
461 * @dev: device for clock "consumer"
462 * @id: clock consumer ID
463 *
464 * Returns a struct clk corresponding to the clock producer, or
465 * valid IS_ERR() condition containing errno. The implementation
466 * uses @dev and @id to determine the clock consumer, and thereby
467 * the clock producer. (IOW, @id may be identical strings, but
468 * clk_get may return different clock producers depending on @dev.)
469 *
470 * Drivers must assume that the clock source is not enabled.
471 *
472 * devm_clk_get should not be called from within interrupt context.
473 *
474 * The clock will automatically be freed when the device is unbound
475 * from the bus.
476 */
477 struct clk *devm_clk_get(struct device *dev, const char *id);
478
479 /**
480 * devm_clk_get_prepared - devm_clk_get() + clk_prepare()
481 * @dev: device for clock "consumer"
482 * @id: clock consumer ID
483 *
484 * Context: May sleep.
485 *
486 * Return: a struct clk corresponding to the clock producer, or
487 * valid IS_ERR() condition containing errno. The implementation
488 * uses @dev and @id to determine the clock consumer, and thereby
489 * the clock producer. (IOW, @id may be identical strings, but
490 * clk_get may return different clock producers depending on @dev.)
491 *
492 * The returned clk (if valid) is prepared. Drivers must however assume
493 * that the clock is not enabled.
494 *
495 * The clock will automatically be unprepared and freed when the device
496 * is unbound from the bus.
497 */
498 struct clk *devm_clk_get_prepared(struct device *dev, const char *id);
499
500 /**
501 * devm_clk_get_enabled - devm_clk_get() + clk_prepare_enable()
502 * @dev: device for clock "consumer"
503 * @id: clock consumer ID
504 *
505 * Context: May sleep.
506 *
507 * Return: a struct clk corresponding to the clock producer, or
508 * valid IS_ERR() condition containing errno. The implementation
509 * uses @dev and @id to determine the clock consumer, and thereby
510 * the clock producer. (IOW, @id may be identical strings, but
511 * clk_get may return different clock producers depending on @dev.)
512 *
513 * The returned clk (if valid) is prepared and enabled.
514 *
515 * The clock will automatically be disabled, unprepared and freed
516 * when the device is unbound from the bus.
517 */
518 struct clk *devm_clk_get_enabled(struct device *dev, const char *id);
519
520 /**
521 * devm_clk_get_optional - lookup and obtain a managed reference to an optional
522 * clock producer.
523 * @dev: device for clock "consumer"
524 * @id: clock consumer ID
525 *
526 * Behaves the same as devm_clk_get() except where there is no clock producer.
527 * In this case, instead of returning -ENOENT, the function returns NULL.
528 */
529 struct clk *devm_clk_get_optional(struct device *dev, const char *id);
530
531 /**
532 * devm_clk_get_optional_prepared - devm_clk_get_optional() + clk_prepare()
533 * @dev: device for clock "consumer"
534 * @id: clock consumer ID
535 *
536 * Context: May sleep.
537 *
538 * Return: a struct clk corresponding to the clock producer, or
539 * valid IS_ERR() condition containing errno. The implementation
540 * uses @dev and @id to determine the clock consumer, and thereby
541 * the clock producer. If no such clk is found, it returns NULL
542 * which serves as a dummy clk. That's the only difference compared
543 * to devm_clk_get_prepared().
544 *
545 * The returned clk (if valid) is prepared. Drivers must however
546 * assume that the clock is not enabled.
547 *
548 * The clock will automatically be unprepared and freed when the
549 * device is unbound from the bus.
550 */
551 struct clk *devm_clk_get_optional_prepared(struct device *dev, const char *id);
552
553 /**
554 * devm_clk_get_optional_enabled - devm_clk_get_optional() +
555 * clk_prepare_enable()
556 * @dev: device for clock "consumer"
557 * @id: clock consumer ID
558 *
559 * Context: May sleep.
560 *
561 * Return: a struct clk corresponding to the clock producer, or
562 * valid IS_ERR() condition containing errno. The implementation
563 * uses @dev and @id to determine the clock consumer, and thereby
564 * the clock producer. If no such clk is found, it returns NULL
565 * which serves as a dummy clk. That's the only difference compared
566 * to devm_clk_get_enabled().
567 *
568 * The returned clk (if valid) is prepared and enabled.
569 *
570 * The clock will automatically be disabled, unprepared and freed
571 * when the device is unbound from the bus.
572 */
573 struct clk *devm_clk_get_optional_enabled(struct device *dev, const char *id);
574
575 /**
576 * devm_get_clk_from_child - lookup and obtain a managed reference to a
577 * clock producer from child node.
578 * @dev: device for clock "consumer"
579 * @np: pointer to clock consumer node
580 * @con_id: clock consumer ID
581 *
582 * This function parses the clocks, and uses them to look up the
583 * struct clk from the registered list of clock providers by using
584 * @np and @con_id
585 *
586 * The clock will automatically be freed when the device is unbound
587 * from the bus.
588 */
589 struct clk *devm_get_clk_from_child(struct device *dev,
590 struct device_node *np, const char *con_id);
591
592 /**
593 * clk_enable - inform the system when the clock source should be running.
594 * @clk: clock source
595 *
596 * If the clock can not be enabled/disabled, this should return success.
597 *
598 * May be called from atomic contexts.
599 *
600 * Returns success (0) or negative errno.
601 */
602 int clk_enable(struct clk *clk);
603
604 /**
605 * clk_bulk_enable - inform the system when the set of clks should be running.
606 * @num_clks: the number of clk_bulk_data
607 * @clks: the clk_bulk_data table of consumer
608 *
609 * May be called from atomic contexts.
610 *
611 * Returns success (0) or negative errno.
612 */
613 int __must_check clk_bulk_enable(int num_clks,
614 const struct clk_bulk_data *clks);
615
616 /**
617 * clk_disable - inform the system when the clock source is no longer required.
618 * @clk: clock source
619 *
620 * Inform the system that a clock source is no longer required by
621 * a driver and may be shut down.
622 *
623 * May be called from atomic contexts.
624 *
625 * Implementation detail: if the clock source is shared between
626 * multiple drivers, clk_enable() calls must be balanced by the
627 * same number of clk_disable() calls for the clock source to be
628 * disabled.
629 */
630 void clk_disable(struct clk *clk);
631
632 /**
633 * clk_bulk_disable - inform the system when the set of clks is no
634 * longer required.
635 * @num_clks: the number of clk_bulk_data
636 * @clks: the clk_bulk_data table of consumer
637 *
638 * Inform the system that a set of clks is no longer required by
639 * a driver and may be shut down.
640 *
641 * May be called from atomic contexts.
642 *
643 * Implementation detail: if the set of clks is shared between
644 * multiple drivers, clk_bulk_enable() calls must be balanced by the
645 * same number of clk_bulk_disable() calls for the clock source to be
646 * disabled.
647 */
648 void clk_bulk_disable(int num_clks, const struct clk_bulk_data *clks);
649
650 /**
651 * clk_get_rate - obtain the current clock rate (in Hz) for a clock source.
652 * This is only valid once the clock source has been enabled.
653 * @clk: clock source
654 */
655 unsigned long clk_get_rate(struct clk *clk);
656
657 /**
658 * clk_put - "free" the clock source
659 * @clk: clock source
660 *
661 * Note: drivers must ensure that all clk_enable calls made on this
662 * clock source are balanced by clk_disable calls prior to calling
663 * this function.
664 *
665 * clk_put should not be called from within interrupt context.
666 */
667 void clk_put(struct clk *clk);
668
669 /**
670 * clk_bulk_put - "free" the clock source
671 * @num_clks: the number of clk_bulk_data
672 * @clks: the clk_bulk_data table of consumer
673 *
674 * Note: drivers must ensure that all clk_bulk_enable calls made on this
675 * clock source are balanced by clk_bulk_disable calls prior to calling
676 * this function.
677 *
678 * clk_bulk_put should not be called from within interrupt context.
679 */
680 void clk_bulk_put(int num_clks, struct clk_bulk_data *clks);
681
682 /**
683 * clk_bulk_put_all - "free" all the clock source
684 * @num_clks: the number of clk_bulk_data
685 * @clks: the clk_bulk_data table of consumer
686 *
687 * Note: drivers must ensure that all clk_bulk_enable calls made on this
688 * clock source are balanced by clk_bulk_disable calls prior to calling
689 * this function.
690 *
691 * clk_bulk_put_all should not be called from within interrupt context.
692 */
693 void clk_bulk_put_all(int num_clks, struct clk_bulk_data *clks);
694
695 /**
696 * devm_clk_put - "free" a managed clock source
697 * @dev: device used to acquire the clock
698 * @clk: clock source acquired with devm_clk_get()
699 *
700 * Note: drivers must ensure that all clk_enable calls made on this
701 * clock source are balanced by clk_disable calls prior to calling
702 * this function.
703 *
704 * clk_put should not be called from within interrupt context.
705 */
706 void devm_clk_put(struct device *dev, struct clk *clk);
707
708 /*
709 * The remaining APIs are optional for machine class support.
710 */
711
712
713 /**
714 * clk_round_rate - adjust a rate to the exact rate a clock can provide
715 * @clk: clock source
716 * @rate: desired clock rate in Hz
717 *
718 * This answers the question "if I were to pass @rate to clk_set_rate(),
719 * what clock rate would I end up with?" without changing the hardware
720 * in any way. In other words:
721 *
722 * rate = clk_round_rate(clk, r);
723 *
724 * and:
725 *
726 * clk_set_rate(clk, r);
727 * rate = clk_get_rate(clk);
728 *
729 * are equivalent except the former does not modify the clock hardware
730 * in any way.
731 *
732 * Returns rounded clock rate in Hz, or negative errno.
733 */
734 long clk_round_rate(struct clk *clk, unsigned long rate);
735
736 /**
737 * clk_set_rate - set the clock rate for a clock source
738 * @clk: clock source
739 * @rate: desired clock rate in Hz
740 *
741 * Updating the rate starts at the top-most affected clock and then
742 * walks the tree down to the bottom-most clock that needs updating.
743 *
744 * Returns success (0) or negative errno.
745 */
746 int clk_set_rate(struct clk *clk, unsigned long rate);
747
748 /**
749 * clk_set_rate_exclusive- set the clock rate and claim exclusivity over
750 * clock source
751 * @clk: clock source
752 * @rate: desired clock rate in Hz
753 *
754 * This helper function allows drivers to atomically set the rate of a producer
755 * and claim exclusivity over the rate control of the producer.
756 *
757 * It is essentially a combination of clk_set_rate() and
758 * clk_rate_exclusite_get(). Caller must balance this call with a call to
759 * clk_rate_exclusive_put()
760 *
761 * Returns success (0) or negative errno.
762 */
763 int clk_set_rate_exclusive(struct clk *clk, unsigned long rate);
764
765 /**
766 * clk_has_parent - check if a clock is a possible parent for another
767 * @clk: clock source
768 * @parent: parent clock source
769 *
770 * This function can be used in drivers that need to check that a clock can be
771 * the parent of another without actually changing the parent.
772 *
773 * Returns true if @parent is a possible parent for @clk, false otherwise.
774 */
775 bool clk_has_parent(struct clk *clk, struct clk *parent);
776
777 /**
778 * clk_set_rate_range - set a rate range for a clock source
779 * @clk: clock source
780 * @min: desired minimum clock rate in Hz, inclusive
781 * @max: desired maximum clock rate in Hz, inclusive
782 *
783 * Returns success (0) or negative errno.
784 */
785 int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max);
786
787 /**
788 * clk_set_min_rate - set a minimum clock rate for a clock source
789 * @clk: clock source
790 * @rate: desired minimum clock rate in Hz, inclusive
791 *
792 * Returns success (0) or negative errno.
793 */
794 int clk_set_min_rate(struct clk *clk, unsigned long rate);
795
796 /**
797 * clk_set_max_rate - set a maximum clock rate for a clock source
798 * @clk: clock source
799 * @rate: desired maximum clock rate in Hz, inclusive
800 *
801 * Returns success (0) or negative errno.
802 */
803 int clk_set_max_rate(struct clk *clk, unsigned long rate);
804
805 /**
806 * clk_set_parent - set the parent clock source for this clock
807 * @clk: clock source
808 * @parent: parent clock source
809 *
810 * Returns success (0) or negative errno.
811 */
812 int clk_set_parent(struct clk *clk, struct clk *parent);
813
814 /**
815 * clk_get_parent - get the parent clock source for this clock
816 * @clk: clock source
817 *
818 * Returns struct clk corresponding to parent clock source, or
819 * valid IS_ERR() condition containing errno.
820 */
821 struct clk *clk_get_parent(struct clk *clk);
822
823 /**
824 * clk_get_sys - get a clock based upon the device name
825 * @dev_id: device name
826 * @con_id: connection ID
827 *
828 * Returns a struct clk corresponding to the clock producer, or
829 * valid IS_ERR() condition containing errno. The implementation
830 * uses @dev_id and @con_id to determine the clock consumer, and
831 * thereby the clock producer. In contrast to clk_get() this function
832 * takes the device name instead of the device itself for identification.
833 *
834 * Drivers must assume that the clock source is not enabled.
835 *
836 * clk_get_sys should not be called from within interrupt context.
837 */
838 struct clk *clk_get_sys(const char *dev_id, const char *con_id);
839
840 /**
841 * clk_save_context - save clock context for poweroff
842 *
843 * Saves the context of the clock register for powerstates in which the
844 * contents of the registers will be lost. Occurs deep within the suspend
845 * code so locking is not necessary.
846 */
847 int clk_save_context(void);
848
849 /**
850 * clk_restore_context - restore clock context after poweroff
851 *
852 * This occurs with all clocks enabled. Occurs deep within the resume code
853 * so locking is not necessary.
854 */
855 void clk_restore_context(void);
856
857 #else /* !CONFIG_HAVE_CLK */
858
clk_get(struct device * dev,const char * id)859 static inline struct clk *clk_get(struct device *dev, const char *id)
860 {
861 return NULL;
862 }
863
clk_bulk_get(struct device * dev,int num_clks,struct clk_bulk_data * clks)864 static inline int __must_check clk_bulk_get(struct device *dev, int num_clks,
865 struct clk_bulk_data *clks)
866 {
867 return 0;
868 }
869
clk_bulk_get_optional(struct device * dev,int num_clks,struct clk_bulk_data * clks)870 static inline int __must_check clk_bulk_get_optional(struct device *dev,
871 int num_clks, struct clk_bulk_data *clks)
872 {
873 return 0;
874 }
875
clk_bulk_get_all(struct device * dev,struct clk_bulk_data ** clks)876 static inline int __must_check clk_bulk_get_all(struct device *dev,
877 struct clk_bulk_data **clks)
878 {
879 return 0;
880 }
881
devm_clk_get(struct device * dev,const char * id)882 static inline struct clk *devm_clk_get(struct device *dev, const char *id)
883 {
884 return NULL;
885 }
886
devm_clk_get_prepared(struct device * dev,const char * id)887 static inline struct clk *devm_clk_get_prepared(struct device *dev,
888 const char *id)
889 {
890 return NULL;
891 }
892
devm_clk_get_enabled(struct device * dev,const char * id)893 static inline struct clk *devm_clk_get_enabled(struct device *dev,
894 const char *id)
895 {
896 return NULL;
897 }
898
devm_clk_get_optional(struct device * dev,const char * id)899 static inline struct clk *devm_clk_get_optional(struct device *dev,
900 const char *id)
901 {
902 return NULL;
903 }
904
devm_clk_get_optional_prepared(struct device * dev,const char * id)905 static inline struct clk *devm_clk_get_optional_prepared(struct device *dev,
906 const char *id)
907 {
908 return NULL;
909 }
910
devm_clk_get_optional_enabled(struct device * dev,const char * id)911 static inline struct clk *devm_clk_get_optional_enabled(struct device *dev,
912 const char *id)
913 {
914 return NULL;
915 }
916
devm_clk_bulk_get(struct device * dev,int num_clks,struct clk_bulk_data * clks)917 static inline int __must_check devm_clk_bulk_get(struct device *dev, int num_clks,
918 struct clk_bulk_data *clks)
919 {
920 return 0;
921 }
922
devm_clk_bulk_get_optional(struct device * dev,int num_clks,struct clk_bulk_data * clks)923 static inline int __must_check devm_clk_bulk_get_optional(struct device *dev,
924 int num_clks, struct clk_bulk_data *clks)
925 {
926 return 0;
927 }
928
devm_clk_bulk_get_all(struct device * dev,struct clk_bulk_data ** clks)929 static inline int __must_check devm_clk_bulk_get_all(struct device *dev,
930 struct clk_bulk_data **clks)
931 {
932
933 return 0;
934 }
935
devm_get_clk_from_child(struct device * dev,struct device_node * np,const char * con_id)936 static inline struct clk *devm_get_clk_from_child(struct device *dev,
937 struct device_node *np, const char *con_id)
938 {
939 return NULL;
940 }
941
clk_put(struct clk * clk)942 static inline void clk_put(struct clk *clk) {}
943
clk_bulk_put(int num_clks,struct clk_bulk_data * clks)944 static inline void clk_bulk_put(int num_clks, struct clk_bulk_data *clks) {}
945
clk_bulk_put_all(int num_clks,struct clk_bulk_data * clks)946 static inline void clk_bulk_put_all(int num_clks, struct clk_bulk_data *clks) {}
947
devm_clk_put(struct device * dev,struct clk * clk)948 static inline void devm_clk_put(struct device *dev, struct clk *clk) {}
949
clk_enable(struct clk * clk)950 static inline int clk_enable(struct clk *clk)
951 {
952 return 0;
953 }
954
clk_bulk_enable(int num_clks,const struct clk_bulk_data * clks)955 static inline int __must_check clk_bulk_enable(int num_clks,
956 const struct clk_bulk_data *clks)
957 {
958 return 0;
959 }
960
clk_disable(struct clk * clk)961 static inline void clk_disable(struct clk *clk) {}
962
963
clk_bulk_disable(int num_clks,const struct clk_bulk_data * clks)964 static inline void clk_bulk_disable(int num_clks,
965 const struct clk_bulk_data *clks) {}
966
clk_get_rate(struct clk * clk)967 static inline unsigned long clk_get_rate(struct clk *clk)
968 {
969 return 0;
970 }
971
clk_set_rate(struct clk * clk,unsigned long rate)972 static inline int clk_set_rate(struct clk *clk, unsigned long rate)
973 {
974 return 0;
975 }
976
clk_set_rate_exclusive(struct clk * clk,unsigned long rate)977 static inline int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
978 {
979 return 0;
980 }
981
clk_round_rate(struct clk * clk,unsigned long rate)982 static inline long clk_round_rate(struct clk *clk, unsigned long rate)
983 {
984 return 0;
985 }
986
clk_has_parent(struct clk * clk,struct clk * parent)987 static inline bool clk_has_parent(struct clk *clk, struct clk *parent)
988 {
989 return true;
990 }
991
clk_set_rate_range(struct clk * clk,unsigned long min,unsigned long max)992 static inline int clk_set_rate_range(struct clk *clk, unsigned long min,
993 unsigned long max)
994 {
995 return 0;
996 }
997
clk_set_min_rate(struct clk * clk,unsigned long rate)998 static inline int clk_set_min_rate(struct clk *clk, unsigned long rate)
999 {
1000 return 0;
1001 }
1002
clk_set_max_rate(struct clk * clk,unsigned long rate)1003 static inline int clk_set_max_rate(struct clk *clk, unsigned long rate)
1004 {
1005 return 0;
1006 }
1007
clk_set_parent(struct clk * clk,struct clk * parent)1008 static inline int clk_set_parent(struct clk *clk, struct clk *parent)
1009 {
1010 return 0;
1011 }
1012
clk_get_parent(struct clk * clk)1013 static inline struct clk *clk_get_parent(struct clk *clk)
1014 {
1015 return NULL;
1016 }
1017
clk_get_sys(const char * dev_id,const char * con_id)1018 static inline struct clk *clk_get_sys(const char *dev_id, const char *con_id)
1019 {
1020 return NULL;
1021 }
1022
clk_save_context(void)1023 static inline int clk_save_context(void)
1024 {
1025 return 0;
1026 }
1027
clk_restore_context(void)1028 static inline void clk_restore_context(void) {}
1029
1030 #endif
1031
1032 /* clk_prepare_enable helps cases using clk_enable in non-atomic context. */
clk_prepare_enable(struct clk * clk)1033 static inline int clk_prepare_enable(struct clk *clk)
1034 {
1035 int ret;
1036
1037 ret = clk_prepare(clk);
1038 if (ret)
1039 return ret;
1040 ret = clk_enable(clk);
1041 if (ret)
1042 clk_unprepare(clk);
1043
1044 return ret;
1045 }
1046
1047 /* clk_disable_unprepare helps cases using clk_disable in non-atomic context. */
clk_disable_unprepare(struct clk * clk)1048 static inline void clk_disable_unprepare(struct clk *clk)
1049 {
1050 clk_disable(clk);
1051 clk_unprepare(clk);
1052 }
1053
1054 static inline int __must_check
clk_bulk_prepare_enable(int num_clks,const struct clk_bulk_data * clks)1055 clk_bulk_prepare_enable(int num_clks, const struct clk_bulk_data *clks)
1056 {
1057 int ret;
1058
1059 ret = clk_bulk_prepare(num_clks, clks);
1060 if (ret)
1061 return ret;
1062 ret = clk_bulk_enable(num_clks, clks);
1063 if (ret)
1064 clk_bulk_unprepare(num_clks, clks);
1065
1066 return ret;
1067 }
1068
clk_bulk_disable_unprepare(int num_clks,const struct clk_bulk_data * clks)1069 static inline void clk_bulk_disable_unprepare(int num_clks,
1070 const struct clk_bulk_data *clks)
1071 {
1072 clk_bulk_disable(num_clks, clks);
1073 clk_bulk_unprepare(num_clks, clks);
1074 }
1075
1076 /**
1077 * clk_get_optional - lookup and obtain a reference to an optional clock
1078 * producer.
1079 * @dev: device for clock "consumer"
1080 * @id: clock consumer ID
1081 *
1082 * Behaves the same as clk_get() except where there is no clock producer. In
1083 * this case, instead of returning -ENOENT, the function returns NULL.
1084 */
clk_get_optional(struct device * dev,const char * id)1085 static inline struct clk *clk_get_optional(struct device *dev, const char *id)
1086 {
1087 struct clk *clk = clk_get(dev, id);
1088
1089 if (clk == ERR_PTR(-ENOENT))
1090 return NULL;
1091
1092 return clk;
1093 }
1094
1095 #if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK)
1096 struct clk *of_clk_get(struct device_node *np, int index);
1097 struct clk *of_clk_get_by_name(struct device_node *np, const char *name);
1098 struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec);
1099 #else
of_clk_get(struct device_node * np,int index)1100 static inline struct clk *of_clk_get(struct device_node *np, int index)
1101 {
1102 return ERR_PTR(-ENOENT);
1103 }
of_clk_get_by_name(struct device_node * np,const char * name)1104 static inline struct clk *of_clk_get_by_name(struct device_node *np,
1105 const char *name)
1106 {
1107 return ERR_PTR(-ENOENT);
1108 }
of_clk_get_from_provider(struct of_phandle_args * clkspec)1109 static inline struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
1110 {
1111 return ERR_PTR(-ENOENT);
1112 }
1113 #endif
1114
1115 #endif
1116