1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (c) 2010-2011 Jeremy Kerr <jeremy.kerr@canonical.com>
4 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
6 #ifndef __LINUX_CLK_PROVIDER_H
7 #define __LINUX_CLK_PROVIDER_H
11 #include <linux/of_clk.h>
13 #ifdef CONFIG_COMMON_CLK
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
20 * Please update clk_flags[] in drivers/clk/clk.c when making changes here!
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 */
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)
45 * struct clk_rate_request - Structure encoding the clk constraints that
46 * a clock user might require.
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.
58 struct clk_rate_request {
60 unsigned long min_rate;
61 unsigned long max_rate;
62 unsigned long best_parent_rate;
63 struct clk_hw *best_parent_hw;
67 * struct clk_duty - Struture encoding the duty cycle ratio of a clock
69 * @num: Numerator of the duty cycle ratio
70 * @den: Denominator of the duty cycle ratio
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.
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
88 * @unprepare: Release the clock from its prepared state. This will typically
89 * undo any work done in the @prepare callback. Called with
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.
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.
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
105 * @disable: Disable the clock atomically. Called with enable_lock held.
106 * This function must not sleep.
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.
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
117 * @save_context: Save the context of the clock in prepration for poweroff.
119 * @restore_context: Restore the context of the clock after a restoration
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.
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
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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,
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);
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)
259 struct clk_parent_data {
260 const struct clk_hw *hw;
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.
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
279 struct clk_init_data {
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;
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
296 * @core: pointer to the struct clk_core instance that points back to this
297 * struct clk_hw instance
299 * @clk: pointer to the per-user struct clk instance that can be used to call
302 * @init: pointer to struct clk_init_data that contains the init data shared
303 * with the common clock framework.
306 struct clk_core *core;
308 const struct clk_init_data *init;
312 * DOC: Basic clock implementations common to many platforms
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
321 * struct clk_fixed_rate - fixed-rate clock
322 * @hw: handle between common and hardware-specific interfaces
323 * @fixed_rate: constant frequency of clock
325 struct clk_fixed_rate {
327 unsigned long fixed_rate;
328 unsigned long fixed_accuracy;
332 #define to_clk_fixed_rate(_hw) container_of(_hw, struct clk_fixed_rate, hw)
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);
350 void of_fixed_clk_setup(struct device_node *np);
353 * struct clk_gate - gating clock
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
361 * Clock which can gate its output. Implements .enable & .disable
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.
380 #define to_clk_gate(_hw) container_of(_hw, struct clk_gate, hw)
382 #define CLK_GATE_SET_TO_DISABLE BIT(0)
383 #define CLK_GATE_HIWORD_MASK BIT(1)
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);
398 struct clk_div_table {
404 * struct clk_divider - adjustable divider clock
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
413 * Clock with an adjustable divider affecting its output frequency. Implements
414 * .recalc_rate, .set_rate and .round_rate
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.
446 const struct clk_div_table *table;
450 #define clk_div_mask(width) ((1 << (width)) - 1)
451 #define to_clk_divider(_hw) container_of(_hw, struct clk_divider, hw)
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)
461 extern const struct clk_ops clk_divider_ops;
462 extern const struct clk_ops clk_divider_ro_ops;
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);
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,
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,
497 void clk_unregister_divider(struct clk *clk);
498 void clk_hw_unregister_divider(struct clk_hw *hw);
501 * struct clk_mux - multiplexer clock
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
511 * Clock with multiple selectable parents. Implements .get_parent, .set_parent
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
534 #define to_clk_mux(_hw) container_of(_hw, struct clk_mux, hw)
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)
542 extern const struct clk_ops clk_mux_ops;
543 extern const struct clk_ops clk_mux_ro_ops;
545 struct clk *clk_register_mux(struct device *dev, const char *name,
546 const char * const *parent_names, u8 num_parents,
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,
553 void __iomem *reg, u8 shift, u8 width,
554 u8 clk_mux_flags, spinlock_t *lock);
556 struct clk *clk_register_mux_table(struct device *dev, const char *name,
557 const char * const *parent_names, u8 num_parents,
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,
564 void __iomem *reg, u8 shift, u32 mask,
565 u8 clk_mux_flags, u32 *table, spinlock_t *lock);
567 int clk_mux_val_to_index(struct clk_hw *hw, u32 *table, unsigned int flags,
569 unsigned int clk_mux_index_to_val(u32 *table, unsigned int flags, u8 index);
571 void clk_unregister_mux(struct clk *clk);
572 void clk_hw_unregister_mux(struct clk_hw *hw);
574 void of_fixed_factor_clk_setup(struct device_node *node);
577 * struct clk_fixed_factor - fixed multiplier and divider clock
579 * @hw: handle between common and hardware-specific interfaces
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
588 struct clk_fixed_factor {
594 #define to_clk_fixed_factor(_hw) container_of(_hw, struct clk_fixed_factor, hw)
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);
607 * struct clk_fractional_divider - adjustable fractional divider clock
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
617 * Clock with adjustable fractional divider affecting its output frequency.
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
625 struct clk_fractional_divider {
635 void (*approximation)(struct clk_hw *hw,
636 unsigned long rate, unsigned long *parent_rate,
637 unsigned long *m, unsigned long *n);
641 #define to_clk_fd(_hw) container_of(_hw, struct clk_fractional_divider, hw)
643 #define CLK_FRAC_DIVIDER_ZERO_BASED BIT(0)
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);
657 * struct clk_multiplier - adjustable multiplier clock
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
665 * Clock with an adjustable multiplier affecting its output frequency.
666 * Implements .recalc_rate, .set_rate and .round_rate
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.
677 struct clk_multiplier {
686 #define to_clk_multiplier(_hw) container_of(_hw, struct clk_multiplier, hw)
688 #define CLK_MULTIPLIER_ZERO_BYPASS BIT(0)
689 #define CLK_MULTIPLIER_ROUND_CLOSEST BIT(1)
691 extern const struct clk_ops clk_multiplier_ops;
694 * struct clk_composite - aggregate clock of mux, divider and gate clocks
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
704 struct clk_composite {
708 struct clk_hw *mux_hw;
709 struct clk_hw *rate_hw;
710 struct clk_hw *gate_hw;
712 const struct clk_ops *mux_ops;
713 const struct clk_ops *rate_ops;
714 const struct clk_ops *gate_ops;
717 #define to_clk_composite(_hw) container_of(_hw, struct clk_composite, hw)
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);
735 * struct clk_gpio_gate - gpio gated clock
737 * @hw: handle between common and hardware-specific interfaces
738 * @gpiod: gpio descriptor
740 * Clock with a gpio control for enabling and disabling the parent clock.
741 * Implements .enable, .disable and .is_enabled
746 struct gpio_desc *gpiod;
749 #define to_clk_gpio(_hw) container_of(_hw, struct clk_gpio, hw)
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);
761 * struct clk_gpio_mux - gpio controlled clock multiplexer
763 * @hw: see struct clk_gpio
764 * @gpiod: gpio descriptor to select the parent of this clock multiplexer
766 * Clock with a gpio control for selecting the parent clock.
767 * Implements .get_parent, .set_parent and .determine_rate
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);
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
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.
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);
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);
797 void clk_unregister(struct clk *clk);
798 void devm_clk_unregister(struct device *dev, struct clk *clk);
800 void clk_hw_unregister(struct clk_hw *hw);
801 void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw);
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,
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)
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);
835 static inline void __clk_hw_set_clk(struct clk_hw *dst, struct clk_hw *src)
838 dst->core = src->core;
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)
846 return divider_round_rate_parent(hw, clk_hw_get_parent(hw),
847 rate, prate, table, width, flags);
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,
856 return divider_ro_round_rate_parent(hw, clk_hw_get_parent(hw),
857 rate, prate, table, width, flags,
862 * FIXME clock api without lock protection
864 unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate);
868 struct clk_onecell_data {
870 unsigned int clk_num;
873 struct clk_hw_onecell_data {
875 struct clk_hw *hws[];
878 extern struct of_device_id __clk_of_table;
880 #define CLK_OF_DECLARE(name, compat, fn) OF_DECLARE_1(clk, name, compat, fn)
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
886 #define CLK_OF_DECLARE_DRIVER(name, compat, fn) \
887 static void __init name##_of_clk_init_driver(struct device_node *np) \
889 of_node_clear_flag(np, OF_POPULATED); \
892 OF_DECLARE_1(clk, name, compat, name##_of_clk_init_driver)
894 #define CLK_HW_INIT(_name, _parent, _ops, _flags) \
895 (&(struct clk_init_data) { \
898 .parent_names = (const char *[]) { _parent }, \
903 #define CLK_HW_INIT_PARENTS(_name, _parents, _ops, _flags) \
904 (&(struct clk_init_data) { \
907 .parent_names = _parents, \
908 .num_parents = ARRAY_SIZE(_parents), \
912 #define CLK_HW_INIT_NO_PARENT(_name, _ops, _flags) \
913 (&(struct clk_init_data) { \
916 .parent_names = NULL, \
921 #define CLK_FIXED_FACTOR(_struct, _name, _parent, \
922 _div, _mult, _flags) \
923 struct clk_fixed_factor _struct = { \
926 .hw.init = CLK_HW_INIT(_name, \
928 &clk_fixed_factor_ops, \
933 int of_clk_add_provider(struct device_node *np,
934 struct clk *(*clk_src_get)(struct of_phandle_args *args,
937 int of_clk_add_hw_provider(struct device_node *np,
938 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
941 int devm_of_clk_add_hw_provider(struct device *dev,
942 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
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,
949 struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec,
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,
954 int of_clk_parent_fill(struct device_node *np, const char **parents,
956 int of_clk_detect_critical(struct device_node *np, int index,
957 unsigned long *flags);
959 #else /* !CONFIG_OF */
961 static inline int of_clk_add_provider(struct device_node *np,
962 struct clk *(*clk_src_get)(struct of_phandle_args *args,
968 static inline int of_clk_add_hw_provider(struct device_node *np,
969 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
975 static inline int devm_of_clk_add_hw_provider(struct device *dev,
976 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
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)
987 return ERR_PTR(-ENOENT);
989 static inline struct clk_hw *
990 of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
992 return ERR_PTR(-ENOENT);
994 static inline struct clk *of_clk_src_onecell_get(
995 struct of_phandle_args *clkspec, void *data)
997 return ERR_PTR(-ENOENT);
999 static inline struct clk_hw *
1000 of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
1002 return ERR_PTR(-ENOENT);
1004 static inline int of_clk_parent_fill(struct device_node *np,
1005 const char **parents, unsigned int size)
1009 static inline int of_clk_detect_critical(struct device_node *np, int index,
1010 unsigned long *flags)
1014 #endif /* CONFIG_OF */
1017 * wrap access to peripherals in accessor routines
1018 * for improved portability across platforms
1021 #if IS_ENABLED(CONFIG_PPC)
1023 static inline u32 clk_readl(u32 __iomem *reg)
1025 return ioread32be(reg);
1028 static inline void clk_writel(u32 val, u32 __iomem *reg)
1030 iowrite32be(val, reg);
1033 #else /* platform dependent I/O accessors */
1035 static inline u32 clk_readl(u32 __iomem *reg)
1040 static inline void clk_writel(u32 val, u32 __iomem *reg)
1045 #endif /* platform dependent I/O accessors */
1047 void clk_gate_restore_context(struct clk_hw *hw);
1049 #endif /* CONFIG_COMMON_CLK */
1050 #endif /* CLK_PROVIDER_H */