clk-provider.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. * flags used across common struct clk. these flags should only affect the
  17. * top-level framework. custom flags for dealing with hardware specifics
  18. * belong in struct clk_foo
  19. */
  20. #define CLK_SET_RATE_GATE BIT(0) /* must be gated across rate change */
  21. #define CLK_SET_PARENT_GATE BIT(1) /* must be gated across re-parent */
  22. #define CLK_SET_RATE_PARENT BIT(2) /* propagate rate change up one level */
  23. #define CLK_IGNORE_UNUSED BIT(3) /* do not gate even if unused */
  24. #define CLK_IS_ROOT BIT(4) /* root clk, has no parent */
  25. struct clk_hw;
  26. /**
  27. * struct clk_ops - Callback operations for hardware clocks; these are to
  28. * be provided by the clock implementation, and will be called by drivers
  29. * through the clk_* api.
  30. *
  31. * @prepare: Prepare the clock for enabling. This must not return until
  32. * the clock is fully prepared, and it's safe to call clk_enable.
  33. * This callback is intended to allow clock implementations to
  34. * do any initialisation that may sleep. Called with
  35. * prepare_lock held.
  36. *
  37. * @unprepare: Release the clock from its prepared state. This will typically
  38. * undo any work done in the @prepare callback. Called with
  39. * prepare_lock held.
  40. *
  41. * @enable: Enable the clock atomically. This must not return until the
  42. * clock is generating a valid clock signal, usable by consumer
  43. * devices. Called with enable_lock held. This function must not
  44. * sleep.
  45. *
  46. * @disable: Disable the clock atomically. Called with enable_lock held.
  47. * This function must not sleep.
  48. *
  49. * @recalc_rate Recalculate the rate of this clock, by quering hardware. The
  50. * parent rate is an input parameter. It is up to the caller to
  51. * insure that the prepare_mutex is held across this call.
  52. * Returns the calculated rate. Optional, but recommended - if
  53. * this op is not set then clock rate will be initialized to 0.
  54. *
  55. * @round_rate: Given a target rate as input, returns the closest rate actually
  56. * supported by the clock.
  57. *
  58. * @get_parent: Queries the hardware to determine the parent of a clock. The
  59. * return value is a u8 which specifies the index corresponding to
  60. * the parent clock. This index can be applied to either the
  61. * .parent_names or .parents arrays. In short, this function
  62. * translates the parent value read from hardware into an array
  63. * index. Currently only called when the clock is initialized by
  64. * __clk_init. This callback is mandatory for clocks with
  65. * multiple parents. It is optional (and unnecessary) for clocks
  66. * with 0 or 1 parents.
  67. *
  68. * @set_parent: Change the input source of this clock; for clocks with multiple
  69. * possible parents specify a new parent by passing in the index
  70. * as a u8 corresponding to the parent in either the .parent_names
  71. * or .parents arrays. This function in affect translates an
  72. * array index into the value programmed into the hardware.
  73. * Returns 0 on success, -EERROR otherwise.
  74. *
  75. * @set_rate: Change the rate of this clock. The requested rate is specified
  76. * by the second argument, which should typically be the return
  77. * of .round_rate call. The third argument gives the parent rate
  78. * which is likely helpful for most .set_rate implementation.
  79. * Returns 0 on success, -EERROR otherwise.
  80. *
  81. * The clk_enable/clk_disable and clk_prepare/clk_unprepare pairs allow
  82. * implementations to split any work between atomic (enable) and sleepable
  83. * (prepare) contexts. If enabling a clock requires code that might sleep,
  84. * this must be done in clk_prepare. Clock enable code that will never be
  85. * called in a sleepable context may be implement in clk_enable.
  86. *
  87. * Typically, drivers will call clk_prepare when a clock may be needed later
  88. * (eg. when a device is opened), and clk_enable when the clock is actually
  89. * required (eg. from an interrupt). Note that clk_prepare MUST have been
  90. * called before clk_enable.
  91. */
  92. struct clk_ops {
  93. int (*prepare)(struct clk_hw *hw);
  94. void (*unprepare)(struct clk_hw *hw);
  95. int (*enable)(struct clk_hw *hw);
  96. void (*disable)(struct clk_hw *hw);
  97. int (*is_enabled)(struct clk_hw *hw);
  98. unsigned long (*recalc_rate)(struct clk_hw *hw,
  99. unsigned long parent_rate);
  100. long (*round_rate)(struct clk_hw *hw, unsigned long,
  101. unsigned long *);
  102. int (*set_parent)(struct clk_hw *hw, u8 index);
  103. u8 (*get_parent)(struct clk_hw *hw);
  104. int (*set_rate)(struct clk_hw *hw, unsigned long,
  105. unsigned long);
  106. void (*init)(struct clk_hw *hw);
  107. };
  108. /**
  109. * struct clk_init_data - holds init data that's common to all clocks and is
  110. * shared between the clock provider and the common clock framework.
  111. *
  112. * @name: clock name
  113. * @ops: operations this clock supports
  114. * @parent_names: array of string names for all possible parents
  115. * @num_parents: number of possible parents
  116. * @flags: framework-level hints and quirks
  117. */
  118. struct clk_init_data {
  119. const char *name;
  120. const struct clk_ops *ops;
  121. const char **parent_names;
  122. u8 num_parents;
  123. unsigned long flags;
  124. };
  125. /**
  126. * struct clk_hw - handle for traversing from a struct clk to its corresponding
  127. * hardware-specific structure. struct clk_hw should be declared within struct
  128. * clk_foo and then referenced by the struct clk instance that uses struct
  129. * clk_foo's clk_ops
  130. *
  131. * @clk: pointer to the struct clk instance that points back to this struct
  132. * clk_hw instance
  133. *
  134. * @init: pointer to struct clk_init_data that contains the init data shared
  135. * with the common clock framework.
  136. */
  137. struct clk_hw {
  138. struct clk *clk;
  139. struct clk_init_data *init;
  140. };
  141. /*
  142. * DOC: Basic clock implementations common to many platforms
  143. *
  144. * Each basic clock hardware type is comprised of a structure describing the
  145. * clock hardware, implementations of the relevant callbacks in struct clk_ops,
  146. * unique flags for that hardware type, a registration function and an
  147. * alternative macro for static initialization
  148. */
  149. /**
  150. * struct clk_fixed_rate - fixed-rate clock
  151. * @hw: handle between common and hardware-specific interfaces
  152. * @fixed_rate: constant frequency of clock
  153. */
  154. struct clk_fixed_rate {
  155. struct clk_hw hw;
  156. unsigned long fixed_rate;
  157. u8 flags;
  158. };
  159. extern const struct clk_ops clk_fixed_rate_ops;
  160. struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
  161. const char *parent_name, unsigned long flags,
  162. unsigned long fixed_rate);
  163. /**
  164. * struct clk_gate - gating clock
  165. *
  166. * @hw: handle between common and hardware-specific interfaces
  167. * @reg: register controlling gate
  168. * @bit_idx: single bit controlling gate
  169. * @flags: hardware-specific flags
  170. * @lock: register lock
  171. *
  172. * Clock which can gate its output. Implements .enable & .disable
  173. *
  174. * Flags:
  175. * CLK_GATE_SET_TO_DISABLE - by default this clock sets the bit at bit_idx to
  176. * enable the clock. Setting this flag does the opposite: setting the bit
  177. * disable the clock and clearing it enables the clock
  178. */
  179. struct clk_gate {
  180. struct clk_hw hw;
  181. void __iomem *reg;
  182. u8 bit_idx;
  183. u8 flags;
  184. spinlock_t *lock;
  185. };
  186. #define CLK_GATE_SET_TO_DISABLE BIT(0)
  187. extern const struct clk_ops clk_gate_ops;
  188. struct clk *clk_register_gate(struct device *dev, const char *name,
  189. const char *parent_name, unsigned long flags,
  190. void __iomem *reg, u8 bit_idx,
  191. u8 clk_gate_flags, spinlock_t *lock);
  192. struct clk_div_table {
  193. unsigned int val;
  194. unsigned int div;
  195. };
  196. /**
  197. * struct clk_divider - adjustable divider clock
  198. *
  199. * @hw: handle between common and hardware-specific interfaces
  200. * @reg: register containing the divider
  201. * @shift: shift to the divider bit field
  202. * @width: width of the divider bit field
  203. * @table: array of value/divider pairs, last entry should have div = 0
  204. * @lock: register lock
  205. *
  206. * Clock with an adjustable divider affecting its output frequency. Implements
  207. * .recalc_rate, .set_rate and .round_rate
  208. *
  209. * Flags:
  210. * CLK_DIVIDER_ONE_BASED - by default the divisor is the value read from the
  211. * register plus one. If CLK_DIVIDER_ONE_BASED is set then the divider is
  212. * the raw value read from the register, with the value of zero considered
  213. * invalid
  214. * CLK_DIVIDER_POWER_OF_TWO - clock divisor is 2 raised to the value read from
  215. * the hardware register
  216. */
  217. struct clk_divider {
  218. struct clk_hw hw;
  219. void __iomem *reg;
  220. u8 shift;
  221. u8 width;
  222. u8 flags;
  223. const struct clk_div_table *table;
  224. spinlock_t *lock;
  225. };
  226. #define CLK_DIVIDER_ONE_BASED BIT(0)
  227. #define CLK_DIVIDER_POWER_OF_TWO BIT(1)
  228. extern const struct clk_ops clk_divider_ops;
  229. struct clk *clk_register_divider(struct device *dev, const char *name,
  230. const char *parent_name, unsigned long flags,
  231. void __iomem *reg, u8 shift, u8 width,
  232. u8 clk_divider_flags, spinlock_t *lock);
  233. struct clk *clk_register_divider_table(struct device *dev, const char *name,
  234. const char *parent_name, unsigned long flags,
  235. void __iomem *reg, u8 shift, u8 width,
  236. u8 clk_divider_flags, const struct clk_div_table *table,
  237. spinlock_t *lock);
  238. /**
  239. * struct clk_mux - multiplexer clock
  240. *
  241. * @hw: handle between common and hardware-specific interfaces
  242. * @reg: register controlling multiplexer
  243. * @shift: shift to multiplexer bit field
  244. * @width: width of mutliplexer bit field
  245. * @num_clks: number of parent clocks
  246. * @lock: register lock
  247. *
  248. * Clock with multiple selectable parents. Implements .get_parent, .set_parent
  249. * and .recalc_rate
  250. *
  251. * Flags:
  252. * CLK_MUX_INDEX_ONE - register index starts at 1, not 0
  253. * CLK_MUX_INDEX_BIT - register index is a single bit (power of two)
  254. */
  255. struct clk_mux {
  256. struct clk_hw hw;
  257. void __iomem *reg;
  258. u8 shift;
  259. u8 width;
  260. u8 flags;
  261. spinlock_t *lock;
  262. };
  263. #define CLK_MUX_INDEX_ONE BIT(0)
  264. #define CLK_MUX_INDEX_BIT BIT(1)
  265. extern const struct clk_ops clk_mux_ops;
  266. struct clk *clk_register_mux(struct device *dev, const char *name,
  267. const char **parent_names, u8 num_parents, unsigned long flags,
  268. void __iomem *reg, u8 shift, u8 width,
  269. u8 clk_mux_flags, spinlock_t *lock);
  270. /**
  271. * struct clk_fixed_factor - fixed multiplier and divider clock
  272. *
  273. * @hw: handle between common and hardware-specific interfaces
  274. * @mult: multiplier
  275. * @div: divider
  276. *
  277. * Clock with a fixed multiplier and divider. The output frequency is the
  278. * parent clock rate divided by div and multiplied by mult.
  279. * Implements .recalc_rate, .set_rate and .round_rate
  280. */
  281. struct clk_fixed_factor {
  282. struct clk_hw hw;
  283. unsigned int mult;
  284. unsigned int div;
  285. };
  286. extern struct clk_ops clk_fixed_factor_ops;
  287. struct clk *clk_register_fixed_factor(struct device *dev, const char *name,
  288. const char *parent_name, unsigned long flags,
  289. unsigned int mult, unsigned int div);
  290. /**
  291. * clk_register - allocate a new clock, register it and return an opaque cookie
  292. * @dev: device that is registering this clock
  293. * @hw: link to hardware-specific clock data
  294. *
  295. * clk_register is the primary interface for populating the clock tree with new
  296. * clock nodes. It returns a pointer to the newly allocated struct clk which
  297. * cannot be dereferenced by driver code but may be used in conjuction with the
  298. * rest of the clock API. In the event of an error clk_register will return an
  299. * error code; drivers must test for an error code after calling clk_register.
  300. */
  301. struct clk *clk_register(struct device *dev, struct clk_hw *hw);
  302. void clk_unregister(struct clk *clk);
  303. /* helper functions */
  304. const char *__clk_get_name(struct clk *clk);
  305. struct clk_hw *__clk_get_hw(struct clk *clk);
  306. u8 __clk_get_num_parents(struct clk *clk);
  307. struct clk *__clk_get_parent(struct clk *clk);
  308. inline int __clk_get_enable_count(struct clk *clk);
  309. inline int __clk_get_prepare_count(struct clk *clk);
  310. unsigned long __clk_get_rate(struct clk *clk);
  311. unsigned long __clk_get_flags(struct clk *clk);
  312. int __clk_is_enabled(struct clk *clk);
  313. struct clk *__clk_lookup(const char *name);
  314. /*
  315. * FIXME clock api without lock protection
  316. */
  317. int __clk_prepare(struct clk *clk);
  318. void __clk_unprepare(struct clk *clk);
  319. void __clk_reparent(struct clk *clk, struct clk *new_parent);
  320. unsigned long __clk_round_rate(struct clk *clk, unsigned long rate);
  321. #endif /* CONFIG_COMMON_CLK */
  322. #endif /* CLK_PROVIDER_H */