clk-provider.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. * DOC: Basic clock implementations common to many platforms
  128. *
  129. * Each basic clock hardware type is comprised of a structure describing the
  130. * clock hardware, implementations of the relevant callbacks in struct clk_ops,
  131. * unique flags for that hardware type, a registration function and an
  132. * alternative macro for static initialization
  133. */
  134. /**
  135. * struct clk_fixed_rate - fixed-rate clock
  136. * @hw: handle between common and hardware-specific interfaces
  137. * @fixed_rate: constant frequency of clock
  138. */
  139. struct clk_fixed_rate {
  140. struct clk_hw hw;
  141. unsigned long fixed_rate;
  142. u8 flags;
  143. };
  144. extern const struct clk_ops clk_fixed_rate_ops;
  145. struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
  146. const char *parent_name, unsigned long flags,
  147. unsigned long fixed_rate);
  148. /**
  149. * struct clk_gate - gating clock
  150. *
  151. * @hw: handle between common and hardware-specific interfaces
  152. * @reg: register controlling gate
  153. * @bit_idx: single bit controlling gate
  154. * @flags: hardware-specific flags
  155. * @lock: register lock
  156. *
  157. * Clock which can gate its output. Implements .enable & .disable
  158. *
  159. * Flags:
  160. * CLK_GATE_SET_DISABLE - by default this clock sets the bit at bit_idx to
  161. * enable the clock. Setting this flag does the opposite: setting the bit
  162. * disable the clock and clearing it enables the clock
  163. */
  164. struct clk_gate {
  165. struct clk_hw hw;
  166. void __iomem *reg;
  167. u8 bit_idx;
  168. u8 flags;
  169. spinlock_t *lock;
  170. char *parent[1];
  171. };
  172. #define CLK_GATE_SET_TO_DISABLE BIT(0)
  173. extern const struct clk_ops clk_gate_ops;
  174. struct clk *clk_register_gate(struct device *dev, const char *name,
  175. const char *parent_name, unsigned long flags,
  176. void __iomem *reg, u8 bit_idx,
  177. u8 clk_gate_flags, spinlock_t *lock);
  178. /**
  179. * struct clk_divider - adjustable divider clock
  180. *
  181. * @hw: handle between common and hardware-specific interfaces
  182. * @reg: register containing the divider
  183. * @shift: shift to the divider bit field
  184. * @width: width of the divider bit field
  185. * @lock: register lock
  186. *
  187. * Clock with an adjustable divider affecting its output frequency. Implements
  188. * .recalc_rate, .set_rate and .round_rate
  189. *
  190. * Flags:
  191. * CLK_DIVIDER_ONE_BASED - by default the divisor is the value read from the
  192. * register plus one. If CLK_DIVIDER_ONE_BASED is set then the divider is
  193. * the raw value read from the register, with the value of zero considered
  194. * invalid
  195. * CLK_DIVIDER_POWER_OF_TWO - clock divisor is 2 raised to the value read from
  196. * the hardware register
  197. */
  198. struct clk_divider {
  199. struct clk_hw hw;
  200. void __iomem *reg;
  201. u8 shift;
  202. u8 width;
  203. u8 flags;
  204. spinlock_t *lock;
  205. char *parent[1];
  206. };
  207. #define CLK_DIVIDER_ONE_BASED BIT(0)
  208. #define CLK_DIVIDER_POWER_OF_TWO BIT(1)
  209. extern const struct clk_ops clk_divider_ops;
  210. struct clk *clk_register_divider(struct device *dev, const char *name,
  211. const char *parent_name, unsigned long flags,
  212. void __iomem *reg, u8 shift, u8 width,
  213. u8 clk_divider_flags, spinlock_t *lock);
  214. /**
  215. * struct clk_mux - multiplexer clock
  216. *
  217. * @hw: handle between common and hardware-specific interfaces
  218. * @reg: register controlling multiplexer
  219. * @shift: shift to multiplexer bit field
  220. * @width: width of mutliplexer bit field
  221. * @num_clks: number of parent clocks
  222. * @lock: register lock
  223. *
  224. * Clock with multiple selectable parents. Implements .get_parent, .set_parent
  225. * and .recalc_rate
  226. *
  227. * Flags:
  228. * CLK_MUX_INDEX_ONE - register index starts at 1, not 0
  229. * CLK_MUX_INDEX_BITWISE - register index is a single bit (power of two)
  230. */
  231. struct clk_mux {
  232. struct clk_hw hw;
  233. void __iomem *reg;
  234. u8 shift;
  235. u8 width;
  236. u8 flags;
  237. spinlock_t *lock;
  238. };
  239. #define CLK_MUX_INDEX_ONE BIT(0)
  240. #define CLK_MUX_INDEX_BIT BIT(1)
  241. extern const struct clk_ops clk_mux_ops;
  242. struct clk *clk_register_mux(struct device *dev, const char *name,
  243. char **parent_names, u8 num_parents, unsigned long flags,
  244. void __iomem *reg, u8 shift, u8 width,
  245. u8 clk_mux_flags, spinlock_t *lock);
  246. /**
  247. * clk_register - allocate a new clock, register it and return an opaque cookie
  248. * @dev: device that is registering this clock
  249. * @name: clock name
  250. * @ops: operations this clock supports
  251. * @hw: link to hardware-specific clock data
  252. * @parent_names: array of string names for all possible parents
  253. * @num_parents: number of possible parents
  254. * @flags: framework-level hints and quirks
  255. *
  256. * clk_register is the primary interface for populating the clock tree with new
  257. * clock nodes. It returns a pointer to the newly allocated struct clk which
  258. * cannot be dereferenced by driver code but may be used in conjuction with the
  259. * rest of the clock API.
  260. */
  261. struct clk *clk_register(struct device *dev, const char *name,
  262. const struct clk_ops *ops, struct clk_hw *hw,
  263. char **parent_names, u8 num_parents, unsigned long flags);
  264. /* helper functions */
  265. const char *__clk_get_name(struct clk *clk);
  266. struct clk_hw *__clk_get_hw(struct clk *clk);
  267. u8 __clk_get_num_parents(struct clk *clk);
  268. struct clk *__clk_get_parent(struct clk *clk);
  269. inline int __clk_get_enable_count(struct clk *clk);
  270. inline int __clk_get_prepare_count(struct clk *clk);
  271. unsigned long __clk_get_rate(struct clk *clk);
  272. unsigned long __clk_get_flags(struct clk *clk);
  273. int __clk_is_enabled(struct clk *clk);
  274. struct clk *__clk_lookup(const char *name);
  275. /*
  276. * FIXME clock api without lock protection
  277. */
  278. int __clk_prepare(struct clk *clk);
  279. void __clk_unprepare(struct clk *clk);
  280. void __clk_reparent(struct clk *clk, struct clk *new_parent);
  281. unsigned long __clk_round_rate(struct clk *clk, unsigned long rate);
  282. #endif /* CONFIG_COMMON_CLK */
  283. #endif /* CLK_PROVIDER_H */