clk-provider.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * linux/include/linux/clk-provider.h
  3. *
  4. * Copyright (c) 2010-2011 Jeremy Kerr <jeremy.kerr@canonical.com>
  5. * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
  6. *
  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.
  10. */
  11. #ifndef __LINUX_CLK_PROVIDER_H
  12. #define __LINUX_CLK_PROVIDER_H
  13. #include <linux/clk.h>
  14. #ifdef CONFIG_COMMON_CLK
  15. /**
  16. * struct clk_hw - handle for traversing from a struct clk to its corresponding
  17. * hardware-specific structure. struct clk_hw should be declared within struct
  18. * clk_foo and then referenced by the struct clk instance that uses struct
  19. * clk_foo's clk_ops
  20. *
  21. * clk: pointer to the struct clk instance that points back to this struct
  22. * clk_hw instance
  23. */
  24. struct clk_hw {
  25. struct clk *clk;
  26. };
  27. /*
  28. * flags used across common struct clk. these flags should only affect the
  29. * top-level framework. custom flags for dealing with hardware specifics
  30. * belong in struct clk_foo
  31. */
  32. #define CLK_SET_RATE_GATE BIT(0) /* must be gated across rate change */
  33. #define CLK_SET_PARENT_GATE BIT(1) /* must be gated across re-parent */
  34. #define CLK_SET_RATE_PARENT BIT(2) /* propagate rate change up one level */
  35. #define CLK_IGNORE_UNUSED BIT(3) /* do not gate even if unused */
  36. #define CLK_IS_ROOT BIT(4) /* root clk, has no parent */
  37. /**
  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.
  41. *
  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
  46. * prepare_lock held.
  47. *
  48. * @unprepare: Release the clock from its prepared state. This will typically
  49. * undo any work done in the @prepare callback. Called with
  50. * prepare_lock held.
  51. *
  52. * @enable: Enable the clock atomically. This must not return until the
  53. * clock is generating a valid clock signal, usable by consumer
  54. * devices. Called with enable_lock held. This function must not
  55. * sleep.
  56. *
  57. * @disable: Disable the clock atomically. Called with enable_lock held.
  58. * This function must not sleep.
  59. *
  60. * @recalc_rate Recalculate the rate of this clock, by quering hardware. The
  61. * parent rate is an input parameter. It is up to the caller to
  62. * insure that the prepare_mutex is held across this call.
  63. * Returns the calculated rate. Optional, but recommended - if
  64. * this op is not set then clock rate will be initialized to 0.
  65. *
  66. * @round_rate: Given a target rate as input, returns the closest rate actually
  67. * supported by the clock.
  68. *
  69. * @get_parent: Queries the hardware to determine the parent of a clock. The
  70. * return value is a u8 which specifies the index corresponding to
  71. * the parent clock. This index can be applied to either the
  72. * .parent_names or .parents arrays. In short, this function
  73. * translates the parent value read from hardware into an array
  74. * index. Currently only called when the clock is initialized by
  75. * __clk_init. This callback is mandatory for clocks with
  76. * multiple parents. It is optional (and unnecessary) for clocks
  77. * with 0 or 1 parents.
  78. *
  79. * @set_parent: Change the input source of this clock; for clocks with multiple
  80. * possible parents specify a new parent by passing in the index
  81. * as a u8 corresponding to the parent in either the .parent_names
  82. * or .parents arrays. This function in affect translates an
  83. * array index into the value programmed into the hardware.
  84. * Returns 0 on success, -EERROR otherwise.
  85. *
  86. * @set_rate: Change the rate of this clock. If this callback returns
  87. * CLK_SET_RATE_PARENT, the rate change will be propagated to the
  88. * parent clock (which may propagate again if the parent clock
  89. * also sets this flag). The requested rate of the parent is
  90. * passed back from the callback in the second 'unsigned long *'
  91. * argument. Note that it is up to the hardware clock's set_rate
  92. * implementation to insure that clocks do not run out of spec
  93. * when propgating the call to set_rate up to the parent. One way
  94. * to do this is to gate the clock (via clk_disable and/or
  95. * clk_unprepare) before calling clk_set_rate, then ungating it
  96. * afterward. If your clock also has the CLK_GATE_SET_RATE flag
  97. * set then this will insure safety. Returns 0 on success,
  98. * -EERROR otherwise.
  99. *
  100. * The clk_enable/clk_disable and clk_prepare/clk_unprepare pairs allow
  101. * implementations to split any work between atomic (enable) and sleepable
  102. * (prepare) contexts. If enabling a clock requires code that might sleep,
  103. * this must be done in clk_prepare. Clock enable code that will never be
  104. * called in a sleepable context may be implement in clk_enable.
  105. *
  106. * Typically, drivers will call clk_prepare when a clock may be needed later
  107. * (eg. when a device is opened), and clk_enable when the clock is actually
  108. * required (eg. from an interrupt). Note that clk_prepare MUST have been
  109. * called before clk_enable.
  110. */
  111. struct clk_ops {
  112. int (*prepare)(struct clk_hw *hw);
  113. void (*unprepare)(struct clk_hw *hw);
  114. int (*enable)(struct clk_hw *hw);
  115. void (*disable)(struct clk_hw *hw);
  116. int (*is_enabled)(struct clk_hw *hw);
  117. unsigned long (*recalc_rate)(struct clk_hw *hw,
  118. unsigned long parent_rate);
  119. long (*round_rate)(struct clk_hw *hw, unsigned long,
  120. unsigned long *);
  121. int (*set_parent)(struct clk_hw *hw, u8 index);
  122. u8 (*get_parent)(struct clk_hw *hw);
  123. int (*set_rate)(struct clk_hw *hw, unsigned long);
  124. void (*init)(struct clk_hw *hw);
  125. };
  126. /**
  127. * clk_register - allocate a new clock, register it and return an opaque cookie
  128. * @dev: device that is registering this clock
  129. * @name: clock name
  130. * @ops: operations this clock supports
  131. * @hw: link to hardware-specific clock data
  132. * @parent_names: array of string names for all possible parents
  133. * @num_parents: number of possible parents
  134. * @flags: framework-level hints and quirks
  135. *
  136. * clk_register is the primary interface for populating the clock tree with new
  137. * clock nodes. It returns a pointer to the newly allocated struct clk which
  138. * cannot be dereferenced by driver code but may be used in conjuction with the
  139. * rest of the clock API.
  140. */
  141. struct clk *clk_register(struct device *dev, const char *name,
  142. const struct clk_ops *ops, struct clk_hw *hw,
  143. char **parent_names, u8 num_parents, unsigned long flags);
  144. /* helper functions */
  145. const char *__clk_get_name(struct clk *clk);
  146. struct clk_hw *__clk_get_hw(struct clk *clk);
  147. u8 __clk_get_num_parents(struct clk *clk);
  148. struct clk *__clk_get_parent(struct clk *clk);
  149. inline int __clk_get_enable_count(struct clk *clk);
  150. inline int __clk_get_prepare_count(struct clk *clk);
  151. unsigned long __clk_get_rate(struct clk *clk);
  152. unsigned long __clk_get_flags(struct clk *clk);
  153. int __clk_is_enabled(struct clk *clk);
  154. struct clk *__clk_lookup(const char *name);
  155. /*
  156. * FIXME clock api without lock protection
  157. */
  158. int __clk_prepare(struct clk *clk);
  159. void __clk_unprepare(struct clk *clk);
  160. void __clk_reparent(struct clk *clk, struct clk *new_parent);
  161. unsigned long __clk_round_rate(struct clk *clk, unsigned long rate);
  162. #endif /* CONFIG_COMMON_CLK */
  163. #endif /* CLK_PROVIDER_H */