2 * linux/include/linux/clk-provider.h
4 * Copyright (c) 2010-2011 Jeremy Kerr <jeremy.kerr@canonical.com>
5 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 #ifndef __LINUX_CLK_PROVIDER_H
12 #define __LINUX_CLK_PROVIDER_H
14 #include <linux/clk.h>
17 #ifdef CONFIG_COMMON_CLK
20 * flags used across common struct clk. these flags should only affect the
21 * top-level framework. custom flags for dealing with hardware specifics
22 * belong in struct clk_foo
24 #define CLK_SET_RATE_GATE BIT(0) /* must be gated across rate change */
25 #define CLK_SET_PARENT_GATE BIT(1) /* must be gated across re-parent */
26 #define CLK_SET_RATE_PARENT BIT(2) /* propagate rate change up one level */
27 #define CLK_IGNORE_UNUSED BIT(3) /* do not gate even if unused */
28 #define CLK_IS_ROOT BIT(4) /* root clk, has no parent */
29 #define CLK_IS_BASIC BIT(5) /* Basic clk, can't do a to_clk_foo() */
30 #define CLK_GET_RATE_NOCACHE BIT(6) /* do not use the cached clk rate */
31 #define CLK_SET_RATE_NO_REPARENT BIT(7) /* don't re-parent on rate change */
32 #define CLK_GET_ACCURACY_NOCACHE BIT(8) /* do not use the cached clk accuracy */
38 * struct clk_ops - Callback operations for hardware clocks; these are to
39 * be provided by the clock implementation, and will be called by drivers
40 * through the clk_* api.
42 * @prepare: Prepare the clock for enabling. This must not return until
43 * the clock is fully prepared, and it's safe to call clk_enable.
44 * This callback is intended to allow clock implementations to
45 * do any initialisation that may sleep. Called with
48 * @unprepare: Release the clock from its prepared state. This will typically
49 * undo any work done in the @prepare callback. Called with
52 * @is_prepared: Queries the hardware to determine if the clock is prepared.
53 * This function is allowed to sleep. Optional, if this op is not
54 * set then the prepare count will be used.
56 * @unprepare_unused: Unprepare the clock atomically. Only called from
57 * clk_disable_unused for prepare clocks with special needs.
58 * Called with prepare mutex held. This function may sleep.
60 * @enable: Enable the clock atomically. This must not return until the
61 * clock is generating a valid clock signal, usable by consumer
62 * devices. Called with enable_lock held. This function must not
65 * @disable: Disable the clock atomically. Called with enable_lock held.
66 * This function must not sleep.
68 * @is_enabled: Queries the hardware to determine if the clock is enabled.
69 * This function must not sleep. Optional, if this op is not
70 * set then the enable count will be used.
72 * @disable_unused: Disable the clock atomically. Only called from
73 * clk_disable_unused for gate clocks with special needs.
74 * Called with enable_lock held. This function must not
77 * @recalc_rate Recalculate the rate of this clock, by querying hardware. The
78 * parent rate is an input parameter. It is up to the caller to
79 * ensure that the prepare_mutex is held across this call.
80 * Returns the calculated rate. Optional, but recommended - if
81 * this op is not set then clock rate will be initialized to 0.
83 * @round_rate: Given a target rate as input, returns the closest rate actually
84 * supported by the clock. The parent rate is an input/output
87 * @determine_rate: Given a target rate as input, returns the closest rate
88 * actually supported by the clock, and optionally the parent clock
89 * that should be used to provide the clock rate.
91 * @set_parent: Change the input source of this clock; for clocks with multiple
92 * possible parents specify a new parent by passing in the index
93 * as a u8 corresponding to the parent in either the .parent_names
94 * or .parents arrays. This function in affect translates an
95 * array index into the value programmed into the hardware.
96 * Returns 0 on success, -EERROR otherwise.
98 * @get_parent: Queries the hardware to determine the parent of a clock. The
99 * return value is a u8 which specifies the index corresponding to
100 * the parent clock. This index can be applied to either the
101 * .parent_names or .parents arrays. In short, this function
102 * translates the parent value read from hardware into an array
103 * index. Currently only called when the clock is initialized by
104 * __clk_init. This callback is mandatory for clocks with
105 * multiple parents. It is optional (and unnecessary) for clocks
106 * with 0 or 1 parents.
108 * @set_rate: Change the rate of this clock. The requested rate is specified
109 * by the second argument, which should typically be the return
110 * of .round_rate call. The third argument gives the parent rate
111 * which is likely helpful for most .set_rate implementation.
112 * Returns 0 on success, -EERROR otherwise.
114 * @set_rate_and_parent: Change the rate and the parent of this clock. The
115 * requested rate is specified by the second argument, which
116 * should typically be the return of .round_rate call. The
117 * third argument gives the parent rate which is likely helpful
118 * for most .set_rate_and_parent implementation. The fourth
119 * argument gives the parent index. This callback is optional (and
120 * unnecessary) for clocks with 0 or 1 parents as well as
121 * for clocks that can tolerate switching the rate and the parent
122 * separately via calls to .set_parent and .set_rate.
123 * Returns 0 on success, -EERROR otherwise.
125 * @recalc_accuracy: Recalculate the accuracy of this clock. The clock accuracy
126 * is expressed in ppb (parts per billion). The parent accuracy is
127 * an input parameter.
128 * Returns the calculated accuracy. Optional - if this op is not
129 * set then clock accuracy will be initialized to parent accuracy
130 * or 0 (perfect clock) if clock has no parent.
132 * @set_phase: Shift the phase this clock signal in degrees specified
133 * by the second argument. Valid values for degrees are
134 * 0-359. Return 0 on success, otherwise -EERROR.
136 * @init: Perform platform-specific initialization magic.
137 * This is not not used by any of the basic clock types.
138 * Please consider other ways of solving initialization problems
139 * before using this callback, as its use is discouraged.
141 * @debug_init: Set up type-specific debugfs entries for this clock. This
142 * is called once, after the debugfs directory entry for this
143 * clock has been created. The dentry pointer representing that
144 * directory is provided as an argument. Called with
145 * prepare_lock held. Returns 0 on success, -EERROR otherwise.
148 * The clk_enable/clk_disable and clk_prepare/clk_unprepare pairs allow
149 * implementations to split any work between atomic (enable) and sleepable
150 * (prepare) contexts. If enabling a clock requires code that might sleep,
151 * this must be done in clk_prepare. Clock enable code that will never be
152 * called in a sleepable context may be implemented in clk_enable.
154 * Typically, drivers will call clk_prepare when a clock may be needed later
155 * (eg. when a device is opened), and clk_enable when the clock is actually
156 * required (eg. from an interrupt). Note that clk_prepare MUST have been
157 * called before clk_enable.
160 int (*prepare)(struct clk_hw *hw);
161 void (*unprepare)(struct clk_hw *hw);
162 int (*is_prepared)(struct clk_hw *hw);
163 void (*unprepare_unused)(struct clk_hw *hw);
164 int (*enable)(struct clk_hw *hw);
165 void (*disable)(struct clk_hw *hw);
166 int (*is_enabled)(struct clk_hw *hw);
167 void (*disable_unused)(struct clk_hw *hw);
168 unsigned long (*recalc_rate)(struct clk_hw *hw,
169 unsigned long parent_rate);
170 long (*round_rate)(struct clk_hw *hw, unsigned long rate,
171 unsigned long *parent_rate);
172 long (*determine_rate)(struct clk_hw *hw, unsigned long rate,
173 unsigned long *best_parent_rate,
174 struct clk **best_parent_clk);
175 int (*set_parent)(struct clk_hw *hw, u8 index);
176 u8 (*get_parent)(struct clk_hw *hw);
177 int (*set_rate)(struct clk_hw *hw, unsigned long rate,
178 unsigned long parent_rate);
179 int (*set_rate_and_parent)(struct clk_hw *hw,
181 unsigned long parent_rate, u8 index);
182 unsigned long (*recalc_accuracy)(struct clk_hw *hw,
183 unsigned long parent_accuracy);
184 int (*set_phase)(struct clk_hw *hw, int degrees);
185 void (*init)(struct clk_hw *hw);
186 int (*debug_init)(struct clk_hw *hw, struct dentry *dentry);
190 * struct clk_init_data - holds init data that's common to all clocks and is
191 * shared between the clock provider and the common clock framework.
194 * @ops: operations this clock supports
195 * @parent_names: array of string names for all possible parents
196 * @num_parents: number of possible parents
197 * @flags: framework-level hints and quirks
199 struct clk_init_data {
201 const struct clk_ops *ops;
202 const char **parent_names;
208 * struct clk_hw - handle for traversing from a struct clk to its corresponding
209 * hardware-specific structure. struct clk_hw should be declared within struct
210 * clk_foo and then referenced by the struct clk instance that uses struct
213 * @clk: pointer to the struct clk instance that points back to this struct
216 * @init: pointer to struct clk_init_data that contains the init data shared
217 * with the common clock framework.
221 const struct clk_init_data *init;
225 * DOC: Basic clock implementations common to many platforms
227 * Each basic clock hardware type is comprised of a structure describing the
228 * clock hardware, implementations of the relevant callbacks in struct clk_ops,
229 * unique flags for that hardware type, a registration function and an
230 * alternative macro for static initialization
234 * struct clk_fixed_rate - fixed-rate clock
235 * @hw: handle between common and hardware-specific interfaces
236 * @fixed_rate: constant frequency of clock
238 struct clk_fixed_rate {
240 unsigned long fixed_rate;
241 unsigned long fixed_accuracy;
245 extern const struct clk_ops clk_fixed_rate_ops;
246 struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
247 const char *parent_name, unsigned long flags,
248 unsigned long fixed_rate);
249 struct clk *clk_register_fixed_rate_with_accuracy(struct device *dev,
250 const char *name, const char *parent_name, unsigned long flags,
251 unsigned long fixed_rate, unsigned long fixed_accuracy);
253 void of_fixed_clk_setup(struct device_node *np);
256 * struct clk_gate - gating clock
258 * @hw: handle between common and hardware-specific interfaces
259 * @reg: register controlling gate
260 * @bit_idx: single bit controlling gate
261 * @flags: hardware-specific flags
262 * @lock: register lock
264 * Clock which can gate its output. Implements .enable & .disable
267 * CLK_GATE_SET_TO_DISABLE - by default this clock sets the bit at bit_idx to
268 * enable the clock. Setting this flag does the opposite: setting the bit
269 * disable the clock and clearing it enables the clock
270 * CLK_GATE_HIWORD_MASK - The gate settings are only in lower 16-bit
271 * of this register, and mask of gate bits are in higher 16-bit of this
272 * register. While setting the gate bits, higher 16-bit should also be
273 * updated to indicate changing gate bits.
283 #define CLK_GATE_SET_TO_DISABLE BIT(0)
284 #define CLK_GATE_HIWORD_MASK BIT(1)
286 extern const struct clk_ops clk_gate_ops;
287 struct clk *clk_register_gate(struct device *dev, const char *name,
288 const char *parent_name, unsigned long flags,
289 void __iomem *reg, u8 bit_idx,
290 u8 clk_gate_flags, spinlock_t *lock);
292 struct clk_div_table {
298 * struct clk_divider - adjustable divider clock
300 * @hw: handle between common and hardware-specific interfaces
301 * @reg: register containing the divider
302 * @shift: shift to the divider bit field
303 * @width: width of the divider bit field
304 * @table: array of value/divider pairs, last entry should have div = 0
305 * @lock: register lock
307 * Clock with an adjustable divider affecting its output frequency. Implements
308 * .recalc_rate, .set_rate and .round_rate
311 * CLK_DIVIDER_ONE_BASED - by default the divisor is the value read from the
312 * register plus one. If CLK_DIVIDER_ONE_BASED is set then the divider is
313 * the raw value read from the register, with the value of zero considered
314 * invalid, unless CLK_DIVIDER_ALLOW_ZERO is set.
315 * CLK_DIVIDER_POWER_OF_TWO - clock divisor is 2 raised to the value read from
316 * the hardware register
317 * CLK_DIVIDER_ALLOW_ZERO - Allow zero divisors. For dividers which have
318 * CLK_DIVIDER_ONE_BASED set, it is possible to end up with a zero divisor.
319 * Some hardware implementations gracefully handle this case and allow a
320 * zero divisor by not modifying their input clock
321 * (divide by one / bypass).
322 * CLK_DIVIDER_HIWORD_MASK - The divider settings are only in lower 16-bit
323 * of this register, and mask of divider bits are in higher 16-bit of this
324 * register. While setting the divider bits, higher 16-bit should also be
325 * updated to indicate changing divider bits.
326 * CLK_DIVIDER_ROUND_CLOSEST - Makes the best calculated divider to be rounded
327 * to the closest integer instead of the up one.
328 * CLK_DIVIDER_READ_ONLY - The divider settings are preconfigured and should
329 * not be changed by the clock framework.
337 const struct clk_div_table *table;
341 #define CLK_DIVIDER_ONE_BASED BIT(0)
342 #define CLK_DIVIDER_POWER_OF_TWO BIT(1)
343 #define CLK_DIVIDER_ALLOW_ZERO BIT(2)
344 #define CLK_DIVIDER_HIWORD_MASK BIT(3)
345 #define CLK_DIVIDER_ROUND_CLOSEST BIT(4)
346 #define CLK_DIVIDER_READ_ONLY BIT(5)
348 extern const struct clk_ops clk_divider_ops;
349 extern const struct clk_ops clk_divider_ro_ops;
350 struct clk *clk_register_divider(struct device *dev, const char *name,
351 const char *parent_name, unsigned long flags,
352 void __iomem *reg, u8 shift, u8 width,
353 u8 clk_divider_flags, spinlock_t *lock);
354 struct clk *clk_register_divider_table(struct device *dev, const char *name,
355 const char *parent_name, unsigned long flags,
356 void __iomem *reg, u8 shift, u8 width,
357 u8 clk_divider_flags, const struct clk_div_table *table,
361 * struct clk_mux - multiplexer clock
363 * @hw: handle between common and hardware-specific interfaces
364 * @reg: register controlling multiplexer
365 * @shift: shift to multiplexer bit field
366 * @width: width of mutliplexer bit field
367 * @flags: hardware-specific flags
368 * @lock: register lock
370 * Clock with multiple selectable parents. Implements .get_parent, .set_parent
374 * CLK_MUX_INDEX_ONE - register index starts at 1, not 0
375 * CLK_MUX_INDEX_BIT - register index is a single bit (power of two)
376 * CLK_MUX_HIWORD_MASK - The mux settings are only in lower 16-bit of this
377 * register, and mask of mux bits are in higher 16-bit of this register.
378 * While setting the mux bits, higher 16-bit should also be updated to
379 * indicate changing mux bits.
391 #define CLK_MUX_INDEX_ONE BIT(0)
392 #define CLK_MUX_INDEX_BIT BIT(1)
393 #define CLK_MUX_HIWORD_MASK BIT(2)
394 #define CLK_MUX_READ_ONLY BIT(3) /* mux setting cannot be changed */
396 extern const struct clk_ops clk_mux_ops;
397 extern const struct clk_ops clk_mux_ro_ops;
399 struct clk *clk_register_mux(struct device *dev, const char *name,
400 const char **parent_names, u8 num_parents, unsigned long flags,
401 void __iomem *reg, u8 shift, u8 width,
402 u8 clk_mux_flags, spinlock_t *lock);
404 struct clk *clk_register_mux_table(struct device *dev, const char *name,
405 const char **parent_names, u8 num_parents, unsigned long flags,
406 void __iomem *reg, u8 shift, u32 mask,
407 u8 clk_mux_flags, u32 *table, spinlock_t *lock);
409 void of_fixed_factor_clk_setup(struct device_node *node);
412 * struct clk_fixed_factor - fixed multiplier and divider clock
414 * @hw: handle between common and hardware-specific interfaces
418 * Clock with a fixed multiplier and divider. The output frequency is the
419 * parent clock rate divided by div and multiplied by mult.
420 * Implements .recalc_rate, .set_rate and .round_rate
423 struct clk_fixed_factor {
429 extern struct clk_ops clk_fixed_factor_ops;
430 struct clk *clk_register_fixed_factor(struct device *dev, const char *name,
431 const char *parent_name, unsigned long flags,
432 unsigned int mult, unsigned int div);
435 * struct clk_fractional_divider - adjustable fractional divider clock
437 * @hw: handle between common and hardware-specific interfaces
438 * @reg: register containing the divider
439 * @mshift: shift to the numerator bit field
440 * @mwidth: width of the numerator bit field
441 * @nshift: shift to the denominator bit field
442 * @nwidth: width of the denominator bit field
443 * @lock: register lock
445 * Clock with adjustable fractional divider affecting its output frequency.
448 struct clk_fractional_divider {
459 extern const struct clk_ops clk_fractional_divider_ops;
460 struct clk *clk_register_fractional_divider(struct device *dev,
461 const char *name, const char *parent_name, unsigned long flags,
462 void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth,
463 u8 clk_divider_flags, spinlock_t *lock);
466 * struct clk_composite - aggregate clock of mux, divider and gate clocks
468 * @hw: handle between common and hardware-specific interfaces
469 * @mux_hw: handle between composite and hardware-specific mux clock
470 * @rate_hw: handle between composite and hardware-specific rate clock
471 * @gate_hw: handle between composite and hardware-specific gate clock
472 * @mux_ops: clock ops for mux
473 * @rate_ops: clock ops for rate
474 * @gate_ops: clock ops for gate
476 struct clk_composite {
480 struct clk_hw *mux_hw;
481 struct clk_hw *rate_hw;
482 struct clk_hw *gate_hw;
484 const struct clk_ops *mux_ops;
485 const struct clk_ops *rate_ops;
486 const struct clk_ops *gate_ops;
489 struct clk *clk_register_composite(struct device *dev, const char *name,
490 const char **parent_names, int num_parents,
491 struct clk_hw *mux_hw, const struct clk_ops *mux_ops,
492 struct clk_hw *rate_hw, const struct clk_ops *rate_ops,
493 struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
494 unsigned long flags);
497 * clk_register - allocate a new clock, register it and return an opaque cookie
498 * @dev: device that is registering this clock
499 * @hw: link to hardware-specific clock data
501 * clk_register is the primary interface for populating the clock tree with new
502 * clock nodes. It returns a pointer to the newly allocated struct clk which
503 * cannot be dereferenced by driver code but may be used in conjuction with the
504 * rest of the clock API. In the event of an error clk_register will return an
505 * error code; drivers must test for an error code after calling clk_register.
507 struct clk *clk_register(struct device *dev, struct clk_hw *hw);
508 struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw);
510 void clk_unregister(struct clk *clk);
511 void devm_clk_unregister(struct device *dev, struct clk *clk);
513 /* helper functions */
514 const char *__clk_get_name(struct clk *clk);
515 struct clk_hw *__clk_get_hw(struct clk *clk);
516 u8 __clk_get_num_parents(struct clk *clk);
517 struct clk *__clk_get_parent(struct clk *clk);
518 struct clk *clk_get_parent_by_index(struct clk *clk, u8 index);
519 unsigned int __clk_get_enable_count(struct clk *clk);
520 unsigned int __clk_get_prepare_count(struct clk *clk);
521 unsigned long __clk_get_rate(struct clk *clk);
522 unsigned long __clk_get_accuracy(struct clk *clk);
523 unsigned long __clk_get_flags(struct clk *clk);
524 bool __clk_is_prepared(struct clk *clk);
525 bool __clk_is_enabled(struct clk *clk);
526 struct clk *__clk_lookup(const char *name);
527 long __clk_mux_determine_rate(struct clk_hw *hw, unsigned long rate,
528 unsigned long *best_parent_rate,
529 struct clk **best_parent_p);
532 * FIXME clock api without lock protection
534 int __clk_prepare(struct clk *clk);
535 void __clk_unprepare(struct clk *clk);
536 void __clk_reparent(struct clk *clk, struct clk *new_parent);
537 unsigned long __clk_round_rate(struct clk *clk, unsigned long rate);
541 typedef void (*of_clk_init_cb_t)(struct device_node *);
543 struct clk_onecell_data {
545 unsigned int clk_num;
548 extern struct of_device_id __clk_of_table;
550 #define CLK_OF_DECLARE(name, compat, fn) OF_DECLARE_1(clk, name, compat, fn)
553 int of_clk_add_provider(struct device_node *np,
554 struct clk *(*clk_src_get)(struct of_phandle_args *args,
557 void of_clk_del_provider(struct device_node *np);
558 struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
560 struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data);
561 int of_clk_get_parent_count(struct device_node *np);
562 const char *of_clk_get_parent_name(struct device_node *np, int index);
564 void of_clk_init(const struct of_device_id *matches);
566 #else /* !CONFIG_OF */
568 static inline int of_clk_add_provider(struct device_node *np,
569 struct clk *(*clk_src_get)(struct of_phandle_args *args,
575 #define of_clk_del_provider(np) \
577 static inline struct clk *of_clk_src_simple_get(
578 struct of_phandle_args *clkspec, void *data)
580 return ERR_PTR(-ENOENT);
582 static inline struct clk *of_clk_src_onecell_get(
583 struct of_phandle_args *clkspec, void *data)
585 return ERR_PTR(-ENOENT);
587 static inline const char *of_clk_get_parent_name(struct device_node *np,
592 #define of_clk_init(matches) \
594 #endif /* CONFIG_OF */
597 * wrap access to peripherals in accessor routines
598 * for improved portability across platforms
601 #if IS_ENABLED(CONFIG_PPC)
603 static inline u32 clk_readl(u32 __iomem *reg)
605 return ioread32be(reg);
608 static inline void clk_writel(u32 val, u32 __iomem *reg)
610 iowrite32be(val, reg);
613 #else /* platform dependent I/O accessors */
615 static inline u32 clk_readl(u32 __iomem *reg)
620 static inline void clk_writel(u32 val, u32 __iomem *reg)
625 #endif /* platform dependent I/O accessors */
627 #ifdef CONFIG_DEBUG_FS
628 struct dentry *clk_debugfs_add_file(struct clk *clk, char *name, umode_t mode,
629 void *data, const struct file_operations *fops);
632 #endif /* CONFIG_COMMON_CLK */
633 #endif /* CLK_PROVIDER_H */