clk-provider.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. The requested rate is specified
  87. * by the second argument, which should typically be the return
  88. * of .round_rate call. The third argument gives the parent rate
  89. * which is likely helpful for most .set_rate implementation.
  90. * Returns 0 on success, -EERROR otherwise.
  91. *
  92. * The clk_enable/clk_disable and clk_prepare/clk_unprepare pairs allow
  93. * implementations to split any work between atomic (enable) and sleepable
  94. * (prepare) contexts. If enabling a clock requires code that might sleep,
  95. * this must be done in clk_prepare. Clock enable code that will never be
  96. * called in a sleepable context may be implement in clk_enable.
  97. *
  98. * Typically, drivers will call clk_prepare when a clock may be needed later
  99. * (eg. when a device is opened), and clk_enable when the clock is actually
  100. * required (eg. from an interrupt). Note that clk_prepare MUST have been
  101. * called before clk_enable.
  102. */
  103. struct clk_ops {
  104. int (*prepare)(struct clk_hw *hw);
  105. void (*unprepare)(struct clk_hw *hw);
  106. int (*enable)(struct clk_hw *hw);
  107. void (*disable)(struct clk_hw *hw);
  108. int (*is_enabled)(struct clk_hw *hw);
  109. unsigned long (*recalc_rate)(struct clk_hw *hw,
  110. unsigned long parent_rate);
  111. long (*round_rate)(struct clk_hw *hw, unsigned long,
  112. unsigned long *);
  113. int (*set_parent)(struct clk_hw *hw, u8 index);
  114. u8 (*get_parent)(struct clk_hw *hw);
  115. int (*set_rate)(struct clk_hw *hw, unsigned long,
  116. unsigned long);
  117. void (*init)(struct clk_hw *hw);
  118. };
  119. /*
  120. * DOC: Basic clock implementations common to many platforms
  121. *
  122. * Each basic clock hardware type is comprised of a structure describing the
  123. * clock hardware, implementations of the relevant callbacks in struct clk_ops,
  124. * unique flags for that hardware type, a registration function and an
  125. * alternative macro for static initialization
  126. */
  127. /**
  128. * struct clk_fixed_rate - fixed-rate clock
  129. * @hw: handle between common and hardware-specific interfaces
  130. * @fixed_rate: constant frequency of clock
  131. */
  132. struct clk_fixed_rate {
  133. struct clk_hw hw;
  134. unsigned long fixed_rate;
  135. u8 flags;
  136. };
  137. extern const struct clk_ops clk_fixed_rate_ops;
  138. struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
  139. const char *parent_name, unsigned long flags,
  140. unsigned long fixed_rate);
  141. /**
  142. * struct clk_gate - gating clock
  143. *
  144. * @hw: handle between common and hardware-specific interfaces
  145. * @reg: register controlling gate
  146. * @bit_idx: single bit controlling gate
  147. * @flags: hardware-specific flags
  148. * @lock: register lock
  149. *
  150. * Clock which can gate its output. Implements .enable & .disable
  151. *
  152. * Flags:
  153. * CLK_GATE_SET_DISABLE - by default this clock sets the bit at bit_idx to
  154. * enable the clock. Setting this flag does the opposite: setting the bit
  155. * disable the clock and clearing it enables the clock
  156. */
  157. struct clk_gate {
  158. struct clk_hw hw;
  159. void __iomem *reg;
  160. u8 bit_idx;
  161. u8 flags;
  162. spinlock_t *lock;
  163. };
  164. #define CLK_GATE_SET_TO_DISABLE BIT(0)
  165. extern const struct clk_ops clk_gate_ops;
  166. struct clk *clk_register_gate(struct device *dev, const char *name,
  167. const char *parent_name, unsigned long flags,
  168. void __iomem *reg, u8 bit_idx,
  169. u8 clk_gate_flags, spinlock_t *lock);
  170. /**
  171. * struct clk_divider - adjustable divider clock
  172. *
  173. * @hw: handle between common and hardware-specific interfaces
  174. * @reg: register containing the divider
  175. * @shift: shift to the divider bit field
  176. * @width: width of the divider bit field
  177. * @lock: register lock
  178. *
  179. * Clock with an adjustable divider affecting its output frequency. Implements
  180. * .recalc_rate, .set_rate and .round_rate
  181. *
  182. * Flags:
  183. * CLK_DIVIDER_ONE_BASED - by default the divisor is the value read from the
  184. * register plus one. If CLK_DIVIDER_ONE_BASED is set then the divider is
  185. * the raw value read from the register, with the value of zero considered
  186. * invalid
  187. * CLK_DIVIDER_POWER_OF_TWO - clock divisor is 2 raised to the value read from
  188. * the hardware register
  189. */
  190. struct clk_divider {
  191. struct clk_hw hw;
  192. void __iomem *reg;
  193. u8 shift;
  194. u8 width;
  195. u8 flags;
  196. spinlock_t *lock;
  197. };
  198. #define CLK_DIVIDER_ONE_BASED BIT(0)
  199. #define CLK_DIVIDER_POWER_OF_TWO BIT(1)
  200. extern const struct clk_ops clk_divider_ops;
  201. struct clk *clk_register_divider(struct device *dev, const char *name,
  202. const char *parent_name, unsigned long flags,
  203. void __iomem *reg, u8 shift, u8 width,
  204. u8 clk_divider_flags, spinlock_t *lock);
  205. /**
  206. * struct clk_mux - multiplexer clock
  207. *
  208. * @hw: handle between common and hardware-specific interfaces
  209. * @reg: register controlling multiplexer
  210. * @shift: shift to multiplexer bit field
  211. * @width: width of mutliplexer bit field
  212. * @num_clks: number of parent clocks
  213. * @lock: register lock
  214. *
  215. * Clock with multiple selectable parents. Implements .get_parent, .set_parent
  216. * and .recalc_rate
  217. *
  218. * Flags:
  219. * CLK_MUX_INDEX_ONE - register index starts at 1, not 0
  220. * CLK_MUX_INDEX_BITWISE - register index is a single bit (power of two)
  221. */
  222. struct clk_mux {
  223. struct clk_hw hw;
  224. void __iomem *reg;
  225. u8 shift;
  226. u8 width;
  227. u8 flags;
  228. spinlock_t *lock;
  229. };
  230. #define CLK_MUX_INDEX_ONE BIT(0)
  231. #define CLK_MUX_INDEX_BIT BIT(1)
  232. extern const struct clk_ops clk_mux_ops;
  233. struct clk *clk_register_mux(struct device *dev, const char *name,
  234. const char **parent_names, u8 num_parents, unsigned long flags,
  235. void __iomem *reg, u8 shift, u8 width,
  236. u8 clk_mux_flags, spinlock_t *lock);
  237. /**
  238. * clk_register - allocate a new clock, register it and return an opaque cookie
  239. * @dev: device that is registering this clock
  240. * @name: clock name
  241. * @ops: operations this clock supports
  242. * @hw: link to hardware-specific clock data
  243. * @parent_names: array of string names for all possible parents
  244. * @num_parents: number of possible parents
  245. * @flags: framework-level hints and quirks
  246. *
  247. * clk_register is the primary interface for populating the clock tree with new
  248. * clock nodes. It returns a pointer to the newly allocated struct clk which
  249. * cannot be dereferenced by driver code but may be used in conjuction with the
  250. * rest of the clock API. In the event of an error clk_register will return an
  251. * error code; drivers must test for an error code after calling clk_register.
  252. */
  253. struct clk *clk_register(struct device *dev, const char *name,
  254. const struct clk_ops *ops, struct clk_hw *hw,
  255. const char **parent_names, u8 num_parents, unsigned long flags);
  256. /* helper functions */
  257. const char *__clk_get_name(struct clk *clk);
  258. struct clk_hw *__clk_get_hw(struct clk *clk);
  259. u8 __clk_get_num_parents(struct clk *clk);
  260. struct clk *__clk_get_parent(struct clk *clk);
  261. inline int __clk_get_enable_count(struct clk *clk);
  262. inline int __clk_get_prepare_count(struct clk *clk);
  263. unsigned long __clk_get_rate(struct clk *clk);
  264. unsigned long __clk_get_flags(struct clk *clk);
  265. int __clk_is_enabled(struct clk *clk);
  266. struct clk *__clk_lookup(const char *name);
  267. /*
  268. * FIXME clock api without lock protection
  269. */
  270. int __clk_prepare(struct clk *clk);
  271. void __clk_unprepare(struct clk *clk);
  272. void __clk_reparent(struct clk *clk, struct clk *new_parent);
  273. unsigned long __clk_round_rate(struct clk *clk, unsigned long rate);
  274. #endif /* CONFIG_COMMON_CLK */
  275. #endif /* CLK_PROVIDER_H */