clk-provider.h 11 KB

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