clk-provider.h 12 KB

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