clk-provider.h 14 KB

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