Merge tag 'v5.15.57' into rpi-5.15.y
[platform/kernel/linux-rpi.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/of.h>
10 #include <linux/of_clk.h>
11
12 /*
13  * flags used across common struct clk.  these flags should only affect the
14  * top-level framework.  custom flags for dealing with hardware specifics
15  * belong in struct clk_foo
16  *
17  * Please update clk_flags[] in drivers/clk/clk.c when making changes here!
18  */
19 #define CLK_SET_RATE_GATE       BIT(0) /* must be gated across rate change */
20 #define CLK_SET_PARENT_GATE     BIT(1) /* must be gated across re-parent */
21 #define CLK_SET_RATE_PARENT     BIT(2) /* propagate rate change up one level */
22 #define CLK_IGNORE_UNUSED       BIT(3) /* do not gate even if unused */
23                                 /* unused */
24                                 /* unused */
25 #define CLK_GET_RATE_NOCACHE    BIT(6) /* do not use the cached clk rate */
26 #define CLK_SET_RATE_NO_REPARENT BIT(7) /* don't re-parent on rate change */
27 #define CLK_GET_ACCURACY_NOCACHE BIT(8) /* do not use the cached clk accuracy */
28 #define CLK_RECALC_NEW_RATES    BIT(9) /* recalc rates after notifications */
29 #define CLK_SET_RATE_UNGATE     BIT(10) /* clock needs to run to set rate */
30 #define CLK_IS_CRITICAL         BIT(11) /* do not gate, ever */
31 /* parents need enable during gate/ungate, set rate and re-parent */
32 #define CLK_OPS_PARENT_ENABLE   BIT(12)
33 /* duty cycle call may be forwarded to the parent clock */
34 #define CLK_DUTY_CYCLE_PARENT   BIT(13)
35
36 struct clk;
37 struct clk_hw;
38 struct clk_core;
39 struct dentry;
40
41 /**
42  * struct clk_rate_request - Structure encoding the clk constraints that
43  * a clock user might require.
44  *
45  * Should be initialized by calling clk_hw_init_rate_request().
46  *
47  * @rate:               Requested clock rate. This field will be adjusted by
48  *                      clock drivers according to hardware capabilities.
49  * @min_rate:           Minimum rate imposed by clk users.
50  * @max_rate:           Maximum rate imposed by clk users.
51  * @best_parent_rate:   The best parent rate a parent can provide to fulfill the
52  *                      requested constraints.
53  * @best_parent_hw:     The most appropriate parent clock that fulfills the
54  *                      requested constraints.
55  *
56  */
57 struct clk_rate_request {
58         unsigned long rate;
59         unsigned long min_rate;
60         unsigned long max_rate;
61         unsigned long best_parent_rate;
62         struct clk_hw *best_parent_hw;
63 };
64
65 void clk_hw_init_rate_request(struct clk_hw * const hw,
66                               struct clk_rate_request *req,
67                               unsigned long rate);
68
69 /**
70  * struct clk_duty - Struture encoding the duty cycle ratio of a clock
71  *
72  * @num:        Numerator of the duty cycle ratio
73  * @den:        Denominator of the duty cycle ratio
74  */
75 struct clk_duty {
76         unsigned int num;
77         unsigned int den;
78 };
79
80 /**
81  * struct clk_ops -  Callback operations for hardware clocks; these are to
82  * be provided by the clock implementation, and will be called by drivers
83  * through the clk_* api.
84  *
85  * @prepare:    Prepare the clock for enabling. This must not return until
86  *              the clock is fully prepared, and it's safe to call clk_enable.
87  *              This callback is intended to allow clock implementations to
88  *              do any initialisation that may sleep. Called with
89  *              prepare_lock held.
90  *
91  * @unprepare:  Release the clock from its prepared state. This will typically
92  *              undo any work done in the @prepare callback. Called with
93  *              prepare_lock held.
94  *
95  * @is_prepared: Queries the hardware to determine if the clock is prepared.
96  *              This function is allowed to sleep. Optional, if this op is not
97  *              set then the prepare count will be used.
98  *
99  * @unprepare_unused: Unprepare the clock atomically.  Only called from
100  *              clk_disable_unused for prepare clocks with special needs.
101  *              Called with prepare mutex held. This function may sleep.
102  *
103  * @enable:     Enable the clock atomically. This must not return until the
104  *              clock is generating a valid clock signal, usable by consumer
105  *              devices. Called with enable_lock held. This function must not
106  *              sleep.
107  *
108  * @disable:    Disable the clock atomically. Called with enable_lock held.
109  *              This function must not sleep.
110  *
111  * @is_enabled: Queries the hardware to determine if the clock is enabled.
112  *              This function must not sleep. Optional, if this op is not
113  *              set then the enable count will be used.
114  *
115  * @disable_unused: Disable the clock atomically.  Only called from
116  *              clk_disable_unused for gate clocks with special needs.
117  *              Called with enable_lock held.  This function must not
118  *              sleep.
119  *
120  * @save_context: Save the context of the clock in prepration for poweroff.
121  *
122  * @restore_context: Restore the context of the clock after a restoration
123  *              of power.
124  *
125  * @recalc_rate Recalculate the rate of this clock, by querying hardware. The
126  *              parent rate is an input parameter.  It is up to the caller to
127  *              ensure that the prepare_mutex is held across this call.
128  *              Returns the calculated rate.  Optional, but recommended - if
129  *              this op is not set then clock rate will be initialized to 0.
130  *
131  * @round_rate: Given a target rate as input, returns the closest rate actually
132  *              supported by the clock. The parent rate is an input/output
133  *              parameter.
134  *
135  * @determine_rate: Given a target rate as input, returns the closest rate
136  *              actually supported by the clock, and optionally the parent clock
137  *              that should be used to provide the clock rate.
138  *
139  * @set_parent: Change the input source of this clock; for clocks with multiple
140  *              possible parents specify a new parent by passing in the index
141  *              as a u8 corresponding to the parent in either the .parent_names
142  *              or .parents arrays.  This function in affect translates an
143  *              array index into the value programmed into the hardware.
144  *              Returns 0 on success, -EERROR otherwise.
145  *
146  * @get_parent: Queries the hardware to determine the parent of a clock.  The
147  *              return value is a u8 which specifies the index corresponding to
148  *              the parent clock.  This index can be applied to either the
149  *              .parent_names or .parents arrays.  In short, this function
150  *              translates the parent value read from hardware into an array
151  *              index.  Currently only called when the clock is initialized by
152  *              __clk_init.  This callback is mandatory for clocks with
153  *              multiple parents.  It is optional (and unnecessary) for clocks
154  *              with 0 or 1 parents.
155  *
156  * @set_rate:   Change the rate of this clock. The requested rate is specified
157  *              by the second argument, which should typically be the return
158  *              of .round_rate call.  The third argument gives the parent rate
159  *              which is likely helpful for most .set_rate implementation.
160  *              Returns 0 on success, -EERROR otherwise.
161  *
162  * @set_rate_and_parent: Change the rate and the parent of this clock. The
163  *              requested rate is specified by the second argument, which
164  *              should typically be the return of .round_rate call.  The
165  *              third argument gives the parent rate which is likely helpful
166  *              for most .set_rate_and_parent implementation. The fourth
167  *              argument gives the parent index. This callback is optional (and
168  *              unnecessary) for clocks with 0 or 1 parents as well as
169  *              for clocks that can tolerate switching the rate and the parent
170  *              separately via calls to .set_parent and .set_rate.
171  *              Returns 0 on success, -EERROR otherwise.
172  *
173  * @recalc_accuracy: Recalculate the accuracy of this clock. The clock accuracy
174  *              is expressed in ppb (parts per billion). The parent accuracy is
175  *              an input parameter.
176  *              Returns the calculated accuracy.  Optional - if this op is not
177  *              set then clock accuracy will be initialized to parent accuracy
178  *              or 0 (perfect clock) if clock has no parent.
179  *
180  * @get_phase:  Queries the hardware to get the current phase of a clock.
181  *              Returned values are 0-359 degrees on success, negative
182  *              error codes on failure.
183  *
184  * @set_phase:  Shift the phase this clock signal in degrees specified
185  *              by the second argument. Valid values for degrees are
186  *              0-359. Return 0 on success, otherwise -EERROR.
187  *
188  * @get_duty_cycle: Queries the hardware to get the current duty cycle ratio
189  *              of a clock. Returned values denominator cannot be 0 and must be
190  *              superior or equal to the numerator.
191  *
192  * @set_duty_cycle: Apply the duty cycle ratio to this clock signal specified by
193  *              the numerator (2nd argurment) and denominator (3rd  argument).
194  *              Argument must be a valid ratio (denominator > 0
195  *              and >= numerator) Return 0 on success, otherwise -EERROR.
196  *
197  * @init:       Perform platform-specific initialization magic.
198  *              This is not used by any of the basic clock types.
199  *              This callback exist for HW which needs to perform some
200  *              initialisation magic for CCF to get an accurate view of the
201  *              clock. It may also be used dynamic resource allocation is
202  *              required. It shall not used to deal with clock parameters,
203  *              such as rate or parents.
204  *              Returns 0 on success, -EERROR otherwise.
205  *
206  * @terminate:  Free any resource allocated by init.
207  *
208  * @debug_init: Set up type-specific debugfs entries for this clock.  This
209  *              is called once, after the debugfs directory entry for this
210  *              clock has been created.  The dentry pointer representing that
211  *              directory is provided as an argument.  Called with
212  *              prepare_lock held.  Returns 0 on success, -EERROR otherwise.
213  *
214  *
215  * The clk_enable/clk_disable and clk_prepare/clk_unprepare pairs allow
216  * implementations to split any work between atomic (enable) and sleepable
217  * (prepare) contexts.  If enabling a clock requires code that might sleep,
218  * this must be done in clk_prepare.  Clock enable code that will never be
219  * called in a sleepable context may be implemented in clk_enable.
220  *
221  * Typically, drivers will call clk_prepare when a clock may be needed later
222  * (eg. when a device is opened), and clk_enable when the clock is actually
223  * required (eg. from an interrupt). Note that clk_prepare MUST have been
224  * called before clk_enable.
225  */
226 struct clk_ops {
227         int             (*prepare)(struct clk_hw *hw);
228         void            (*unprepare)(struct clk_hw *hw);
229         int             (*is_prepared)(struct clk_hw *hw);
230         void            (*unprepare_unused)(struct clk_hw *hw);
231         int             (*enable)(struct clk_hw *hw);
232         void            (*disable)(struct clk_hw *hw);
233         int             (*is_enabled)(struct clk_hw *hw);
234         void            (*disable_unused)(struct clk_hw *hw);
235         int             (*save_context)(struct clk_hw *hw);
236         void            (*restore_context)(struct clk_hw *hw);
237         unsigned long   (*recalc_rate)(struct clk_hw *hw,
238                                         unsigned long parent_rate);
239         long            (*round_rate)(struct clk_hw *hw, unsigned long rate,
240                                         unsigned long *parent_rate);
241         int             (*determine_rate)(struct clk_hw *hw,
242                                           struct clk_rate_request *req);
243         int             (*set_parent)(struct clk_hw *hw, u8 index);
244         u8              (*get_parent)(struct clk_hw *hw);
245         int             (*set_rate)(struct clk_hw *hw, unsigned long rate,
246                                     unsigned long parent_rate);
247         int             (*set_rate_and_parent)(struct clk_hw *hw,
248                                     unsigned long rate,
249                                     unsigned long parent_rate, u8 index);
250         unsigned long   (*recalc_accuracy)(struct clk_hw *hw,
251                                            unsigned long parent_accuracy);
252         int             (*get_phase)(struct clk_hw *hw);
253         int             (*set_phase)(struct clk_hw *hw, int degrees);
254         int             (*get_duty_cycle)(struct clk_hw *hw,
255                                           struct clk_duty *duty);
256         int             (*set_duty_cycle)(struct clk_hw *hw,
257                                           struct clk_duty *duty);
258         int             (*init)(struct clk_hw *hw);
259         void            (*terminate)(struct clk_hw *hw);
260         void            (*debug_init)(struct clk_hw *hw, struct dentry *dentry);
261 };
262
263 /**
264  * struct clk_parent_data - clk parent information
265  * @hw: parent clk_hw pointer (used for clk providers with internal clks)
266  * @fw_name: parent name local to provider registering clk
267  * @name: globally unique parent name (used as a fallback)
268  * @index: parent index local to provider registering clk (if @fw_name absent)
269  */
270 struct clk_parent_data {
271         const struct clk_hw     *hw;
272         const char              *fw_name;
273         const char              *name;
274         int                     index;
275 };
276
277 /**
278  * struct clk_init_data - holds init data that's common to all clocks and is
279  * shared between the clock provider and the common clock framework.
280  *
281  * @name: clock name
282  * @ops: operations this clock supports
283  * @parent_names: array of string names for all possible parents
284  * @parent_data: array of parent data for all possible parents (when some
285  *               parents are external to the clk controller)
286  * @parent_hws: array of pointers to all possible parents (when all parents
287  *              are internal to the clk controller)
288  * @num_parents: number of possible parents
289  * @flags: framework-level hints and quirks
290  */
291 struct clk_init_data {
292         const char              *name;
293         const struct clk_ops    *ops;
294         /* Only one of the following three should be assigned */
295         const char              * const *parent_names;
296         const struct clk_parent_data    *parent_data;
297         const struct clk_hw             **parent_hws;
298         u8                      num_parents;
299         unsigned long           flags;
300 };
301
302 /**
303  * struct clk_hw - handle for traversing from a struct clk to its corresponding
304  * hardware-specific structure.  struct clk_hw should be declared within struct
305  * clk_foo and then referenced by the struct clk instance that uses struct
306  * clk_foo's clk_ops
307  *
308  * @core: pointer to the struct clk_core instance that points back to this
309  * struct clk_hw instance
310  *
311  * @clk: pointer to the per-user struct clk instance that can be used to call
312  * into the clk API
313  *
314  * @init: pointer to struct clk_init_data that contains the init data shared
315  * with the common clock framework. This pointer will be set to NULL once
316  * a clk_register() variant is called on this clk_hw pointer.
317  */
318 struct clk_hw {
319         struct clk_core *core;
320         struct clk *clk;
321         const struct clk_init_data *init;
322 };
323
324 /*
325  * DOC: Basic clock implementations common to many platforms
326  *
327  * Each basic clock hardware type is comprised of a structure describing the
328  * clock hardware, implementations of the relevant callbacks in struct clk_ops,
329  * unique flags for that hardware type, a registration function and an
330  * alternative macro for static initialization
331  */
332
333 /**
334  * struct clk_fixed_rate - fixed-rate clock
335  * @hw:         handle between common and hardware-specific interfaces
336  * @fixed_rate: constant frequency of clock
337  * @fixed_accuracy: constant accuracy of clock in ppb (parts per billion)
338  * @flags:      hardware specific flags
339  *
340  * Flags:
341  * * CLK_FIXED_RATE_PARENT_ACCURACY - Use the accuracy of the parent clk
342  *                                    instead of what's set in @fixed_accuracy.
343  */
344 struct clk_fixed_rate {
345         struct          clk_hw hw;
346         unsigned long   fixed_rate;
347         unsigned long   fixed_accuracy;
348         unsigned long   flags;
349 };
350
351 #define CLK_FIXED_RATE_PARENT_ACCURACY  BIT(0)
352
353 extern const struct clk_ops clk_fixed_rate_ops;
354 struct clk_hw *__clk_hw_register_fixed_rate(struct device *dev,
355                 struct device_node *np, const char *name,
356                 const char *parent_name, const struct clk_hw *parent_hw,
357                 const struct clk_parent_data *parent_data, unsigned long flags,
358                 unsigned long fixed_rate, unsigned long fixed_accuracy,
359                 unsigned long clk_fixed_flags);
360 struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
361                 const char *parent_name, unsigned long flags,
362                 unsigned long fixed_rate);
363 /**
364  * clk_hw_register_fixed_rate - register fixed-rate clock with the clock
365  * framework
366  * @dev: device that is registering this clock
367  * @name: name of this clock
368  * @parent_name: name of clock's parent
369  * @flags: framework-specific flags
370  * @fixed_rate: non-adjustable clock rate
371  */
372 #define clk_hw_register_fixed_rate(dev, name, parent_name, flags, fixed_rate)  \
373         __clk_hw_register_fixed_rate((dev), NULL, (name), (parent_name), NULL, \
374                                      NULL, (flags), (fixed_rate), 0, 0)
375 /**
376  * clk_hw_register_fixed_rate_parent_hw - register fixed-rate clock with
377  * the clock framework
378  * @dev: device that is registering this clock
379  * @name: name of this clock
380  * @parent_hw: pointer to parent clk
381  * @flags: framework-specific flags
382  * @fixed_rate: non-adjustable clock rate
383  */
384 #define clk_hw_register_fixed_rate_parent_hw(dev, name, parent_hw, flags,     \
385                                              fixed_rate)                      \
386         __clk_hw_register_fixed_rate((dev), NULL, (name), NULL, (parent_hw),  \
387                                      NULL, (flags), (fixed_rate), 0, 0)
388 /**
389  * clk_hw_register_fixed_rate_parent_data - register fixed-rate clock with
390  * the clock framework
391  * @dev: device that is registering this clock
392  * @name: name of this clock
393  * @parent_data: parent clk data
394  * @flags: framework-specific flags
395  * @fixed_rate: non-adjustable clock rate
396  */
397 #define clk_hw_register_fixed_rate_parent_data(dev, name, parent_hw, flags,   \
398                                              fixed_rate)                      \
399         __clk_hw_register_fixed_rate((dev), NULL, (name), NULL, NULL,         \
400                                      (parent_data), (flags), (fixed_rate), 0, \
401                                      0)
402 /**
403  * clk_hw_register_fixed_rate_with_accuracy - register fixed-rate clock with
404  * the clock framework
405  * @dev: device that is registering this clock
406  * @name: name of this clock
407  * @parent_name: name of clock's parent
408  * @flags: framework-specific flags
409  * @fixed_rate: non-adjustable clock rate
410  * @fixed_accuracy: non-adjustable clock accuracy
411  */
412 #define clk_hw_register_fixed_rate_with_accuracy(dev, name, parent_name,      \
413                                                  flags, fixed_rate,           \
414                                                  fixed_accuracy)              \
415         __clk_hw_register_fixed_rate((dev), NULL, (name), (parent_name),      \
416                                      NULL, NULL, (flags), (fixed_rate),       \
417                                      (fixed_accuracy), 0)
418 /**
419  * clk_hw_register_fixed_rate_with_accuracy_parent_hw - register fixed-rate
420  * clock with the clock framework
421  * @dev: device that is registering this clock
422  * @name: name of this clock
423  * @parent_hw: pointer to parent clk
424  * @flags: framework-specific flags
425  * @fixed_rate: non-adjustable clock rate
426  * @fixed_accuracy: non-adjustable clock accuracy
427  */
428 #define clk_hw_register_fixed_rate_with_accuracy_parent_hw(dev, name,         \
429                 parent_hw, flags, fixed_rate, fixed_accuracy)                 \
430         __clk_hw_register_fixed_rate((dev), NULL, (name), NULL, (parent_hw)   \
431                                      NULL, NULL, (flags), (fixed_rate),       \
432                                      (fixed_accuracy), 0)
433 /**
434  * clk_hw_register_fixed_rate_with_accuracy_parent_data - register fixed-rate
435  * clock with the clock framework
436  * @dev: device that is registering this clock
437  * @name: name of this clock
438  * @parent_name: name of clock's parent
439  * @flags: framework-specific flags
440  * @fixed_rate: non-adjustable clock rate
441  * @fixed_accuracy: non-adjustable clock accuracy
442  */
443 #define clk_hw_register_fixed_rate_with_accuracy_parent_data(dev, name,       \
444                 parent_data, flags, fixed_rate, fixed_accuracy)               \
445         __clk_hw_register_fixed_rate((dev), NULL, (name), NULL, NULL,         \
446                                      (parent_data), NULL, (flags),            \
447                                      (fixed_rate), (fixed_accuracy), 0)
448
449 void clk_unregister_fixed_rate(struct clk *clk);
450 void clk_hw_unregister_fixed_rate(struct clk_hw *hw);
451
452 void of_fixed_clk_setup(struct device_node *np);
453
454 /**
455  * struct clk_gate - gating clock
456  *
457  * @hw:         handle between common and hardware-specific interfaces
458  * @reg:        register controlling gate
459  * @bit_idx:    single bit controlling gate
460  * @flags:      hardware-specific flags
461  * @lock:       register lock
462  *
463  * Clock which can gate its output.  Implements .enable & .disable
464  *
465  * Flags:
466  * CLK_GATE_SET_TO_DISABLE - by default this clock sets the bit at bit_idx to
467  *      enable the clock.  Setting this flag does the opposite: setting the bit
468  *      disable the clock and clearing it enables the clock
469  * CLK_GATE_HIWORD_MASK - The gate settings are only in lower 16-bit
470  *      of this register, and mask of gate bits are in higher 16-bit of this
471  *      register.  While setting the gate bits, higher 16-bit should also be
472  *      updated to indicate changing gate bits.
473  * CLK_GATE_BIG_ENDIAN - by default little endian register accesses are used for
474  *      the gate register.  Setting this flag makes the register accesses big
475  *      endian.
476  */
477 struct clk_gate {
478         struct clk_hw hw;
479         void __iomem    *reg;
480         u8              bit_idx;
481         u8              flags;
482         spinlock_t      *lock;
483 };
484
485 #define to_clk_gate(_hw) container_of(_hw, struct clk_gate, hw)
486
487 #define CLK_GATE_SET_TO_DISABLE         BIT(0)
488 #define CLK_GATE_HIWORD_MASK            BIT(1)
489 #define CLK_GATE_BIG_ENDIAN             BIT(2)
490
491 extern const struct clk_ops clk_gate_ops;
492 struct clk_hw *__clk_hw_register_gate(struct device *dev,
493                 struct device_node *np, const char *name,
494                 const char *parent_name, const struct clk_hw *parent_hw,
495                 const struct clk_parent_data *parent_data,
496                 unsigned long flags,
497                 void __iomem *reg, u8 bit_idx,
498                 u8 clk_gate_flags, spinlock_t *lock);
499 struct clk *clk_register_gate(struct device *dev, const char *name,
500                 const char *parent_name, unsigned long flags,
501                 void __iomem *reg, u8 bit_idx,
502                 u8 clk_gate_flags, spinlock_t *lock);
503 /**
504  * clk_hw_register_gate - register a gate clock with the clock framework
505  * @dev: device that is registering this clock
506  * @name: name of this clock
507  * @parent_name: name of this clock's parent
508  * @flags: framework-specific flags for this clock
509  * @reg: register address to control gating of this clock
510  * @bit_idx: which bit in the register controls gating of this clock
511  * @clk_gate_flags: gate-specific flags for this clock
512  * @lock: shared register lock for this clock
513  */
514 #define clk_hw_register_gate(dev, name, parent_name, flags, reg, bit_idx,     \
515                              clk_gate_flags, lock)                            \
516         __clk_hw_register_gate((dev), NULL, (name), (parent_name), NULL,      \
517                                NULL, (flags), (reg), (bit_idx),               \
518                                (clk_gate_flags), (lock))
519 /**
520  * clk_hw_register_gate_parent_hw - register a gate clock with the clock
521  * framework
522  * @dev: device that is registering this clock
523  * @name: name of this clock
524  * @parent_hw: pointer to parent clk
525  * @flags: framework-specific flags for this clock
526  * @reg: register address to control gating of this clock
527  * @bit_idx: which bit in the register controls gating of this clock
528  * @clk_gate_flags: gate-specific flags for this clock
529  * @lock: shared register lock for this clock
530  */
531 #define clk_hw_register_gate_parent_hw(dev, name, parent_hw, flags, reg,      \
532                                        bit_idx, clk_gate_flags, lock)         \
533         __clk_hw_register_gate((dev), NULL, (name), NULL, (parent_hw),        \
534                                NULL, (flags), (reg), (bit_idx),               \
535                                (clk_gate_flags), (lock))
536 /**
537  * clk_hw_register_gate_parent_data - register a gate clock with the clock
538  * framework
539  * @dev: device that is registering this clock
540  * @name: name of this clock
541  * @parent_data: parent clk data
542  * @flags: framework-specific flags for this clock
543  * @reg: register address to control gating of this clock
544  * @bit_idx: which bit in the register controls gating of this clock
545  * @clk_gate_flags: gate-specific flags for this clock
546  * @lock: shared register lock for this clock
547  */
548 #define clk_hw_register_gate_parent_data(dev, name, parent_data, flags, reg,  \
549                                        bit_idx, clk_gate_flags, lock)         \
550         __clk_hw_register_gate((dev), NULL, (name), NULL, NULL, (parent_data), \
551                                (flags), (reg), (bit_idx),                     \
552                                (clk_gate_flags), (lock))
553 void clk_unregister_gate(struct clk *clk);
554 void clk_hw_unregister_gate(struct clk_hw *hw);
555 int clk_gate_is_enabled(struct clk_hw *hw);
556
557 struct clk_div_table {
558         unsigned int    val;
559         unsigned int    div;
560 };
561
562 /**
563  * struct clk_divider - adjustable divider clock
564  *
565  * @hw:         handle between common and hardware-specific interfaces
566  * @reg:        register containing the divider
567  * @shift:      shift to the divider bit field
568  * @width:      width of the divider bit field
569  * @table:      array of value/divider pairs, last entry should have div = 0
570  * @lock:       register lock
571  *
572  * Clock with an adjustable divider affecting its output frequency.  Implements
573  * .recalc_rate, .set_rate and .round_rate
574  *
575  * Flags:
576  * CLK_DIVIDER_ONE_BASED - by default the divisor is the value read from the
577  *      register plus one.  If CLK_DIVIDER_ONE_BASED is set then the divider is
578  *      the raw value read from the register, with the value of zero considered
579  *      invalid, unless CLK_DIVIDER_ALLOW_ZERO is set.
580  * CLK_DIVIDER_POWER_OF_TWO - clock divisor is 2 raised to the value read from
581  *      the hardware register
582  * CLK_DIVIDER_ALLOW_ZERO - Allow zero divisors.  For dividers which have
583  *      CLK_DIVIDER_ONE_BASED set, it is possible to end up with a zero divisor.
584  *      Some hardware implementations gracefully handle this case and allow a
585  *      zero divisor by not modifying their input clock
586  *      (divide by one / bypass).
587  * CLK_DIVIDER_HIWORD_MASK - The divider settings are only in lower 16-bit
588  *      of this register, and mask of divider bits are in higher 16-bit of this
589  *      register.  While setting the divider bits, higher 16-bit should also be
590  *      updated to indicate changing divider bits.
591  * CLK_DIVIDER_ROUND_CLOSEST - Makes the best calculated divider to be rounded
592  *      to the closest integer instead of the up one.
593  * CLK_DIVIDER_READ_ONLY - The divider settings are preconfigured and should
594  *      not be changed by the clock framework.
595  * CLK_DIVIDER_MAX_AT_ZERO - For dividers which are like CLK_DIVIDER_ONE_BASED
596  *      except when the value read from the register is zero, the divisor is
597  *      2^width of the field.
598  * CLK_DIVIDER_BIG_ENDIAN - By default little endian register accesses are used
599  *      for the divider register.  Setting this flag makes the register accesses
600  *      big endian.
601  */
602 struct clk_divider {
603         struct clk_hw   hw;
604         void __iomem    *reg;
605         u8              shift;
606         u8              width;
607         u8              flags;
608         const struct clk_div_table      *table;
609         spinlock_t      *lock;
610 };
611
612 #define clk_div_mask(width)     ((1 << (width)) - 1)
613 #define to_clk_divider(_hw) container_of(_hw, struct clk_divider, hw)
614
615 #define CLK_DIVIDER_ONE_BASED           BIT(0)
616 #define CLK_DIVIDER_POWER_OF_TWO        BIT(1)
617 #define CLK_DIVIDER_ALLOW_ZERO          BIT(2)
618 #define CLK_DIVIDER_HIWORD_MASK         BIT(3)
619 #define CLK_DIVIDER_ROUND_CLOSEST       BIT(4)
620 #define CLK_DIVIDER_READ_ONLY           BIT(5)
621 #define CLK_DIVIDER_MAX_AT_ZERO         BIT(6)
622 #define CLK_DIVIDER_BIG_ENDIAN          BIT(7)
623
624 extern const struct clk_ops clk_divider_ops;
625 extern const struct clk_ops clk_divider_ro_ops;
626
627 unsigned long divider_recalc_rate(struct clk_hw *hw, unsigned long parent_rate,
628                 unsigned int val, const struct clk_div_table *table,
629                 unsigned long flags, unsigned long width);
630 long divider_round_rate_parent(struct clk_hw *hw, struct clk_hw *parent,
631                                unsigned long rate, unsigned long *prate,
632                                const struct clk_div_table *table,
633                                u8 width, unsigned long flags);
634 long divider_ro_round_rate_parent(struct clk_hw *hw, struct clk_hw *parent,
635                                   unsigned long rate, unsigned long *prate,
636                                   const struct clk_div_table *table, u8 width,
637                                   unsigned long flags, unsigned int val);
638 int divider_determine_rate(struct clk_hw *hw, struct clk_rate_request *req,
639                            const struct clk_div_table *table, u8 width,
640                            unsigned long flags);
641 int divider_ro_determine_rate(struct clk_hw *hw, struct clk_rate_request *req,
642                               const struct clk_div_table *table, u8 width,
643                               unsigned long flags, unsigned int val);
644 int divider_get_val(unsigned long rate, unsigned long parent_rate,
645                 const struct clk_div_table *table, u8 width,
646                 unsigned long flags);
647
648 struct clk_hw *__clk_hw_register_divider(struct device *dev,
649                 struct device_node *np, const char *name,
650                 const char *parent_name, const struct clk_hw *parent_hw,
651                 const struct clk_parent_data *parent_data, unsigned long flags,
652                 void __iomem *reg, u8 shift, u8 width, u8 clk_divider_flags,
653                 const struct clk_div_table *table, spinlock_t *lock);
654 struct clk_hw *__devm_clk_hw_register_divider(struct device *dev,
655                 struct device_node *np, const char *name,
656                 const char *parent_name, const struct clk_hw *parent_hw,
657                 const struct clk_parent_data *parent_data, unsigned long flags,
658                 void __iomem *reg, u8 shift, u8 width, u8 clk_divider_flags,
659                 const struct clk_div_table *table, spinlock_t *lock);
660 struct clk *clk_register_divider_table(struct device *dev, const char *name,
661                 const char *parent_name, unsigned long flags,
662                 void __iomem *reg, u8 shift, u8 width,
663                 u8 clk_divider_flags, const struct clk_div_table *table,
664                 spinlock_t *lock);
665 /**
666  * clk_register_divider - register a divider clock with the clock framework
667  * @dev: device registering this clock
668  * @name: name of this clock
669  * @parent_name: name of clock's parent
670  * @flags: framework-specific flags
671  * @reg: register address to adjust divider
672  * @shift: number of bits to shift the bitfield
673  * @width: width of the bitfield
674  * @clk_divider_flags: divider-specific flags for this clock
675  * @lock: shared register lock for this clock
676  */
677 #define clk_register_divider(dev, name, parent_name, flags, reg, shift, width, \
678                              clk_divider_flags, lock)                          \
679         clk_register_divider_table((dev), (name), (parent_name), (flags),      \
680                                    (reg), (shift), (width),                    \
681                                    (clk_divider_flags), NULL, (lock))
682 /**
683  * clk_hw_register_divider - register a divider clock with the clock framework
684  * @dev: device registering this clock
685  * @name: name of this clock
686  * @parent_name: name of clock's parent
687  * @flags: framework-specific flags
688  * @reg: register address to adjust divider
689  * @shift: number of bits to shift the bitfield
690  * @width: width of the bitfield
691  * @clk_divider_flags: divider-specific flags for this clock
692  * @lock: shared register lock for this clock
693  */
694 #define clk_hw_register_divider(dev, name, parent_name, flags, reg, shift,    \
695                                 width, clk_divider_flags, lock)               \
696         __clk_hw_register_divider((dev), NULL, (name), (parent_name), NULL,   \
697                                   NULL, (flags), (reg), (shift), (width),     \
698                                   (clk_divider_flags), NULL, (lock))
699 /**
700  * clk_hw_register_divider_parent_hw - register a divider clock with the clock
701  * framework
702  * @dev: device registering this clock
703  * @name: name of this clock
704  * @parent_hw: pointer to parent clk
705  * @flags: framework-specific flags
706  * @reg: register address to adjust divider
707  * @shift: number of bits to shift the bitfield
708  * @width: width of the bitfield
709  * @clk_divider_flags: divider-specific flags for this clock
710  * @lock: shared register lock for this clock
711  */
712 #define clk_hw_register_divider_parent_hw(dev, name, parent_hw, flags, reg,   \
713                                           shift, width, clk_divider_flags,    \
714                                           lock)                               \
715         __clk_hw_register_divider((dev), NULL, (name), NULL, (parent_hw),     \
716                                   NULL, (flags), (reg), (shift), (width),     \
717                                   (clk_divider_flags), NULL, (lock))
718 /**
719  * clk_hw_register_divider_parent_data - register a divider clock with the clock
720  * framework
721  * @dev: device registering this clock
722  * @name: name of this clock
723  * @parent_data: parent clk data
724  * @flags: framework-specific flags
725  * @reg: register address to adjust divider
726  * @shift: number of bits to shift the bitfield
727  * @width: width of the bitfield
728  * @clk_divider_flags: divider-specific flags for this clock
729  * @lock: shared register lock for this clock
730  */
731 #define clk_hw_register_divider_parent_data(dev, name, parent_data, flags,    \
732                                             reg, shift, width,                \
733                                             clk_divider_flags, lock)          \
734         __clk_hw_register_divider((dev), NULL, (name), NULL, NULL,            \
735                                   (parent_data), (flags), (reg), (shift),     \
736                                   (width), (clk_divider_flags), NULL, (lock))
737 /**
738  * clk_hw_register_divider_table - register a table based divider clock with
739  * the clock framework
740  * @dev: device registering this clock
741  * @name: name of this clock
742  * @parent_name: name of clock's parent
743  * @flags: framework-specific flags
744  * @reg: register address to adjust divider
745  * @shift: number of bits to shift the bitfield
746  * @width: width of the bitfield
747  * @clk_divider_flags: divider-specific flags for this clock
748  * @table: array of divider/value pairs ending with a div set to 0
749  * @lock: shared register lock for this clock
750  */
751 #define clk_hw_register_divider_table(dev, name, parent_name, flags, reg,     \
752                                       shift, width, clk_divider_flags, table, \
753                                       lock)                                   \
754         __clk_hw_register_divider((dev), NULL, (name), (parent_name), NULL,   \
755                                   NULL, (flags), (reg), (shift), (width),     \
756                                   (clk_divider_flags), (table), (lock))
757 /**
758  * clk_hw_register_divider_table_parent_hw - register a table based divider
759  * clock with the clock framework
760  * @dev: device registering this clock
761  * @name: name of this clock
762  * @parent_hw: pointer to parent clk
763  * @flags: framework-specific flags
764  * @reg: register address to adjust divider
765  * @shift: number of bits to shift the bitfield
766  * @width: width of the bitfield
767  * @clk_divider_flags: divider-specific flags for this clock
768  * @table: array of divider/value pairs ending with a div set to 0
769  * @lock: shared register lock for this clock
770  */
771 #define clk_hw_register_divider_table_parent_hw(dev, name, parent_hw, flags,  \
772                                                 reg, shift, width,            \
773                                                 clk_divider_flags, table,     \
774                                                 lock)                         \
775         __clk_hw_register_divider((dev), NULL, (name), NULL, (parent_hw),     \
776                                   NULL, (flags), (reg), (shift), (width),     \
777                                   (clk_divider_flags), (table), (lock))
778 /**
779  * clk_hw_register_divider_table_parent_data - register a table based divider
780  * clock with the clock framework
781  * @dev: device registering this clock
782  * @name: name of this clock
783  * @parent_data: parent clk data
784  * @flags: framework-specific flags
785  * @reg: register address to adjust divider
786  * @shift: number of bits to shift the bitfield
787  * @width: width of the bitfield
788  * @clk_divider_flags: divider-specific flags for this clock
789  * @table: array of divider/value pairs ending with a div set to 0
790  * @lock: shared register lock for this clock
791  */
792 #define clk_hw_register_divider_table_parent_data(dev, name, parent_data,     \
793                                                   flags, reg, shift, width,   \
794                                                   clk_divider_flags, table,   \
795                                                   lock)                       \
796         __clk_hw_register_divider((dev), NULL, (name), NULL, NULL,            \
797                                   (parent_data), (flags), (reg), (shift),     \
798                                   (width), (clk_divider_flags), (table),      \
799                                   (lock))
800 /**
801  * devm_clk_hw_register_divider - register a divider clock with the clock framework
802  * @dev: device registering this clock
803  * @name: name of this clock
804  * @parent_name: name of clock's parent
805  * @flags: framework-specific flags
806  * @reg: register address to adjust divider
807  * @shift: number of bits to shift the bitfield
808  * @width: width of the bitfield
809  * @clk_divider_flags: divider-specific flags for this clock
810  * @lock: shared register lock for this clock
811  */
812 #define devm_clk_hw_register_divider(dev, name, parent_name, flags, reg, shift,    \
813                                 width, clk_divider_flags, lock)               \
814         __devm_clk_hw_register_divider((dev), NULL, (name), (parent_name), NULL,   \
815                                   NULL, (flags), (reg), (shift), (width),     \
816                                   (clk_divider_flags), NULL, (lock))
817 /**
818  * devm_clk_hw_register_divider_table - register a table based divider clock
819  * with the clock framework (devres variant)
820  * @dev: device registering this clock
821  * @name: name of this clock
822  * @parent_name: name of clock's parent
823  * @flags: framework-specific flags
824  * @reg: register address to adjust divider
825  * @shift: number of bits to shift the bitfield
826  * @width: width of the bitfield
827  * @clk_divider_flags: divider-specific flags for this clock
828  * @table: array of divider/value pairs ending with a div set to 0
829  * @lock: shared register lock for this clock
830  */
831 #define devm_clk_hw_register_divider_table(dev, name, parent_name, flags,     \
832                                            reg, shift, width,                 \
833                                            clk_divider_flags, table, lock)    \
834         __devm_clk_hw_register_divider((dev), NULL, (name), (parent_name),    \
835                                        NULL, NULL, (flags), (reg), (shift),   \
836                                        (width), (clk_divider_flags), (table), \
837                                        (lock))
838
839 void clk_unregister_divider(struct clk *clk);
840 void clk_hw_unregister_divider(struct clk_hw *hw);
841
842 /**
843  * struct clk_mux - multiplexer clock
844  *
845  * @hw:         handle between common and hardware-specific interfaces
846  * @reg:        register controlling multiplexer
847  * @table:      array of register values corresponding to the parent index
848  * @shift:      shift to multiplexer bit field
849  * @mask:       mask of mutliplexer bit field
850  * @flags:      hardware-specific flags
851  * @lock:       register lock
852  *
853  * Clock with multiple selectable parents.  Implements .get_parent, .set_parent
854  * and .recalc_rate
855  *
856  * Flags:
857  * CLK_MUX_INDEX_ONE - register index starts at 1, not 0
858  * CLK_MUX_INDEX_BIT - register index is a single bit (power of two)
859  * CLK_MUX_HIWORD_MASK - The mux settings are only in lower 16-bit of this
860  *      register, and mask of mux bits are in higher 16-bit of this register.
861  *      While setting the mux bits, higher 16-bit should also be updated to
862  *      indicate changing mux bits.
863  * CLK_MUX_READ_ONLY - The mux registers can't be written, only read in the
864  *      .get_parent clk_op.
865  * CLK_MUX_ROUND_CLOSEST - Use the parent rate that is closest to the desired
866  *      frequency.
867  * CLK_MUX_BIG_ENDIAN - By default little endian register accesses are used for
868  *      the mux register.  Setting this flag makes the register accesses big
869  *      endian.
870  */
871 struct clk_mux {
872         struct clk_hw   hw;
873         void __iomem    *reg;
874         u32             *table;
875         u32             mask;
876         u8              shift;
877         u8              flags;
878         spinlock_t      *lock;
879 };
880
881 #define to_clk_mux(_hw) container_of(_hw, struct clk_mux, hw)
882
883 #define CLK_MUX_INDEX_ONE               BIT(0)
884 #define CLK_MUX_INDEX_BIT               BIT(1)
885 #define CLK_MUX_HIWORD_MASK             BIT(2)
886 #define CLK_MUX_READ_ONLY               BIT(3) /* mux can't be changed */
887 #define CLK_MUX_ROUND_CLOSEST           BIT(4)
888 #define CLK_MUX_BIG_ENDIAN              BIT(5)
889
890 extern const struct clk_ops clk_mux_ops;
891 extern const struct clk_ops clk_mux_ro_ops;
892
893 struct clk_hw *__clk_hw_register_mux(struct device *dev, struct device_node *np,
894                 const char *name, u8 num_parents,
895                 const char * const *parent_names,
896                 const struct clk_hw **parent_hws,
897                 const struct clk_parent_data *parent_data,
898                 unsigned long flags, void __iomem *reg, u8 shift, u32 mask,
899                 u8 clk_mux_flags, u32 *table, spinlock_t *lock);
900 struct clk_hw *__devm_clk_hw_register_mux(struct device *dev, struct device_node *np,
901                 const char *name, u8 num_parents,
902                 const char * const *parent_names,
903                 const struct clk_hw **parent_hws,
904                 const struct clk_parent_data *parent_data,
905                 unsigned long flags, void __iomem *reg, u8 shift, u32 mask,
906                 u8 clk_mux_flags, u32 *table, spinlock_t *lock);
907 struct clk *clk_register_mux_table(struct device *dev, const char *name,
908                 const char * const *parent_names, u8 num_parents,
909                 unsigned long flags, void __iomem *reg, u8 shift, u32 mask,
910                 u8 clk_mux_flags, u32 *table, spinlock_t *lock);
911
912 #define clk_register_mux(dev, name, parent_names, num_parents, flags, reg,    \
913                          shift, width, clk_mux_flags, lock)                   \
914         clk_register_mux_table((dev), (name), (parent_names), (num_parents),  \
915                                (flags), (reg), (shift), BIT((width)) - 1,     \
916                                (clk_mux_flags), NULL, (lock))
917 #define clk_hw_register_mux_table(dev, name, parent_names, num_parents,       \
918                                   flags, reg, shift, mask, clk_mux_flags,     \
919                                   table, lock)                                \
920         __clk_hw_register_mux((dev), NULL, (name), (num_parents),             \
921                               (parent_names), NULL, NULL, (flags), (reg),     \
922                               (shift), (mask), (clk_mux_flags), (table),      \
923                               (lock))
924 #define clk_hw_register_mux(dev, name, parent_names, num_parents, flags, reg, \
925                             shift, width, clk_mux_flags, lock)                \
926         __clk_hw_register_mux((dev), NULL, (name), (num_parents),             \
927                               (parent_names), NULL, NULL, (flags), (reg),     \
928                               (shift), BIT((width)) - 1, (clk_mux_flags),     \
929                               NULL, (lock))
930 #define clk_hw_register_mux_hws(dev, name, parent_hws, num_parents, flags,    \
931                                 reg, shift, width, clk_mux_flags, lock)       \
932         __clk_hw_register_mux((dev), NULL, (name), (num_parents), NULL,       \
933                               (parent_hws), NULL, (flags), (reg), (shift),    \
934                               BIT((width)) - 1, (clk_mux_flags), NULL, (lock))
935 #define clk_hw_register_mux_parent_data(dev, name, parent_data, num_parents,  \
936                                         flags, reg, shift, width,             \
937                                         clk_mux_flags, lock)                  \
938         __clk_hw_register_mux((dev), NULL, (name), (num_parents), NULL, NULL, \
939                               (parent_data), (flags), (reg), (shift),         \
940                               BIT((width)) - 1, (clk_mux_flags), NULL, (lock))
941 #define devm_clk_hw_register_mux(dev, name, parent_names, num_parents, flags, reg, \
942                             shift, width, clk_mux_flags, lock)                \
943         __devm_clk_hw_register_mux((dev), NULL, (name), (num_parents),        \
944                               (parent_names), NULL, NULL, (flags), (reg),     \
945                               (shift), BIT((width)) - 1, (clk_mux_flags),     \
946                               NULL, (lock))
947
948 int clk_mux_val_to_index(struct clk_hw *hw, u32 *table, unsigned int flags,
949                          unsigned int val);
950 unsigned int clk_mux_index_to_val(u32 *table, unsigned int flags, u8 index);
951
952 void clk_unregister_mux(struct clk *clk);
953 void clk_hw_unregister_mux(struct clk_hw *hw);
954
955 void of_fixed_factor_clk_setup(struct device_node *node);
956
957 /**
958  * struct clk_fixed_factor - fixed multiplier and divider clock
959  *
960  * @hw:         handle between common and hardware-specific interfaces
961  * @mult:       multiplier
962  * @div:        divider
963  *
964  * Clock with a fixed multiplier and divider. The output frequency is the
965  * parent clock rate divided by div and multiplied by mult.
966  * Implements .recalc_rate, .set_rate and .round_rate
967  */
968
969 struct clk_fixed_factor {
970         struct clk_hw   hw;
971         unsigned int    mult;
972         unsigned int    div;
973 };
974
975 #define to_clk_fixed_factor(_hw) container_of(_hw, struct clk_fixed_factor, hw)
976
977 extern const struct clk_ops clk_fixed_factor_ops;
978 struct clk *clk_register_fixed_factor(struct device *dev, const char *name,
979                 const char *parent_name, unsigned long flags,
980                 unsigned int mult, unsigned int div);
981 void clk_unregister_fixed_factor(struct clk *clk);
982 struct clk_hw *clk_hw_register_fixed_factor(struct device *dev,
983                 const char *name, const char *parent_name, unsigned long flags,
984                 unsigned int mult, unsigned int div);
985 void clk_hw_unregister_fixed_factor(struct clk_hw *hw);
986 struct clk_hw *devm_clk_hw_register_fixed_factor(struct device *dev,
987                 const char *name, const char *parent_name, unsigned long flags,
988                 unsigned int mult, unsigned int div);
989 /**
990  * struct clk_fractional_divider - adjustable fractional divider clock
991  *
992  * @hw:         handle between common and hardware-specific interfaces
993  * @reg:        register containing the divider
994  * @mshift:     shift to the numerator bit field
995  * @mwidth:     width of the numerator bit field
996  * @nshift:     shift to the denominator bit field
997  * @nwidth:     width of the denominator bit field
998  * @lock:       register lock
999  *
1000  * Clock with adjustable fractional divider affecting its output frequency.
1001  *
1002  * Flags:
1003  * CLK_FRAC_DIVIDER_ZERO_BASED - by default the numerator and denominator
1004  *      is the value read from the register. If CLK_FRAC_DIVIDER_ZERO_BASED
1005  *      is set then the numerator and denominator are both the value read
1006  *      plus one.
1007  * CLK_FRAC_DIVIDER_BIG_ENDIAN - By default little endian register accesses are
1008  *      used for the divider register.  Setting this flag makes the register
1009  *      accesses big endian.
1010  * CLK_FRAC_DIVIDER_POWER_OF_TWO_PS - By default the resulting fraction might
1011  *      be saturated and the caller will get quite far from the good enough
1012  *      approximation. Instead the caller may require, by setting this flag,
1013  *      to shift left by a few bits in case, when the asked one is quite small
1014  *      to satisfy the desired range of denominator. It assumes that on the
1015  *      caller's side the power-of-two capable prescaler exists.
1016  */
1017 struct clk_fractional_divider {
1018         struct clk_hw   hw;
1019         void __iomem    *reg;
1020         u8              mshift;
1021         u8              mwidth;
1022         u32             mmask;
1023         u8              nshift;
1024         u8              nwidth;
1025         u32             nmask;
1026         u8              flags;
1027         void            (*approximation)(struct clk_hw *hw,
1028                                 unsigned long rate, unsigned long *parent_rate,
1029                                 unsigned long *m, unsigned long *n);
1030         spinlock_t      *lock;
1031 };
1032
1033 #define to_clk_fd(_hw) container_of(_hw, struct clk_fractional_divider, hw)
1034
1035 #define CLK_FRAC_DIVIDER_ZERO_BASED             BIT(0)
1036 #define CLK_FRAC_DIVIDER_BIG_ENDIAN             BIT(1)
1037 #define CLK_FRAC_DIVIDER_POWER_OF_TWO_PS        BIT(2)
1038
1039 struct clk *clk_register_fractional_divider(struct device *dev,
1040                 const char *name, const char *parent_name, unsigned long flags,
1041                 void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth,
1042                 u8 clk_divider_flags, spinlock_t *lock);
1043 struct clk_hw *clk_hw_register_fractional_divider(struct device *dev,
1044                 const char *name, const char *parent_name, unsigned long flags,
1045                 void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth,
1046                 u8 clk_divider_flags, spinlock_t *lock);
1047 void clk_hw_unregister_fractional_divider(struct clk_hw *hw);
1048
1049 /**
1050  * struct clk_multiplier - adjustable multiplier clock
1051  *
1052  * @hw:         handle between common and hardware-specific interfaces
1053  * @reg:        register containing the multiplier
1054  * @shift:      shift to the multiplier bit field
1055  * @width:      width of the multiplier bit field
1056  * @lock:       register lock
1057  *
1058  * Clock with an adjustable multiplier affecting its output frequency.
1059  * Implements .recalc_rate, .set_rate and .round_rate
1060  *
1061  * Flags:
1062  * CLK_MULTIPLIER_ZERO_BYPASS - By default, the multiplier is the value read
1063  *      from the register, with 0 being a valid value effectively
1064  *      zeroing the output clock rate. If CLK_MULTIPLIER_ZERO_BYPASS is
1065  *      set, then a null multiplier will be considered as a bypass,
1066  *      leaving the parent rate unmodified.
1067  * CLK_MULTIPLIER_ROUND_CLOSEST - Makes the best calculated divider to be
1068  *      rounded to the closest integer instead of the down one.
1069  * CLK_MULTIPLIER_BIG_ENDIAN - By default little endian register accesses are
1070  *      used for the multiplier register.  Setting this flag makes the register
1071  *      accesses big endian.
1072  */
1073 struct clk_multiplier {
1074         struct clk_hw   hw;
1075         void __iomem    *reg;
1076         u8              shift;
1077         u8              width;
1078         u8              flags;
1079         spinlock_t      *lock;
1080 };
1081
1082 #define to_clk_multiplier(_hw) container_of(_hw, struct clk_multiplier, hw)
1083
1084 #define CLK_MULTIPLIER_ZERO_BYPASS      BIT(0)
1085 #define CLK_MULTIPLIER_ROUND_CLOSEST    BIT(1)
1086 #define CLK_MULTIPLIER_BIG_ENDIAN       BIT(2)
1087
1088 extern const struct clk_ops clk_multiplier_ops;
1089
1090 /***
1091  * struct clk_composite - aggregate clock of mux, divider and gate clocks
1092  *
1093  * @hw:         handle between common and hardware-specific interfaces
1094  * @mux_hw:     handle between composite and hardware-specific mux clock
1095  * @rate_hw:    handle between composite and hardware-specific rate clock
1096  * @gate_hw:    handle between composite and hardware-specific gate clock
1097  * @mux_ops:    clock ops for mux
1098  * @rate_ops:   clock ops for rate
1099  * @gate_ops:   clock ops for gate
1100  */
1101 struct clk_composite {
1102         struct clk_hw   hw;
1103         struct clk_ops  ops;
1104
1105         struct clk_hw   *mux_hw;
1106         struct clk_hw   *rate_hw;
1107         struct clk_hw   *gate_hw;
1108
1109         const struct clk_ops    *mux_ops;
1110         const struct clk_ops    *rate_ops;
1111         const struct clk_ops    *gate_ops;
1112 };
1113
1114 #define to_clk_composite(_hw) container_of(_hw, struct clk_composite, hw)
1115
1116 struct clk *clk_register_composite(struct device *dev, const char *name,
1117                 const char * const *parent_names, int num_parents,
1118                 struct clk_hw *mux_hw, const struct clk_ops *mux_ops,
1119                 struct clk_hw *rate_hw, const struct clk_ops *rate_ops,
1120                 struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
1121                 unsigned long flags);
1122 struct clk *clk_register_composite_pdata(struct device *dev, const char *name,
1123                 const struct clk_parent_data *parent_data, int num_parents,
1124                 struct clk_hw *mux_hw, const struct clk_ops *mux_ops,
1125                 struct clk_hw *rate_hw, const struct clk_ops *rate_ops,
1126                 struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
1127                 unsigned long flags);
1128 void clk_unregister_composite(struct clk *clk);
1129 struct clk_hw *clk_hw_register_composite(struct device *dev, const char *name,
1130                 const char * const *parent_names, int num_parents,
1131                 struct clk_hw *mux_hw, const struct clk_ops *mux_ops,
1132                 struct clk_hw *rate_hw, const struct clk_ops *rate_ops,
1133                 struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
1134                 unsigned long flags);
1135 struct clk_hw *clk_hw_register_composite_pdata(struct device *dev,
1136                 const char *name,
1137                 const struct clk_parent_data *parent_data, int num_parents,
1138                 struct clk_hw *mux_hw, const struct clk_ops *mux_ops,
1139                 struct clk_hw *rate_hw, const struct clk_ops *rate_ops,
1140                 struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
1141                 unsigned long flags);
1142 struct clk_hw *devm_clk_hw_register_composite_pdata(struct device *dev,
1143                 const char *name, const struct clk_parent_data *parent_data,
1144                 int num_parents,
1145                 struct clk_hw *mux_hw, const struct clk_ops *mux_ops,
1146                 struct clk_hw *rate_hw, const struct clk_ops *rate_ops,
1147                 struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
1148                 unsigned long flags);
1149 void clk_hw_unregister_composite(struct clk_hw *hw);
1150
1151 struct clk *clk_register(struct device *dev, struct clk_hw *hw);
1152 struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw);
1153
1154 int __must_check clk_hw_register(struct device *dev, struct clk_hw *hw);
1155 int __must_check devm_clk_hw_register(struct device *dev, struct clk_hw *hw);
1156 int __must_check of_clk_hw_register(struct device_node *node, struct clk_hw *hw);
1157
1158 void clk_unregister(struct clk *clk);
1159 void devm_clk_unregister(struct device *dev, struct clk *clk);
1160
1161 void clk_hw_unregister(struct clk_hw *hw);
1162 void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw);
1163
1164 /* helper functions */
1165 const char *__clk_get_name(const struct clk *clk);
1166 const char *clk_hw_get_name(const struct clk_hw *hw);
1167 #ifdef CONFIG_COMMON_CLK
1168 struct clk_hw *__clk_get_hw(struct clk *clk);
1169 #else
1170 static inline struct clk_hw *__clk_get_hw(struct clk *clk)
1171 {
1172         return (struct clk_hw *)clk;
1173 }
1174 #endif
1175
1176 struct clk *clk_hw_get_clk(struct clk_hw *hw, const char *con_id);
1177 struct clk *devm_clk_hw_get_clk(struct device *dev, struct clk_hw *hw,
1178                                 const char *con_id);
1179
1180 unsigned int clk_hw_get_num_parents(const struct clk_hw *hw);
1181 struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw);
1182 struct clk_hw *clk_hw_get_parent_by_index(const struct clk_hw *hw,
1183                                           unsigned int index);
1184 int clk_hw_get_parent_index(struct clk_hw *hw);
1185 int clk_hw_set_parent(struct clk_hw *hw, struct clk_hw *new_parent);
1186 unsigned int __clk_get_enable_count(struct clk *clk);
1187 unsigned long clk_hw_get_rate(const struct clk_hw *hw);
1188 unsigned long clk_hw_get_flags(const struct clk_hw *hw);
1189 #define clk_hw_can_set_rate_parent(hw) \
1190         (clk_hw_get_flags((hw)) & CLK_SET_RATE_PARENT)
1191
1192 bool clk_hw_is_prepared(const struct clk_hw *hw);
1193 bool clk_hw_rate_is_protected(const struct clk_hw *hw);
1194 bool clk_hw_is_enabled(const struct clk_hw *hw);
1195 bool __clk_is_enabled(struct clk *clk);
1196 struct clk *__clk_lookup(const char *name);
1197 int __clk_mux_determine_rate(struct clk_hw *hw,
1198                              struct clk_rate_request *req);
1199 int __clk_determine_rate(struct clk_hw *core, struct clk_rate_request *req);
1200 int __clk_mux_determine_rate_closest(struct clk_hw *hw,
1201                                      struct clk_rate_request *req);
1202 int clk_mux_determine_rate_flags(struct clk_hw *hw,
1203                                  struct clk_rate_request *req,
1204                                  unsigned long flags);
1205 void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent);
1206 void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
1207                            unsigned long max_rate);
1208
1209 static inline void __clk_hw_set_clk(struct clk_hw *dst, struct clk_hw *src)
1210 {
1211         dst->clk = src->clk;
1212         dst->core = src->core;
1213 }
1214
1215 static inline long divider_round_rate(struct clk_hw *hw, unsigned long rate,
1216                                       unsigned long *prate,
1217                                       const struct clk_div_table *table,
1218                                       u8 width, unsigned long flags)
1219 {
1220         return divider_round_rate_parent(hw, clk_hw_get_parent(hw),
1221                                          rate, prate, table, width, flags);
1222 }
1223
1224 static inline long divider_ro_round_rate(struct clk_hw *hw, unsigned long rate,
1225                                          unsigned long *prate,
1226                                          const struct clk_div_table *table,
1227                                          u8 width, unsigned long flags,
1228                                          unsigned int val)
1229 {
1230         return divider_ro_round_rate_parent(hw, clk_hw_get_parent(hw),
1231                                             rate, prate, table, width, flags,
1232                                             val);
1233 }
1234
1235 /*
1236  * FIXME clock api without lock protection
1237  */
1238 unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate);
1239
1240 struct clk_onecell_data {
1241         struct clk **clks;
1242         unsigned int clk_num;
1243 };
1244
1245 struct clk_hw_onecell_data {
1246         unsigned int num;
1247         struct clk_hw *hws[];
1248 };
1249
1250 #define CLK_OF_DECLARE(name, compat, fn) OF_DECLARE_1(clk, name, compat, fn)
1251
1252 /*
1253  * Use this macro when you have a driver that requires two initialization
1254  * routines, one at of_clk_init(), and one at platform device probe
1255  */
1256 #define CLK_OF_DECLARE_DRIVER(name, compat, fn) \
1257         static void __init name##_of_clk_init_driver(struct device_node *np) \
1258         {                                                               \
1259                 of_node_clear_flag(np, OF_POPULATED);                   \
1260                 fn(np);                                                 \
1261         }                                                               \
1262         OF_DECLARE_1(clk, name, compat, name##_of_clk_init_driver)
1263
1264 #define CLK_HW_INIT(_name, _parent, _ops, _flags)               \
1265         (&(struct clk_init_data) {                              \
1266                 .flags          = _flags,                       \
1267                 .name           = _name,                        \
1268                 .parent_names   = (const char *[]) { _parent }, \
1269                 .num_parents    = 1,                            \
1270                 .ops            = _ops,                         \
1271         })
1272
1273 #define CLK_HW_INIT_HW(_name, _parent, _ops, _flags)                    \
1274         (&(struct clk_init_data) {                                      \
1275                 .flags          = _flags,                               \
1276                 .name           = _name,                                \
1277                 .parent_hws     = (const struct clk_hw*[]) { _parent }, \
1278                 .num_parents    = 1,                                    \
1279                 .ops            = _ops,                                 \
1280         })
1281
1282 /*
1283  * This macro is intended for drivers to be able to share the otherwise
1284  * individual struct clk_hw[] compound literals created by the compiler
1285  * when using CLK_HW_INIT_HW. It does NOT support multiple parents.
1286  */
1287 #define CLK_HW_INIT_HWS(_name, _parent, _ops, _flags)                   \
1288         (&(struct clk_init_data) {                                      \
1289                 .flags          = _flags,                               \
1290                 .name           = _name,                                \
1291                 .parent_hws     = _parent,                              \
1292                 .num_parents    = 1,                                    \
1293                 .ops            = _ops,                                 \
1294         })
1295
1296 #define CLK_HW_INIT_FW_NAME(_name, _parent, _ops, _flags)               \
1297         (&(struct clk_init_data) {                                      \
1298                 .flags          = _flags,                               \
1299                 .name           = _name,                                \
1300                 .parent_data    = (const struct clk_parent_data[]) {    \
1301                                         { .fw_name = _parent },         \
1302                                   },                                    \
1303                 .num_parents    = 1,                                    \
1304                 .ops            = _ops,                                 \
1305         })
1306
1307 #define CLK_HW_INIT_PARENTS(_name, _parents, _ops, _flags)      \
1308         (&(struct clk_init_data) {                              \
1309                 .flags          = _flags,                       \
1310                 .name           = _name,                        \
1311                 .parent_names   = _parents,                     \
1312                 .num_parents    = ARRAY_SIZE(_parents),         \
1313                 .ops            = _ops,                         \
1314         })
1315
1316 #define CLK_HW_INIT_PARENTS_HW(_name, _parents, _ops, _flags)   \
1317         (&(struct clk_init_data) {                              \
1318                 .flags          = _flags,                       \
1319                 .name           = _name,                        \
1320                 .parent_hws     = _parents,                     \
1321                 .num_parents    = ARRAY_SIZE(_parents),         \
1322                 .ops            = _ops,                         \
1323         })
1324
1325 #define CLK_HW_INIT_PARENTS_DATA(_name, _parents, _ops, _flags) \
1326         (&(struct clk_init_data) {                              \
1327                 .flags          = _flags,                       \
1328                 .name           = _name,                        \
1329                 .parent_data    = _parents,                     \
1330                 .num_parents    = ARRAY_SIZE(_parents),         \
1331                 .ops            = _ops,                         \
1332         })
1333
1334 #define CLK_HW_INIT_NO_PARENT(_name, _ops, _flags)      \
1335         (&(struct clk_init_data) {                      \
1336                 .flags          = _flags,               \
1337                 .name           = _name,                \
1338                 .parent_names   = NULL,                 \
1339                 .num_parents    = 0,                    \
1340                 .ops            = _ops,                 \
1341         })
1342
1343 #define CLK_FIXED_FACTOR(_struct, _name, _parent,                       \
1344                         _div, _mult, _flags)                            \
1345         struct clk_fixed_factor _struct = {                             \
1346                 .div            = _div,                                 \
1347                 .mult           = _mult,                                \
1348                 .hw.init        = CLK_HW_INIT(_name,                    \
1349                                               _parent,                  \
1350                                               &clk_fixed_factor_ops,    \
1351                                               _flags),                  \
1352         }
1353
1354 #define CLK_FIXED_FACTOR_HW(_struct, _name, _parent,                    \
1355                             _div, _mult, _flags)                        \
1356         struct clk_fixed_factor _struct = {                             \
1357                 .div            = _div,                                 \
1358                 .mult           = _mult,                                \
1359                 .hw.init        = CLK_HW_INIT_HW(_name,                 \
1360                                                  _parent,               \
1361                                                  &clk_fixed_factor_ops, \
1362                                                  _flags),               \
1363         }
1364
1365 /*
1366  * This macro allows the driver to reuse the _parent array for multiple
1367  * fixed factor clk declarations.
1368  */
1369 #define CLK_FIXED_FACTOR_HWS(_struct, _name, _parent,                   \
1370                              _div, _mult, _flags)                       \
1371         struct clk_fixed_factor _struct = {                             \
1372                 .div            = _div,                                 \
1373                 .mult           = _mult,                                \
1374                 .hw.init        = CLK_HW_INIT_HWS(_name,                \
1375                                                   _parent,              \
1376                                                   &clk_fixed_factor_ops, \
1377                                                   _flags),      \
1378         }
1379
1380 #define CLK_FIXED_FACTOR_FW_NAME(_struct, _name, _parent,               \
1381                                  _div, _mult, _flags)                   \
1382         struct clk_fixed_factor _struct = {                             \
1383                 .div            = _div,                                 \
1384                 .mult           = _mult,                                \
1385                 .hw.init        = CLK_HW_INIT_FW_NAME(_name,            \
1386                                                       _parent,          \
1387                                                       &clk_fixed_factor_ops, \
1388                                                       _flags),          \
1389         }
1390
1391 #ifdef CONFIG_OF
1392 int of_clk_add_provider(struct device_node *np,
1393                         struct clk *(*clk_src_get)(struct of_phandle_args *args,
1394                                                    void *data),
1395                         void *data);
1396 int of_clk_add_hw_provider(struct device_node *np,
1397                            struct clk_hw *(*get)(struct of_phandle_args *clkspec,
1398                                                  void *data),
1399                            void *data);
1400 int devm_of_clk_add_hw_provider(struct device *dev,
1401                            struct clk_hw *(*get)(struct of_phandle_args *clkspec,
1402                                                  void *data),
1403                            void *data);
1404 void of_clk_del_provider(struct device_node *np);
1405 void devm_of_clk_del_provider(struct device *dev);
1406 struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
1407                                   void *data);
1408 struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec,
1409                                     void *data);
1410 struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data);
1411 struct clk_hw *of_clk_hw_onecell_get(struct of_phandle_args *clkspec,
1412                                      void *data);
1413 int of_clk_parent_fill(struct device_node *np, const char **parents,
1414                        unsigned int size);
1415 int of_clk_detect_critical(struct device_node *np, int index,
1416                             unsigned long *flags);
1417
1418 #else /* !CONFIG_OF */
1419
1420 static inline int of_clk_add_provider(struct device_node *np,
1421                         struct clk *(*clk_src_get)(struct of_phandle_args *args,
1422                                                    void *data),
1423                         void *data)
1424 {
1425         return 0;
1426 }
1427 static inline int of_clk_add_hw_provider(struct device_node *np,
1428                         struct clk_hw *(*get)(struct of_phandle_args *clkspec,
1429                                               void *data),
1430                         void *data)
1431 {
1432         return 0;
1433 }
1434 static inline int devm_of_clk_add_hw_provider(struct device *dev,
1435                            struct clk_hw *(*get)(struct of_phandle_args *clkspec,
1436                                                  void *data),
1437                            void *data)
1438 {
1439         return 0;
1440 }
1441 static inline void of_clk_del_provider(struct device_node *np) {}
1442 static inline void devm_of_clk_del_provider(struct device *dev) {}
1443 static inline struct clk *of_clk_src_simple_get(
1444         struct of_phandle_args *clkspec, void *data)
1445 {
1446         return ERR_PTR(-ENOENT);
1447 }
1448 static inline struct clk_hw *
1449 of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
1450 {
1451         return ERR_PTR(-ENOENT);
1452 }
1453 static inline struct clk *of_clk_src_onecell_get(
1454         struct of_phandle_args *clkspec, void *data)
1455 {
1456         return ERR_PTR(-ENOENT);
1457 }
1458 static inline struct clk_hw *
1459 of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
1460 {
1461         return ERR_PTR(-ENOENT);
1462 }
1463 static inline int of_clk_parent_fill(struct device_node *np,
1464                                      const char **parents, unsigned int size)
1465 {
1466         return 0;
1467 }
1468 static inline int of_clk_detect_critical(struct device_node *np, int index,
1469                                           unsigned long *flags)
1470 {
1471         return 0;
1472 }
1473 #endif /* CONFIG_OF */
1474
1475 void clk_gate_restore_context(struct clk_hw *hw);
1476
1477 #endif /* CLK_PROVIDER_H */