a8225eec72af39c5edf2d8220ce09993c17ac3b8
[platform/kernel/u-boot.git] / include / clk.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (c) 2015 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  * Copyright (c) 2016, NVIDIA CORPORATION.
6  */
7
8 #ifndef _CLK_H_
9 #define _CLK_H_
10
11 #include <dm/ofnode.h>
12 #include <linux/err.h>
13 #include <linux/errno.h>
14 #include <linux/types.h>
15
16 /**
17  * A clock is a hardware signal that oscillates autonomously at a specific
18  * frequency and duty cycle. Most hardware modules require one or more clock
19  * signal to drive their operation. Clock signals are typically generated
20  * externally to the HW module consuming them, by an entity this API calls a
21  * clock provider. This API provides a standard means for drivers to enable and
22  * disable clocks, and to set the rate at which they oscillate.
23  *
24  * A driver that implements UCLASS_CLK is a clock provider. A provider will
25  * often implement multiple separate clocks, since the hardware it manages
26  * often has this capability. clk-uclass.h describes the interface which
27  * clock providers must implement.
28  *
29  * Clock consumers/clients are the HW modules driven by the clock signals. This
30  * header file describes the API used by drivers for those HW modules.
31  */
32
33 struct udevice;
34
35 /**
36  * struct clk - A handle to (allowing control of) a single clock.
37  *
38  * Clients provide storage for clock handles. The content of the structure is
39  * managed solely by the clock API and clock drivers. A clock struct is
40  * initialized by "get"ing the clock struct. The clock struct is passed to all
41  * other clock APIs to identify which clock signal to operate upon.
42  *
43  * @dev: The device which implements the clock signal.
44  * @rate: The clock rate (in HZ).
45  * @flags: Flags used across common clock structure (e.g. CLK_)
46  *         Clock IP blocks specific flags (i.e. mux, div, gate, etc) are defined
47  *         in struct's for those devices (e.g. struct clk_mux).
48  * @id: The clock signal ID within the provider.
49  * @data: An optional data field for scenarios where a single integer ID is not
50  *        sufficient. If used, it can be populated through an .of_xlate op and
51  *        processed during the various clock ops.
52  *
53  * Should additional information to identify and configure any clock signal
54  * for any provider be required in the future, the struct could be expanded to
55  * either (a) add more fields to allow clock providers to store additional
56  * information, or (b) replace the id field with an opaque pointer, which the
57  * provider would dynamically allocated during its .of_xlate op, and process
58  * during is .request op. This may require the addition of an extra op to clean
59  * up the allocation.
60  */
61 struct clk {
62         struct udevice *dev;
63         long long rate; /* in HZ */
64         u32 flags;
65         int enable_count;
66         /*
67          * Written by of_xlate. In the future, we might add more fields here.
68          */
69         unsigned long id;
70         unsigned long data;
71 };
72
73 /**
74  * struct clk_bulk - A handle to (allowing control of) a bulk of clocks.
75  *
76  * Clients provide storage for the clock bulk. The content of the structure is
77  * managed solely by the clock API. A clock bulk struct is
78  * initialized by "get"ing the clock bulk struct.
79  * The clock bulk struct is passed to all other bulk clock APIs to apply
80  * the API to all the clock in the bulk struct.
81  *
82  * @clks: An array of clock handles.
83  * @count: The number of clock handles in the clks array.
84  */
85 struct clk_bulk {
86         struct clk *clks;
87         unsigned int count;
88 };
89
90 #if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(CLK)
91 struct phandle_1_arg;
92 /**
93  * clk_get_by_phandle() - Get a clock by its phandle information (of-platadata)
94  *
95  * This function is used when of-platdata is enabled.
96  *
97  * This looks up a clock using the phandle info. With dtoc, each phandle in the
98  * 'clocks' property is transformed into an idx representing the device. For
99  * example:
100  *
101  *      clocks = <&dpll_mpu_ck 23>;
102  *
103  * might result in:
104  *
105  *      .clocks = {1, {23}},},
106  *
107  * indicating that the clock is udevice idx 1 in dt-plat.c with an argument of
108  * 23. This function can return a valid clock given the above information. In
109  * this example it would return a clock containing the 'dpll_mpu_ck' device and
110  * the clock ID 23.
111  *
112  * @dev: Device containing the phandle
113  * @cells: Phandle info
114  * @clock: A pointer to a clock struct to initialise
115  * Return: 0 if OK, or a negative error code.
116  */
117 int clk_get_by_phandle(struct udevice *dev, const struct phandle_1_arg *cells,
118                        struct clk *clk);
119
120 /**
121  * clk_get_by_index() - Get/request a clock by integer index.
122  *
123  * This looks up and requests a clock. The index is relative to the client
124  * device; each device is assumed to have n clocks associated with it somehow,
125  * and this function finds and requests one of them. The mapping of client
126  * device clock indices to provider clocks may be via device-tree properties,
127  * board-provided mapping tables, or some other mechanism.
128  *
129  * @dev:        The client device.
130  * @index:      The index of the clock to request, within the client's list of
131  *              clocks.
132  * @clock       A pointer to a clock struct to initialize.
133  * Return: 0 if OK, or a negative error code.
134  */
135 int clk_get_by_index(struct udevice *dev, int index, struct clk *clk);
136
137 /**
138  * clk_get_by_index_nodev - Get/request a clock by integer index
139  * without a device.
140  *
141  * This is a version of clk_get_by_index() that does not use a device.
142  *
143  * @node:       The client ofnode.
144  * @index:      The index of the clock to request, within the client's list of
145  *              clocks.
146  * @clock       A pointer to a clock struct to initialize.
147  * Return: 0 if OK, or a negative error code.
148  */
149 int clk_get_by_index_nodev(ofnode node, int index, struct clk *clk);
150
151 /**
152  * clk_get_bulk - Get/request all clocks of a device.
153  *
154  * This looks up and requests all clocks of the client device; each device is
155  * assumed to have n clocks associated with it somehow, and this function finds
156  * and requests all of them in a separate structure. The mapping of client
157  * device clock indices to provider clocks may be via device-tree properties,
158  * board-provided mapping tables, or some other mechanism.
159  *
160  * @dev:        The client device.
161  * @bulk        A pointer to a clock bulk struct to initialize.
162  * Return: 0 if OK, or a negative error code.
163  */
164 int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk);
165
166 /**
167  * clk_get_by_name - Get/request a clock by name.
168  *
169  * This looks up and requests a clock. The name is relative to the client
170  * device; each device is assumed to have n clocks associated with it somehow,
171  * and this function finds and requests one of them. The mapping of client
172  * device clock names to provider clocks may be via device-tree properties,
173  * board-provided mapping tables, or some other mechanism.
174  *
175  * @dev:        The client device.
176  * @name:       The name of the clock to request, within the client's list of
177  *              clocks.
178  * @clock:      A pointer to a clock struct to initialize.
179  * Return: 0 if OK, or a negative error code.
180  */
181 int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk);
182
183 /**
184  * clk_get_by_name_nodev - Get/request a clock by name without a device.
185  *
186  * This is a version of clk_get_by_name() that does not use a device.
187  *
188  * @node:       The client ofnode.
189  * @name:       The name of the clock to request, within the client's list of
190  *              clocks.
191  * @clock:      A pointer to a clock struct to initialize.
192  * Return: 0 if OK, or a negative error code.
193  */
194 int clk_get_by_name_nodev(ofnode node, const char *name, struct clk *clk);
195
196 /**
197  * clk_get_by_name_nodev_optional - Get/request an optinonal clock by name
198  *              without a device.
199  * @node:       The client ofnode.
200  * @name:       The name of the clock to request.
201  * @name:       The name of the clock to request, within the client's list of
202  *              clocks.
203  * @clock:      A pointer to a clock struct to initialize.
204  *
205  * Behaves the same as clk_get_by_name_nodev() except where there is
206  * no clock producer, in this case, skip the error number -ENODATA, and
207  * the function returns 0.
208  */
209 int clk_get_by_name_nodev_optional(ofnode node, const char *name,
210                                    struct clk *clk);
211
212 /**
213  * devm_clk_get - lookup and obtain a managed reference to a clock producer.
214  * @dev: device for clock "consumer"
215  * @id: clock consumer ID
216  *
217  * Returns a struct clk corresponding to the clock producer, or
218  * valid IS_ERR() condition containing errno.  The implementation
219  * uses @dev and @id to determine the clock consumer, and thereby
220  * the clock producer.  (IOW, @id may be identical strings, but
221  * clk_get may return different clock producers depending on @dev.)
222  *
223  * Drivers must assume that the clock source is not enabled.
224  *
225  * devm_clk_get should not be called from within interrupt context.
226  *
227  * The clock will automatically be freed when the device is unbound
228  * from the bus.
229  */
230 struct clk *devm_clk_get(struct udevice *dev, const char *id);
231
232 /**
233  * devm_clk_get_optional - lookup and obtain a managed reference to an optional
234  *                         clock producer.
235  * @dev: device for clock "consumer"
236  * @id: clock consumer ID
237  *
238  * Behaves the same as devm_clk_get() except where there is no clock producer.
239  * In this case, instead of returning -ENOENT, the function returns NULL.
240  */
241 struct clk *devm_clk_get_optional(struct udevice *dev, const char *id);
242
243 /**
244  * clk_release_all() - Disable (turn off)/Free an array of previously
245  * requested clocks.
246  *
247  * For each clock contained in the clock array, this function will check if
248  * clock has been previously requested and then will disable and free it.
249  *
250  * @clk:        A clock struct array that was previously successfully
251  *              requested by clk_request/get_by_*().
252  * @count       Number of clock contained in the array
253  * Return: zero on success, or -ve error code.
254  */
255 int clk_release_all(struct clk *clk, int count);
256
257 /**
258  * devm_clk_put - "free" a managed clock source
259  * @dev: device used to acquire the clock
260  * @clk: clock source acquired with devm_clk_get()
261  *
262  * Note: drivers must ensure that all clk_enable calls made on this
263  * clock source are balanced by clk_disable calls prior to calling
264  * this function.
265  *
266  * clk_put should not be called from within interrupt context.
267  */
268 void devm_clk_put(struct udevice *dev, struct clk *clk);
269
270 #else
271 static inline int clk_get_by_index(struct udevice *dev, int index,
272                                    struct clk *clk)
273 {
274         return -ENOSYS;
275 }
276
277 static inline int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk)
278 {
279         return -ENOSYS;
280 }
281
282 static inline int clk_get_by_name(struct udevice *dev, const char *name,
283                            struct clk *clk)
284 {
285         return -ENOSYS;
286 }
287
288 static inline int
289 clk_get_by_name_nodev(ofnode node, const char *name, struct clk *clk)
290 {
291         return -ENOSYS;
292 }
293
294 static inline int
295 clk_get_by_name_nodev_optional(ofnode node, const char *name, struct clk *clk)
296 {
297         return -ENOSYS;
298 }
299
300 static inline int clk_release_all(struct clk *clk, int count)
301 {
302         return -ENOSYS;
303 }
304 #endif
305
306 /**
307  * enum clk_defaults_stage - What stage clk_set_defaults() is called at
308  * @CLK_DEFAULTS_PRE: Called before probe. Setting of defaults for clocks owned
309  *                    by this clock driver will be defered until after probing.
310  * @CLK_DEFAULTS_POST: Called after probe. Only defaults for clocks owned by
311  *                     this clock driver will be set.
312  * @CLK_DEFAULTS_POST_FORCE: Called after probe, and always set defaults, even
313  *                           before relocation. Usually, defaults are not set
314  *                           pre-relocation to avoid setting them twice (when
315  *                           the device is probed again post-relocation). This
316  *                           may incur a performance cost as device tree
317  *                           properties must be parsed for a second time.
318  *                           However, when not using SPL, pre-relocation may be
319  *                           the only time we can set defaults for some clocks
320  *                           (such as those used for the RAM we will relocate
321  *                           into).
322  */
323 enum clk_defaults_stage {
324         CLK_DEFAULTS_PRE = 0,
325         CLK_DEFAULTS_POST = 1,
326         CLK_DEFAULTS_POST_FORCE,
327 };
328
329 #if CONFIG_IS_ENABLED(OF_REAL) && CONFIG_IS_ENABLED(CLK)
330 /**
331  * clk_set_defaults - Process 'assigned-{clocks/clock-parents/clock-rates}'
332  *                    properties to configure clocks
333  *
334  * @dev:        A device to process (the ofnode associated with this device
335  *              will be processed).
336  * @stage:      The stage of the probing process this function is called during.
337  */
338 int clk_set_defaults(struct udevice *dev, enum clk_defaults_stage stage);
339 #else
340 static inline int clk_set_defaults(struct udevice *dev, int stage)
341 {
342         return 0;
343 }
344 #endif
345
346 /**
347  * clk_release_bulk() - Disable (turn off)/Free an array of previously
348  * requested clocks in a clock bulk struct.
349  *
350  * For each clock contained in the clock bulk struct, this function will check
351  * if clock has been previously requested and then will disable and free it.
352  *
353  * @clk:        A clock bulk struct that was previously successfully
354  *              requested by clk_get_bulk().
355  * Return: zero on success, or -ve error code.
356  */
357 static inline int clk_release_bulk(struct clk_bulk *bulk)
358 {
359         return clk_release_all(bulk->clks, bulk->count);
360 }
361
362 #if CONFIG_IS_ENABLED(CLK)
363 /**
364  * clk_request - Request a clock by provider-specific ID.
365  *
366  * This requests a clock using a provider-specific ID. Generally, this function
367  * should not be used, since clk_get_by_index/name() provide an interface that
368  * better separates clients from intimate knowledge of clock providers.
369  * However, this function may be useful in core SoC-specific code.
370  *
371  * @dev:        The clock provider device.
372  * @clock:      A pointer to a clock struct to initialize. The caller must
373  *              have already initialized any field in this struct which the
374  *              clock provider uses to identify the clock.
375  * Return: 0 if OK, or a negative error code.
376  */
377 int clk_request(struct udevice *dev, struct clk *clk);
378
379 /**
380  * clk_free - Free a previously requested clock.
381  *
382  * @clock:      A clock struct that was previously successfully requested by
383  *              clk_request/get_by_*().
384  * Return: 0 if OK, or a negative error code.
385  */
386 int clk_free(struct clk *clk);
387
388 /**
389  * clk_get_rate() - Get current clock rate.
390  *
391  * @clk:        A clock struct that was previously successfully requested by
392  *              clk_request/get_by_*().
393  * Return: clock rate in Hz on success, 0 for invalid clock, or -ve error code
394  *         for other errors.
395  */
396 ulong clk_get_rate(struct clk *clk);
397
398 /**
399  * clk_get_parent() - Get current clock's parent.
400  *
401  * @clk:        A clock struct that was previously successfully requested by
402  *              clk_request/get_by_*().
403  * Return: pointer to parent's struct clk, or error code passed as pointer
404  */
405 struct clk *clk_get_parent(struct clk *clk);
406
407 /**
408  * clk_get_parent_rate() - Get parent of current clock rate.
409  *
410  * @clk:        A clock struct that was previously successfully requested by
411  *              clk_request/get_by_*().
412  * Return: clock rate in Hz, or -ve error code.
413  */
414 long long clk_get_parent_rate(struct clk *clk);
415
416 /**
417  * clk_round_rate() - Adjust a rate to the exact rate a clock can provide
418  *
419  * This answers the question "if I were to pass @rate to clk_set_rate(),
420  * what clock rate would I end up with?" without changing the hardware
421  * in any way.  In other words:
422  *
423  *   rate = clk_round_rate(clk, r);
424  *
425  * and:
426  *
427  *   rate = clk_set_rate(clk, r);
428  *
429  * are equivalent except the former does not modify the clock hardware
430  * in any way.
431  *
432  * @clk: A clock struct that was previously successfully requested by
433  *       clk_request/get_by_*().
434  * @rate: desired clock rate in Hz.
435  * Return: rounded rate in Hz, or -ve error code.
436  */
437 ulong clk_round_rate(struct clk *clk, ulong rate);
438
439 /**
440  * clk_set_rate() - Set current clock rate.
441  *
442  * @clk:        A clock struct that was previously successfully requested by
443  *              clk_request/get_by_*().
444  * @rate:       New clock rate in Hz.
445  * Return: new rate, or -ve error code.
446  */
447 ulong clk_set_rate(struct clk *clk, ulong rate);
448
449 /**
450  * clk_set_parent() - Set current clock parent.
451  *
452  * @clk:        A clock struct that was previously successfully requested by
453  *              clk_request/get_by_*().
454  * @parent:     A clock struct that was previously successfully requested by
455  *              clk_request/get_by_*().
456  * Return: new rate, or -ve error code.
457  */
458 int clk_set_parent(struct clk *clk, struct clk *parent);
459
460 /**
461  * clk_enable() - Enable (turn on) a clock.
462  *
463  * @clk:        A clock struct that was previously successfully requested by
464  *              clk_request/get_by_*().
465  * Return: zero on success, or -ve error code.
466  */
467 int clk_enable(struct clk *clk);
468
469 /**
470  * clk_enable_bulk() - Enable (turn on) all clocks in a clock bulk struct.
471  *
472  * @bulk:       A clock bulk struct that was previously successfully requested
473  *              by clk_get_bulk().
474  * Return: zero on success, or -ve error code.
475  */
476 int clk_enable_bulk(struct clk_bulk *bulk);
477
478 /**
479  * clk_disable() - Disable (turn off) a clock.
480  *
481  * @clk:        A clock struct that was previously successfully requested by
482  *              clk_request/get_by_*().
483  * Return: zero on success, or -ve error code.
484  */
485 int clk_disable(struct clk *clk);
486
487 /**
488  * clk_disable_bulk() - Disable (turn off) all clocks in a clock bulk struct.
489  *
490  * @bulk:       A clock bulk struct that was previously successfully requested
491  *              by clk_get_bulk().
492  * Return: zero on success, or -ve error code.
493  */
494 int clk_disable_bulk(struct clk_bulk *bulk);
495
496 /**
497  * clk_is_match - check if two clk's point to the same hardware clock
498  * @p: clk compared against q
499  * @q: clk compared against p
500  *
501  * Returns true if the two struct clk pointers both point to the same hardware
502  * clock node.
503  *
504  * Returns false otherwise. Note that two NULL clks are treated as matching.
505  */
506 bool clk_is_match(const struct clk *p, const struct clk *q);
507
508 /**
509  * clk_get_by_id() - Get the clock by its ID
510  *
511  * @id: The clock ID to search for
512  *
513  * @clkp:       A pointer to clock struct that has been found among added clocks
514  *              to UCLASS_CLK
515  * Return: zero on success, or -ENOENT on error
516  */
517 int clk_get_by_id(ulong id, struct clk **clkp);
518
519 /**
520  * clk_dev_binded() - Check whether the clk has a device binded
521  *
522  * @clk         A pointer to the clk
523  *
524  * Return: true on binded, or false on no
525  */
526 bool clk_dev_binded(struct clk *clk);
527
528 #else /* CONFIG_IS_ENABLED(CLK) */
529
530 static inline int clk_request(struct udevice *dev, struct clk *clk)
531 {
532         return -ENOSYS;
533 }
534
535 static inline int clk_free(struct clk *clk)
536 {
537         return 0;
538 }
539
540 static inline ulong clk_get_rate(struct clk *clk)
541 {
542         return -ENOSYS;
543 }
544
545 static inline struct clk *clk_get_parent(struct clk *clk)
546 {
547         return ERR_PTR(-ENOSYS);
548 }
549
550 static inline long long clk_get_parent_rate(struct clk *clk)
551 {
552         return -ENOSYS;
553 }
554
555 static inline ulong clk_round_rate(struct clk *clk, ulong rate)
556 {
557         return -ENOSYS;
558 }
559
560 static inline ulong clk_set_rate(struct clk *clk, ulong rate)
561 {
562         return -ENOSYS;
563 }
564
565 static inline int clk_set_parent(struct clk *clk, struct clk *parent)
566 {
567         return -ENOSYS;
568 }
569
570 static inline int clk_enable(struct clk *clk)
571 {
572         return 0;
573 }
574
575 static inline int clk_enable_bulk(struct clk_bulk *bulk)
576 {
577         return 0;
578 }
579
580 static inline int clk_disable(struct clk *clk)
581 {
582         return 0;
583 }
584
585 static inline int clk_disable_bulk(struct clk_bulk *bulk)
586 {
587         return 0;
588 }
589
590 static inline bool clk_is_match(const struct clk *p, const struct clk *q)
591 {
592         return false;
593 }
594
595 static inline int clk_get_by_id(ulong id, struct clk **clkp)
596 {
597         return -ENOSYS;
598 }
599
600 static inline bool clk_dev_binded(struct clk *clk)
601 {
602         return false;
603 }
604 #endif /* CONFIG_IS_ENABLED(CLK) */
605
606 /**
607  * clk_valid() - check if clk is valid
608  *
609  * @clk:        the clock to check
610  * Return: true if valid, or false
611  */
612 static inline bool clk_valid(struct clk *clk)
613 {
614         return clk && !!clk->dev;
615 }
616
617 int soc_clk_dump(void);
618
619 #endif
620
621 #define clk_prepare_enable(clk) clk_enable(clk)
622 #define clk_disable_unprepare(clk) clk_disable(clk)