clk-provider.h 13 KB

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