clk: Allow parents to be specified without string names
[linux-2.6-microblaze.git] / include / linux / clk-provider.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  *  Copyright (c) 2010-2011 Jeremy Kerr <jeremy.kerr@canonical.com>
4  *  Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
5  */
6 #ifndef __LINUX_CLK_PROVIDER_H
7 #define __LINUX_CLK_PROVIDER_H
8
9 #include <linux/io.h>
10 #include <linux/of.h>
11 #include <linux/of_clk.h>
12
13 #ifdef CONFIG_COMMON_CLK
14
15 /*
16  * flags used across common struct clk.  these flags should only affect the
17  * top-level framework.  custom flags for dealing with hardware specifics
18  * belong in struct clk_foo
19  *
20  * Please update clk_flags[] in drivers/clk/clk.c when making changes here!
21  */
22 #define CLK_SET_RATE_GATE       BIT(0) /* must be gated across rate change */
23 #define CLK_SET_PARENT_GATE     BIT(1) /* must be gated across re-parent */
24 #define CLK_SET_RATE_PARENT     BIT(2) /* propagate rate change up one level */
25 #define CLK_IGNORE_UNUSED       BIT(3) /* do not gate even if unused */
26                                 /* unused */
27 #define CLK_IS_BASIC            BIT(5) /* Basic clk, can't do a to_clk_foo() */
28 #define CLK_GET_RATE_NOCACHE    BIT(6) /* do not use the cached clk rate */
29 #define CLK_SET_RATE_NO_REPARENT BIT(7) /* don't re-parent on rate change */
30 #define CLK_GET_ACCURACY_NOCACHE BIT(8) /* do not use the cached clk accuracy */
31 #define CLK_RECALC_NEW_RATES    BIT(9) /* recalc rates after notifications */
32 #define CLK_SET_RATE_UNGATE     BIT(10) /* clock needs to run to set rate */
33 #define CLK_IS_CRITICAL         BIT(11) /* do not gate, ever */
34 /* parents need enable during gate/ungate, set rate and re-parent */
35 #define CLK_OPS_PARENT_ENABLE   BIT(12)
36 /* duty cycle call may be forwarded to the parent clock */
37 #define CLK_DUTY_CYCLE_PARENT   BIT(13)
38
39 struct clk;
40 struct clk_hw;
41 struct clk_core;
42 struct dentry;
43
44 /**
45  * struct clk_rate_request - Structure encoding the clk constraints that
46  * a clock user might require.
47  *
48  * @rate:               Requested clock rate. This field will be adjusted by
49  *                      clock drivers according to hardware capabilities.
50  * @min_rate:           Minimum rate imposed by clk users.
51  * @max_rate:           Maximum rate imposed by clk users.
52  * @best_parent_rate:   The best parent rate a parent can provide to fulfill the
53  *                      requested constraints.
54  * @best_parent_hw:     The most appropriate parent clock that fulfills the
55  *                      requested constraints.
56  *
57  */
58 struct clk_rate_request {
59         unsigned long rate;
60         unsigned long min_rate;
61         unsigned long max_rate;
62         unsigned long best_parent_rate;
63         struct clk_hw *best_parent_hw;
64 };
65
66 /**
67  * struct clk_duty - Struture encoding the duty cycle ratio of a clock
68  *
69  * @num:        Numerator of the duty cycle ratio
70  * @den:        Denominator of the duty cycle ratio
71  */
72 struct clk_duty {
73         unsigned int num;
74         unsigned int den;
75 };
76
77 /**
78  * struct clk_ops -  Callback operations for hardware clocks; these are to
79  * be provided by the clock implementation, and will be called by drivers
80  * through the clk_* api.
81  *
82  * @prepare:    Prepare the clock for enabling. This must not return until
83  *              the clock is fully prepared, and it's safe to call clk_enable.
84  *              This callback is intended to allow clock implementations to
85  *              do any initialisation that may sleep. Called with
86  *              prepare_lock held.
87  *
88  * @unprepare:  Release the clock from its prepared state. This will typically
89  *              undo any work done in the @prepare callback. Called with
90  *              prepare_lock held.
91  *
92  * @is_prepared: Queries the hardware to determine if the clock is prepared.
93  *              This function is allowed to sleep. Optional, if this op is not
94  *              set then the prepare count will be used.
95  *
96  * @unprepare_unused: Unprepare the clock atomically.  Only called from
97  *              clk_disable_unused for prepare clocks with special needs.
98  *              Called with prepare mutex held. This function may sleep.
99  *
100  * @enable:     Enable the clock atomically. This must not return until the
101  *              clock is generating a valid clock signal, usable by consumer
102  *              devices. Called with enable_lock held. This function must not
103  *              sleep.
104  *
105  * @disable:    Disable the clock atomically. Called with enable_lock held.
106  *              This function must not sleep.
107  *
108  * @is_enabled: Queries the hardware to determine if the clock is enabled.
109  *              This function must not sleep. Optional, if this op is not
110  *              set then the enable count will be used.
111  *
112  * @disable_unused: Disable the clock atomically.  Only called from
113  *              clk_disable_unused for gate clocks with special needs.
114  *              Called with enable_lock held.  This function must not
115  *              sleep.
116  *
117  * @save_context: Save the context of the clock in prepration for poweroff.
118  *
119  * @restore_context: Restore the context of the clock after a restoration
120  *              of power.
121  *
122  * @recalc_rate Recalculate the rate of this clock, by querying hardware. The
123  *              parent rate is an input parameter.  It is up to the caller to
124  *              ensure that the prepare_mutex is held across this call.
125  *              Returns the calculated rate.  Optional, but recommended - if
126  *              this op is not set then clock rate will be initialized to 0.
127  *
128  * @round_rate: Given a target rate as input, returns the closest rate actually
129  *              supported by the clock. The parent rate is an input/output
130  *              parameter.
131  *
132  * @determine_rate: Given a target rate as input, returns the closest rate
133  *              actually supported by the clock, and optionally the parent clock
134  *              that should be used to provide the clock rate.
135  *
136  * @set_parent: Change the input source of this clock; for clocks with multiple
137  *              possible parents specify a new parent by passing in the index
138  *              as a u8 corresponding to the parent in either the .parent_names
139  *              or .parents arrays.  This function in affect translates an
140  *              array index into the value programmed into the hardware.
141  *              Returns 0 on success, -EERROR otherwise.
142  *
143  * @get_parent: Queries the hardware to determine the parent of a clock.  The
144  *              return value is a u8 which specifies the index corresponding to
145  *              the parent clock.  This index can be applied to either the
146  *              .parent_names or .parents arrays.  In short, this function
147  *              translates the parent value read from hardware into an array
148  *              index.  Currently only called when the clock is initialized by
149  *              __clk_init.  This callback is mandatory for clocks with
150  *              multiple parents.  It is optional (and unnecessary) for clocks
151  *              with 0 or 1 parents.
152  *
153  * @set_rate:   Change the rate of this clock. The requested rate is specified
154  *              by the second argument, which should typically be the return
155  *              of .round_rate call.  The third argument gives the parent rate
156  *              which is likely helpful for most .set_rate implementation.
157  *              Returns 0 on success, -EERROR otherwise.
158  *
159  * @set_rate_and_parent: Change the rate and the parent of this clock. The
160  *              requested rate is specified by the second argument, which
161  *              should typically be the return of .round_rate call.  The
162  *              third argument gives the parent rate which is likely helpful
163  *              for most .set_rate_and_parent implementation. The fourth
164  *              argument gives the parent index. This callback is optional (and
165  *              unnecessary) for clocks with 0 or 1 parents as well as
166  *              for clocks that can tolerate switching the rate and the parent
167  *              separately via calls to .set_parent and .set_rate.
168  *              Returns 0 on success, -EERROR otherwise.
169  *
170  * @recalc_accuracy: Recalculate the accuracy of this clock. The clock accuracy
171  *              is expressed in ppb (parts per billion). The parent accuracy is
172  *              an input parameter.
173  *              Returns the calculated accuracy.  Optional - if this op is not
174  *              set then clock accuracy will be initialized to parent accuracy
175  *              or 0 (perfect clock) if clock has no parent.
176  *
177  * @get_phase:  Queries the hardware to get the current phase of a clock.
178  *              Returned values are 0-359 degrees on success, negative
179  *              error codes on failure.
180  *
181  * @set_phase:  Shift the phase this clock signal in degrees specified
182  *              by the second argument. Valid values for degrees are
183  *              0-359. Return 0 on success, otherwise -EERROR.
184  *
185  * @get_duty_cycle: Queries the hardware to get the current duty cycle ratio
186  *              of a clock. Returned values denominator cannot be 0 and must be
187  *              superior or equal to the numerator.
188  *
189  * @set_duty_cycle: Apply the duty cycle ratio to this clock signal specified by
190  *              the numerator (2nd argurment) and denominator (3rd  argument).
191  *              Argument must be a valid ratio (denominator > 0
192  *              and >= numerator) Return 0 on success, otherwise -EERROR.
193  *
194  * @init:       Perform platform-specific initialization magic.
195  *              This is not not used by any of the basic clock types.
196  *              Please consider other ways of solving initialization problems
197  *              before using this callback, as its use is discouraged.
198  *
199  * @debug_init: Set up type-specific debugfs entries for this clock.  This
200  *              is called once, after the debugfs directory entry for this
201  *              clock has been created.  The dentry pointer representing that
202  *              directory is provided as an argument.  Called with
203  *              prepare_lock held.  Returns 0 on success, -EERROR otherwise.
204  *
205  *
206  * The clk_enable/clk_disable and clk_prepare/clk_unprepare pairs allow
207  * implementations to split any work between atomic (enable) and sleepable
208  * (prepare) contexts.  If enabling a clock requires code that might sleep,
209  * this must be done in clk_prepare.  Clock enable code that will never be
210  * called in a sleepable context may be implemented in clk_enable.
211  *
212  * Typically, drivers will call clk_prepare when a clock may be needed later
213  * (eg. when a device is opened), and clk_enable when the clock is actually
214  * required (eg. from an interrupt). Note that clk_prepare MUST have been
215  * called before clk_enable.
216  */
217 struct clk_ops {
218         int             (*prepare)(struct clk_hw *hw);
219         void            (*unprepare)(struct clk_hw *hw);
220         int             (*is_prepared)(struct clk_hw *hw);
221         void            (*unprepare_unused)(struct clk_hw *hw);
222         int             (*enable)(struct clk_hw *hw);
223         void            (*disable)(struct clk_hw *hw);
224         int             (*is_enabled)(struct clk_hw *hw);
225         void            (*disable_unused)(struct clk_hw *hw);
226         int             (*save_context)(struct clk_hw *hw);
227         void            (*restore_context)(struct clk_hw *hw);
228         unsigned long   (*recalc_rate)(struct clk_hw *hw,
229                                         unsigned long parent_rate);
230         long            (*round_rate)(struct clk_hw *hw, unsigned long rate,
231                                         unsigned long *parent_rate);
232         int             (*determine_rate)(struct clk_hw *hw,
233                                           struct clk_rate_request *req);
234         int             (*set_parent)(struct clk_hw *hw, u8 index);
235         u8              (*get_parent)(struct clk_hw *hw);
236         int             (*set_rate)(struct clk_hw *hw, unsigned long rate,
237                                     unsigned long parent_rate);
238         int             (*set_rate_and_parent)(struct clk_hw *hw,
239                                     unsigned long rate,
240                                     unsigned long parent_rate, u8 index);
241         unsigned long   (*recalc_accuracy)(struct clk_hw *hw,
242                                            unsigned long parent_accuracy);
243         int             (*get_phase)(struct clk_hw *hw);
244         int             (*set_phase)(struct clk_hw *hw, int degrees);
245         int             (*get_duty_cycle)(struct clk_hw *hw,
246                                           struct clk_duty *duty);
247         int             (*set_duty_cycle)(struct clk_hw *hw,
248                                           struct clk_duty *duty);
249         void            (*init)(struct clk_hw *hw);
250         void            (*debug_init)(struct clk_hw *hw, struct dentry *dentry);
251 };
252
253 /**
254  * struct clk_parent_data - clk parent information
255  * @hw: parent clk_hw pointer (used for clk providers with internal clks)
256  * @fw_name: parent name local to provider registering clk
257  * @name: globally unique parent name (used as a fallback)
258  */
259 struct clk_parent_data {
260         const struct clk_hw     *hw;
261         const char              *fw_name;
262         const char              *name;
263 };
264
265 /**
266  * struct clk_init_data - holds init data that's common to all clocks and is
267  * shared between the clock provider and the common clock framework.
268  *
269  * @name: clock name
270  * @ops: operations this clock supports
271  * @parent_names: array of string names for all possible parents
272  * @parent_data: array of parent data for all possible parents (when some
273  *               parents are external to the clk controller)
274  * @parent_hws: array of pointers to all possible parents (when all parents
275  *              are internal to the clk controller)
276  * @num_parents: number of possible parents
277  * @flags: framework-level hints and quirks
278  */
279 struct clk_init_data {
280         const char              *name;
281         const struct clk_ops    *ops;
282         /* Only one of the following three should be assigned */
283         const char              * const *parent_names;
284         const struct clk_parent_data    *parent_data;
285         const struct clk_hw             **parent_hws;
286         u8                      num_parents;
287         unsigned long           flags;
288 };
289
290 /**
291  * struct clk_hw - handle for traversing from a struct clk to its corresponding
292  * hardware-specific structure.  struct clk_hw should be declared within struct
293  * clk_foo and then referenced by the struct clk instance that uses struct
294  * clk_foo's clk_ops
295  *
296  * @core: pointer to the struct clk_core instance that points back to this
297  * struct clk_hw instance
298  *
299  * @clk: pointer to the per-user struct clk instance that can be used to call
300  * into the clk API
301  *
302  * @init: pointer to struct clk_init_data that contains the init data shared
303  * with the common clock framework.
304  */
305 struct clk_hw {
306         struct clk_core *core;
307         struct clk *clk;
308         const struct clk_init_data *init;
309 };
310
311 /*
312  * DOC: Basic clock implementations common to many platforms
313  *
314  * Each basic clock hardware type is comprised of a structure describing the
315  * clock hardware, implementations of the relevant callbacks in struct clk_ops,
316  * unique flags for that hardware type, a registration function and an
317  * alternative macro for static initialization
318  */
319
320 /**
321  * struct clk_fixed_rate - fixed-rate clock
322  * @hw:         handle between common and hardware-specific interfaces
323  * @fixed_rate: constant frequency of clock
324  */
325 struct clk_fixed_rate {
326         struct          clk_hw hw;
327         unsigned long   fixed_rate;
328         unsigned long   fixed_accuracy;
329         u8              flags;
330 };
331
332 #define to_clk_fixed_rate(_hw) container_of(_hw, struct clk_fixed_rate, hw)
333
334 extern const struct clk_ops clk_fixed_rate_ops;
335 struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
336                 const char *parent_name, unsigned long flags,
337                 unsigned long fixed_rate);
338 struct clk_hw *clk_hw_register_fixed_rate(struct device *dev, const char *name,
339                 const char *parent_name, unsigned long flags,
340                 unsigned long fixed_rate);
341 struct clk *clk_register_fixed_rate_with_accuracy(struct device *dev,
342                 const char *name, const char *parent_name, unsigned long flags,
343                 unsigned long fixed_rate, unsigned long fixed_accuracy);
344 void clk_unregister_fixed_rate(struct clk *clk);
345 struct clk_hw *clk_hw_register_fixed_rate_with_accuracy(struct device *dev,
346                 const char *name, const char *parent_name, unsigned long flags,
347                 unsigned long fixed_rate, unsigned long fixed_accuracy);
348 void clk_hw_unregister_fixed_rate(struct clk_hw *hw);
349
350 void of_fixed_clk_setup(struct device_node *np);
351
352 /**
353  * struct clk_gate - gating clock
354  *
355  * @hw:         handle between common and hardware-specific interfaces
356  * @reg:        register controlling gate
357  * @bit_idx:    single bit controlling gate
358  * @flags:      hardware-specific flags
359  * @lock:       register lock
360  *
361  * Clock which can gate its output.  Implements .enable & .disable
362  *
363  * Flags:
364  * CLK_GATE_SET_TO_DISABLE - by default this clock sets the bit at bit_idx to
365  *      enable the clock.  Setting this flag does the opposite: setting the bit
366  *      disable the clock and clearing it enables the clock
367  * CLK_GATE_HIWORD_MASK - The gate settings are only in lower 16-bit
368  *      of this register, and mask of gate bits are in higher 16-bit of this
369  *      register.  While setting the gate bits, higher 16-bit should also be
370  *      updated to indicate changing gate bits.
371  */
372 struct clk_gate {
373         struct clk_hw hw;
374         void __iomem    *reg;
375         u8              bit_idx;
376         u8              flags;
377         spinlock_t      *lock;
378 };
379
380 #define to_clk_gate(_hw) container_of(_hw, struct clk_gate, hw)
381
382 #define CLK_GATE_SET_TO_DISABLE         BIT(0)
383 #define CLK_GATE_HIWORD_MASK            BIT(1)
384
385 extern const struct clk_ops clk_gate_ops;
386 struct clk *clk_register_gate(struct device *dev, const char *name,
387                 const char *parent_name, unsigned long flags,
388                 void __iomem *reg, u8 bit_idx,
389                 u8 clk_gate_flags, spinlock_t *lock);
390 struct clk_hw *clk_hw_register_gate(struct device *dev, const char *name,
391                 const char *parent_name, unsigned long flags,
392                 void __iomem *reg, u8 bit_idx,
393                 u8 clk_gate_flags, spinlock_t *lock);
394 void clk_unregister_gate(struct clk *clk);
395 void clk_hw_unregister_gate(struct clk_hw *hw);
396 int clk_gate_is_enabled(struct clk_hw *hw);
397
398 struct clk_div_table {
399         unsigned int    val;
400         unsigned int    div;
401 };
402
403 /**
404  * struct clk_divider - adjustable divider clock
405  *
406  * @hw:         handle between common and hardware-specific interfaces
407  * @reg:        register containing the divider
408  * @shift:      shift to the divider bit field
409  * @width:      width of the divider bit field
410  * @table:      array of value/divider pairs, last entry should have div = 0
411  * @lock:       register lock
412  *
413  * Clock with an adjustable divider affecting its output frequency.  Implements
414  * .recalc_rate, .set_rate and .round_rate
415  *
416  * Flags:
417  * CLK_DIVIDER_ONE_BASED - by default the divisor is the value read from the
418  *      register plus one.  If CLK_DIVIDER_ONE_BASED is set then the divider is
419  *      the raw value read from the register, with the value of zero considered
420  *      invalid, unless CLK_DIVIDER_ALLOW_ZERO is set.
421  * CLK_DIVIDER_POWER_OF_TWO - clock divisor is 2 raised to the value read from
422  *      the hardware register
423  * CLK_DIVIDER_ALLOW_ZERO - Allow zero divisors.  For dividers which have
424  *      CLK_DIVIDER_ONE_BASED set, it is possible to end up with a zero divisor.
425  *      Some hardware implementations gracefully handle this case and allow a
426  *      zero divisor by not modifying their input clock
427  *      (divide by one / bypass).
428  * CLK_DIVIDER_HIWORD_MASK - The divider settings are only in lower 16-bit
429  *      of this register, and mask of divider bits are in higher 16-bit of this
430  *      register.  While setting the divider bits, higher 16-bit should also be
431  *      updated to indicate changing divider bits.
432  * CLK_DIVIDER_ROUND_CLOSEST - Makes the best calculated divider to be rounded
433  *      to the closest integer instead of the up one.
434  * CLK_DIVIDER_READ_ONLY - The divider settings are preconfigured and should
435  *      not be changed by the clock framework.
436  * CLK_DIVIDER_MAX_AT_ZERO - For dividers which are like CLK_DIVIDER_ONE_BASED
437  *      except when the value read from the register is zero, the divisor is
438  *      2^width of the field.
439  */
440 struct clk_divider {
441         struct clk_hw   hw;
442         void __iomem    *reg;
443         u8              shift;
444         u8              width;
445         u8              flags;
446         const struct clk_div_table      *table;
447         spinlock_t      *lock;
448 };
449
450 #define clk_div_mask(width)     ((1 << (width)) - 1)
451 #define to_clk_divider(_hw) container_of(_hw, struct clk_divider, hw)
452
453 #define CLK_DIVIDER_ONE_BASED           BIT(0)
454 #define CLK_DIVIDER_POWER_OF_TWO        BIT(1)
455 #define CLK_DIVIDER_ALLOW_ZERO          BIT(2)
456 #define CLK_DIVIDER_HIWORD_MASK         BIT(3)
457 #define CLK_DIVIDER_ROUND_CLOSEST       BIT(4)
458 #define CLK_DIVIDER_READ_ONLY           BIT(5)
459 #define CLK_DIVIDER_MAX_AT_ZERO         BIT(6)
460
461 extern const struct clk_ops clk_divider_ops;
462 extern const struct clk_ops clk_divider_ro_ops;
463
464 unsigned long divider_recalc_rate(struct clk_hw *hw, unsigned long parent_rate,
465                 unsigned int val, const struct clk_div_table *table,
466                 unsigned long flags, unsigned long width);
467 long divider_round_rate_parent(struct clk_hw *hw, struct clk_hw *parent,
468                                unsigned long rate, unsigned long *prate,
469                                const struct clk_div_table *table,
470                                u8 width, unsigned long flags);
471 long divider_ro_round_rate_parent(struct clk_hw *hw, struct clk_hw *parent,
472                                   unsigned long rate, unsigned long *prate,
473                                   const struct clk_div_table *table, u8 width,
474                                   unsigned long flags, unsigned int val);
475 int divider_get_val(unsigned long rate, unsigned long parent_rate,
476                 const struct clk_div_table *table, u8 width,
477                 unsigned long flags);
478
479 struct clk *clk_register_divider(struct device *dev, const char *name,
480                 const char *parent_name, unsigned long flags,
481                 void __iomem *reg, u8 shift, u8 width,
482                 u8 clk_divider_flags, spinlock_t *lock);
483 struct clk_hw *clk_hw_register_divider(struct device *dev, const char *name,
484                 const char *parent_name, unsigned long flags,
485                 void __iomem *reg, u8 shift, u8 width,
486                 u8 clk_divider_flags, spinlock_t *lock);
487 struct clk *clk_register_divider_table(struct device *dev, const char *name,
488                 const char *parent_name, unsigned long flags,
489                 void __iomem *reg, u8 shift, u8 width,
490                 u8 clk_divider_flags, const struct clk_div_table *table,
491                 spinlock_t *lock);
492 struct clk_hw *clk_hw_register_divider_table(struct device *dev,
493                 const char *name, const char *parent_name, unsigned long flags,
494                 void __iomem *reg, u8 shift, u8 width,
495                 u8 clk_divider_flags, const struct clk_div_table *table,
496                 spinlock_t *lock);
497 void clk_unregister_divider(struct clk *clk);
498 void clk_hw_unregister_divider(struct clk_hw *hw);
499
500 /**
501  * struct clk_mux - multiplexer clock
502  *
503  * @hw:         handle between common and hardware-specific interfaces
504  * @reg:        register controlling multiplexer
505  * @table:      array of register values corresponding to the parent index
506  * @shift:      shift to multiplexer bit field
507  * @mask:       mask of mutliplexer bit field
508  * @flags:      hardware-specific flags
509  * @lock:       register lock
510  *
511  * Clock with multiple selectable parents.  Implements .get_parent, .set_parent
512  * and .recalc_rate
513  *
514  * Flags:
515  * CLK_MUX_INDEX_ONE - register index starts at 1, not 0
516  * CLK_MUX_INDEX_BIT - register index is a single bit (power of two)
517  * CLK_MUX_HIWORD_MASK - The mux settings are only in lower 16-bit of this
518  *      register, and mask of mux bits are in higher 16-bit of this register.
519  *      While setting the mux bits, higher 16-bit should also be updated to
520  *      indicate changing mux bits.
521  * CLK_MUX_ROUND_CLOSEST - Use the parent rate that is closest to the desired
522  *      frequency.
523  */
524 struct clk_mux {
525         struct clk_hw   hw;
526         void __iomem    *reg;
527         u32             *table;
528         u32             mask;
529         u8              shift;
530         u8              flags;
531         spinlock_t      *lock;
532 };
533
534 #define to_clk_mux(_hw) container_of(_hw, struct clk_mux, hw)
535
536 #define CLK_MUX_INDEX_ONE               BIT(0)
537 #define CLK_MUX_INDEX_BIT               BIT(1)
538 #define CLK_MUX_HIWORD_MASK             BIT(2)
539 #define CLK_MUX_READ_ONLY               BIT(3) /* mux can't be changed */
540 #define CLK_MUX_ROUND_CLOSEST           BIT(4)
541
542 extern const struct clk_ops clk_mux_ops;
543 extern const struct clk_ops clk_mux_ro_ops;
544
545 struct clk *clk_register_mux(struct device *dev, const char *name,
546                 const char * const *parent_names, u8 num_parents,
547                 unsigned long flags,
548                 void __iomem *reg, u8 shift, u8 width,
549                 u8 clk_mux_flags, spinlock_t *lock);
550 struct clk_hw *clk_hw_register_mux(struct device *dev, const char *name,
551                 const char * const *parent_names, u8 num_parents,
552                 unsigned long flags,
553                 void __iomem *reg, u8 shift, u8 width,
554                 u8 clk_mux_flags, spinlock_t *lock);
555
556 struct clk *clk_register_mux_table(struct device *dev, const char *name,
557                 const char * const *parent_names, u8 num_parents,
558                 unsigned long flags,
559                 void __iomem *reg, u8 shift, u32 mask,
560                 u8 clk_mux_flags, u32 *table, spinlock_t *lock);
561 struct clk_hw *clk_hw_register_mux_table(struct device *dev, const char *name,
562                 const char * const *parent_names, u8 num_parents,
563                 unsigned long flags,
564                 void __iomem *reg, u8 shift, u32 mask,
565                 u8 clk_mux_flags, u32 *table, spinlock_t *lock);
566
567 int clk_mux_val_to_index(struct clk_hw *hw, u32 *table, unsigned int flags,
568                          unsigned int val);
569 unsigned int clk_mux_index_to_val(u32 *table, unsigned int flags, u8 index);
570
571 void clk_unregister_mux(struct clk *clk);
572 void clk_hw_unregister_mux(struct clk_hw *hw);
573
574 void of_fixed_factor_clk_setup(struct device_node *node);
575
576 /**
577  * struct clk_fixed_factor - fixed multiplier and divider clock
578  *
579  * @hw:         handle between common and hardware-specific interfaces
580  * @mult:       multiplier
581  * @div:        divider
582  *
583  * Clock with a fixed multiplier and divider. The output frequency is the
584  * parent clock rate divided by div and multiplied by mult.
585  * Implements .recalc_rate, .set_rate and .round_rate
586  */
587
588 struct clk_fixed_factor {
589         struct clk_hw   hw;
590         unsigned int    mult;
591         unsigned int    div;
592 };
593
594 #define to_clk_fixed_factor(_hw) container_of(_hw, struct clk_fixed_factor, hw)
595
596 extern const struct clk_ops clk_fixed_factor_ops;
597 struct clk *clk_register_fixed_factor(struct device *dev, const char *name,
598                 const char *parent_name, unsigned long flags,
599                 unsigned int mult, unsigned int div);
600 void clk_unregister_fixed_factor(struct clk *clk);
601 struct clk_hw *clk_hw_register_fixed_factor(struct device *dev,
602                 const char *name, const char *parent_name, unsigned long flags,
603                 unsigned int mult, unsigned int div);
604 void clk_hw_unregister_fixed_factor(struct clk_hw *hw);
605
606 /**
607  * struct clk_fractional_divider - adjustable fractional divider clock
608  *
609  * @hw:         handle between common and hardware-specific interfaces
610  * @reg:        register containing the divider
611  * @mshift:     shift to the numerator bit field
612  * @mwidth:     width of the numerator bit field
613  * @nshift:     shift to the denominator bit field
614  * @nwidth:     width of the denominator bit field
615  * @lock:       register lock
616  *
617  * Clock with adjustable fractional divider affecting its output frequency.
618  *
619  * Flags:
620  * CLK_FRAC_DIVIDER_ZERO_BASED - by default the numerator and denominator
621  *      is the value read from the register. If CLK_FRAC_DIVIDER_ZERO_BASED
622  *      is set then the numerator and denominator are both the value read
623  *      plus one.
624  */
625 struct clk_fractional_divider {
626         struct clk_hw   hw;
627         void __iomem    *reg;
628         u8              mshift;
629         u8              mwidth;
630         u32             mmask;
631         u8              nshift;
632         u8              nwidth;
633         u32             nmask;
634         u8              flags;
635         void            (*approximation)(struct clk_hw *hw,
636                                 unsigned long rate, unsigned long *parent_rate,
637                                 unsigned long *m, unsigned long *n);
638         spinlock_t      *lock;
639 };
640
641 #define to_clk_fd(_hw) container_of(_hw, struct clk_fractional_divider, hw)
642
643 #define CLK_FRAC_DIVIDER_ZERO_BASED             BIT(0)
644
645 extern const struct clk_ops clk_fractional_divider_ops;
646 struct clk *clk_register_fractional_divider(struct device *dev,
647                 const char *name, const char *parent_name, unsigned long flags,
648                 void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth,
649                 u8 clk_divider_flags, spinlock_t *lock);
650 struct clk_hw *clk_hw_register_fractional_divider(struct device *dev,
651                 const char *name, const char *parent_name, unsigned long flags,
652                 void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth,
653                 u8 clk_divider_flags, spinlock_t *lock);
654 void clk_hw_unregister_fractional_divider(struct clk_hw *hw);
655
656 /**
657  * struct clk_multiplier - adjustable multiplier clock
658  *
659  * @hw:         handle between common and hardware-specific interfaces
660  * @reg:        register containing the multiplier
661  * @shift:      shift to the multiplier bit field
662  * @width:      width of the multiplier bit field
663  * @lock:       register lock
664  *
665  * Clock with an adjustable multiplier affecting its output frequency.
666  * Implements .recalc_rate, .set_rate and .round_rate
667  *
668  * Flags:
669  * CLK_MULTIPLIER_ZERO_BYPASS - By default, the multiplier is the value read
670  *      from the register, with 0 being a valid value effectively
671  *      zeroing the output clock rate. If CLK_MULTIPLIER_ZERO_BYPASS is
672  *      set, then a null multiplier will be considered as a bypass,
673  *      leaving the parent rate unmodified.
674  * CLK_MULTIPLIER_ROUND_CLOSEST - Makes the best calculated divider to be
675  *      rounded to the closest integer instead of the down one.
676  */
677 struct clk_multiplier {
678         struct clk_hw   hw;
679         void __iomem    *reg;
680         u8              shift;
681         u8              width;
682         u8              flags;
683         spinlock_t      *lock;
684 };
685
686 #define to_clk_multiplier(_hw) container_of(_hw, struct clk_multiplier, hw)
687
688 #define CLK_MULTIPLIER_ZERO_BYPASS              BIT(0)
689 #define CLK_MULTIPLIER_ROUND_CLOSEST    BIT(1)
690
691 extern const struct clk_ops clk_multiplier_ops;
692
693 /***
694  * struct clk_composite - aggregate clock of mux, divider and gate clocks
695  *
696  * @hw:         handle between common and hardware-specific interfaces
697  * @mux_hw:     handle between composite and hardware-specific mux clock
698  * @rate_hw:    handle between composite and hardware-specific rate clock
699  * @gate_hw:    handle between composite and hardware-specific gate clock
700  * @mux_ops:    clock ops for mux
701  * @rate_ops:   clock ops for rate
702  * @gate_ops:   clock ops for gate
703  */
704 struct clk_composite {
705         struct clk_hw   hw;
706         struct clk_ops  ops;
707
708         struct clk_hw   *mux_hw;
709         struct clk_hw   *rate_hw;
710         struct clk_hw   *gate_hw;
711
712         const struct clk_ops    *mux_ops;
713         const struct clk_ops    *rate_ops;
714         const struct clk_ops    *gate_ops;
715 };
716
717 #define to_clk_composite(_hw) container_of(_hw, struct clk_composite, hw)
718
719 struct clk *clk_register_composite(struct device *dev, const char *name,
720                 const char * const *parent_names, int num_parents,
721                 struct clk_hw *mux_hw, const struct clk_ops *mux_ops,
722                 struct clk_hw *rate_hw, const struct clk_ops *rate_ops,
723                 struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
724                 unsigned long flags);
725 void clk_unregister_composite(struct clk *clk);
726 struct clk_hw *clk_hw_register_composite(struct device *dev, const char *name,
727                 const char * const *parent_names, int num_parents,
728                 struct clk_hw *mux_hw, const struct clk_ops *mux_ops,
729                 struct clk_hw *rate_hw, const struct clk_ops *rate_ops,
730                 struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
731                 unsigned long flags);
732 void clk_hw_unregister_composite(struct clk_hw *hw);
733
734 /***
735  * struct clk_gpio_gate - gpio gated clock
736  *
737  * @hw:         handle between common and hardware-specific interfaces
738  * @gpiod:      gpio descriptor
739  *
740  * Clock with a gpio control for enabling and disabling the parent clock.
741  * Implements .enable, .disable and .is_enabled
742  */
743
744 struct clk_gpio {
745         struct clk_hw   hw;
746         struct gpio_desc *gpiod;
747 };
748
749 #define to_clk_gpio(_hw) container_of(_hw, struct clk_gpio, hw)
750
751 extern const struct clk_ops clk_gpio_gate_ops;
752 struct clk *clk_register_gpio_gate(struct device *dev, const char *name,
753                 const char *parent_name, struct gpio_desc *gpiod,
754                 unsigned long flags);
755 struct clk_hw *clk_hw_register_gpio_gate(struct device *dev, const char *name,
756                 const char *parent_name, struct gpio_desc *gpiod,
757                 unsigned long flags);
758 void clk_hw_unregister_gpio_gate(struct clk_hw *hw);
759
760 /**
761  * struct clk_gpio_mux - gpio controlled clock multiplexer
762  *
763  * @hw:         see struct clk_gpio
764  * @gpiod:      gpio descriptor to select the parent of this clock multiplexer
765  *
766  * Clock with a gpio control for selecting the parent clock.
767  * Implements .get_parent, .set_parent and .determine_rate
768  */
769
770 extern const struct clk_ops clk_gpio_mux_ops;
771 struct clk *clk_register_gpio_mux(struct device *dev, const char *name,
772                 const char * const *parent_names, u8 num_parents, struct gpio_desc *gpiod,
773                 unsigned long flags);
774 struct clk_hw *clk_hw_register_gpio_mux(struct device *dev, const char *name,
775                 const char * const *parent_names, u8 num_parents, struct gpio_desc *gpiod,
776                 unsigned long flags);
777 void clk_hw_unregister_gpio_mux(struct clk_hw *hw);
778
779 /**
780  * clk_register - allocate a new clock, register it and return an opaque cookie
781  * @dev: device that is registering this clock
782  * @hw: link to hardware-specific clock data
783  *
784  * clk_register is the primary interface for populating the clock tree with new
785  * clock nodes.  It returns a pointer to the newly allocated struct clk which
786  * cannot be dereferenced by driver code but may be used in conjuction with the
787  * rest of the clock API.  In the event of an error clk_register will return an
788  * error code; drivers must test for an error code after calling clk_register.
789  */
790 struct clk *clk_register(struct device *dev, struct clk_hw *hw);
791 struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw);
792
793 int __must_check clk_hw_register(struct device *dev, struct clk_hw *hw);
794 int __must_check devm_clk_hw_register(struct device *dev, struct clk_hw *hw);
795 int __must_check of_clk_hw_register(struct device_node *node, struct clk_hw *hw);
796
797 void clk_unregister(struct clk *clk);
798 void devm_clk_unregister(struct device *dev, struct clk *clk);
799
800 void clk_hw_unregister(struct clk_hw *hw);
801 void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw);
802
803 /* helper functions */
804 const char *__clk_get_name(const struct clk *clk);
805 const char *clk_hw_get_name(const struct clk_hw *hw);
806 struct clk_hw *__clk_get_hw(struct clk *clk);
807 unsigned int clk_hw_get_num_parents(const struct clk_hw *hw);
808 struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw);
809 struct clk_hw *clk_hw_get_parent_by_index(const struct clk_hw *hw,
810                                           unsigned int index);
811 unsigned int __clk_get_enable_count(struct clk *clk);
812 unsigned long clk_hw_get_rate(const struct clk_hw *hw);
813 unsigned long __clk_get_flags(struct clk *clk);
814 unsigned long clk_hw_get_flags(const struct clk_hw *hw);
815 #define clk_hw_can_set_rate_parent(hw) \
816         (clk_hw_get_flags((hw)) & CLK_SET_RATE_PARENT)
817
818 bool clk_hw_is_prepared(const struct clk_hw *hw);
819 bool clk_hw_rate_is_protected(const struct clk_hw *hw);
820 bool clk_hw_is_enabled(const struct clk_hw *hw);
821 bool __clk_is_enabled(struct clk *clk);
822 struct clk *__clk_lookup(const char *name);
823 int __clk_mux_determine_rate(struct clk_hw *hw,
824                              struct clk_rate_request *req);
825 int __clk_determine_rate(struct clk_hw *core, struct clk_rate_request *req);
826 int __clk_mux_determine_rate_closest(struct clk_hw *hw,
827                                      struct clk_rate_request *req);
828 int clk_mux_determine_rate_flags(struct clk_hw *hw,
829                                  struct clk_rate_request *req,
830                                  unsigned long flags);
831 void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent);
832 void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
833                            unsigned long max_rate);
834
835 static inline void __clk_hw_set_clk(struct clk_hw *dst, struct clk_hw *src)
836 {
837         dst->clk = src->clk;
838         dst->core = src->core;
839 }
840
841 static inline long divider_round_rate(struct clk_hw *hw, unsigned long rate,
842                                       unsigned long *prate,
843                                       const struct clk_div_table *table,
844                                       u8 width, unsigned long flags)
845 {
846         return divider_round_rate_parent(hw, clk_hw_get_parent(hw),
847                                          rate, prate, table, width, flags);
848 }
849
850 static inline long divider_ro_round_rate(struct clk_hw *hw, unsigned long rate,
851                                          unsigned long *prate,
852                                          const struct clk_div_table *table,
853                                          u8 width, unsigned long flags,
854                                          unsigned int val)
855 {
856         return divider_ro_round_rate_parent(hw, clk_hw_get_parent(hw),
857                                             rate, prate, table, width, flags,
858                                             val);
859 }
860
861 /*
862  * FIXME clock api without lock protection
863  */
864 unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate);
865
866 struct of_device_id;
867
868 struct clk_onecell_data {
869         struct clk **clks;
870         unsigned int clk_num;
871 };
872
873 struct clk_hw_onecell_data {
874         unsigned int num;
875         struct clk_hw *hws[];
876 };
877
878 extern struct of_device_id __clk_of_table;
879
880 #define CLK_OF_DECLARE(name, compat, fn) OF_DECLARE_1(clk, name, compat, fn)
881
882 /*
883  * Use this macro when you have a driver that requires two initialization
884  * routines, one at of_clk_init(), and one at platform device probe
885  */
886 #define CLK_OF_DECLARE_DRIVER(name, compat, fn) \
887         static void __init name##_of_clk_init_driver(struct device_node *np) \
888         {                                                               \
889                 of_node_clear_flag(np, OF_POPULATED);                   \
890                 fn(np);                                                 \
891         }                                                               \
892         OF_DECLARE_1(clk, name, compat, name##_of_clk_init_driver)
893
894 #define CLK_HW_INIT(_name, _parent, _ops, _flags)               \
895         (&(struct clk_init_data) {                              \
896                 .flags          = _flags,                       \
897                 .name           = _name,                        \
898                 .parent_names   = (const char *[]) { _parent }, \
899                 .num_parents    = 1,                            \
900                 .ops            = _ops,                         \
901         })
902
903 #define CLK_HW_INIT_PARENTS(_name, _parents, _ops, _flags)      \
904         (&(struct clk_init_data) {                              \
905                 .flags          = _flags,                       \
906                 .name           = _name,                        \
907                 .parent_names   = _parents,                     \
908                 .num_parents    = ARRAY_SIZE(_parents),         \
909                 .ops            = _ops,                         \
910         })
911
912 #define CLK_HW_INIT_NO_PARENT(_name, _ops, _flags)      \
913         (&(struct clk_init_data) {                      \
914                 .flags          = _flags,               \
915                 .name           = _name,                \
916                 .parent_names   = NULL,                 \
917                 .num_parents    = 0,                    \
918                 .ops            = _ops,                 \
919         })
920
921 #define CLK_FIXED_FACTOR(_struct, _name, _parent,                       \
922                         _div, _mult, _flags)                            \
923         struct clk_fixed_factor _struct = {                             \
924                 .div            = _div,                                 \
925                 .mult           = _mult,                                \
926                 .hw.init        = CLK_HW_INIT(_name,                    \
927                                               _parent,                  \
928                                               &clk_fixed_factor_ops,    \
929                                               _flags),                  \
930         }
931
932 #ifdef CONFIG_OF
933 int of_clk_add_provider(struct device_node *np,
934                         struct clk *(*clk_src_get)(struct of_phandle_args *args,
935                                                    void *data),
936                         void *data);
937 int of_clk_add_hw_provider(struct device_node *np,
938                            struct clk_hw *(*get)(struct of_phandle_args *clkspec,
939                                                  void *data),
940                            void *data);
941 int devm_of_clk_add_hw_provider(struct device *dev,
942                            struct clk_hw *(*get)(struct of_phandle_args *clkspec,
943                                                  void *data),
944                            void *data);
945 void of_clk_del_provider(struct device_node *np);
946 void devm_of_clk_del_provider(struct device *dev);
947 struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
948                                   void *data);
949 struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec,
950                                     void *data);
951 struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data);
952 struct clk_hw *of_clk_hw_onecell_get(struct of_phandle_args *clkspec,
953                                      void *data);
954 int of_clk_parent_fill(struct device_node *np, const char **parents,
955                        unsigned int size);
956 int of_clk_detect_critical(struct device_node *np, int index,
957                             unsigned long *flags);
958
959 #else /* !CONFIG_OF */
960
961 static inline int of_clk_add_provider(struct device_node *np,
962                         struct clk *(*clk_src_get)(struct of_phandle_args *args,
963                                                    void *data),
964                         void *data)
965 {
966         return 0;
967 }
968 static inline int of_clk_add_hw_provider(struct device_node *np,
969                         struct clk_hw *(*get)(struct of_phandle_args *clkspec,
970                                               void *data),
971                         void *data)
972 {
973         return 0;
974 }
975 static inline int devm_of_clk_add_hw_provider(struct device *dev,
976                            struct clk_hw *(*get)(struct of_phandle_args *clkspec,
977                                                  void *data),
978                            void *data)
979 {
980         return 0;
981 }
982 static inline void of_clk_del_provider(struct device_node *np) {}
983 static inline void devm_of_clk_del_provider(struct device *dev) {}
984 static inline struct clk *of_clk_src_simple_get(
985         struct of_phandle_args *clkspec, void *data)
986 {
987         return ERR_PTR(-ENOENT);
988 }
989 static inline struct clk_hw *
990 of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
991 {
992         return ERR_PTR(-ENOENT);
993 }
994 static inline struct clk *of_clk_src_onecell_get(
995         struct of_phandle_args *clkspec, void *data)
996 {
997         return ERR_PTR(-ENOENT);
998 }
999 static inline struct clk_hw *
1000 of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
1001 {
1002         return ERR_PTR(-ENOENT);
1003 }
1004 static inline int of_clk_parent_fill(struct device_node *np,
1005                                      const char **parents, unsigned int size)
1006 {
1007         return 0;
1008 }
1009 static inline int of_clk_detect_critical(struct device_node *np, int index,
1010                                           unsigned long *flags)
1011 {
1012         return 0;
1013 }
1014 #endif /* CONFIG_OF */
1015
1016 /*
1017  * wrap access to peripherals in accessor routines
1018  * for improved portability across platforms
1019  */
1020
1021 #if IS_ENABLED(CONFIG_PPC)
1022
1023 static inline u32 clk_readl(u32 __iomem *reg)
1024 {
1025         return ioread32be(reg);
1026 }
1027
1028 static inline void clk_writel(u32 val, u32 __iomem *reg)
1029 {
1030         iowrite32be(val, reg);
1031 }
1032
1033 #else   /* platform dependent I/O accessors */
1034
1035 static inline u32 clk_readl(u32 __iomem *reg)
1036 {
1037         return readl(reg);
1038 }
1039
1040 static inline void clk_writel(u32 val, u32 __iomem *reg)
1041 {
1042         writel(val, reg);
1043 }
1044
1045 #endif  /* platform dependent I/O accessors */
1046
1047 void clk_gate_restore_context(struct clk_hw *hw);
1048
1049 #endif /* CONFIG_COMMON_CLK */
1050 #endif /* CLK_PROVIDER_H */