clk-provider.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. * @is_prepared: Queries the hardware to determine if the clock is prepared.
  44. * This function is allowed to sleep. Optional, if this op is not
  45. * set then the prepare count will be used.
  46. *
  47. * @unprepare_unused: Unprepare the clock atomically. Only called from
  48. * clk_disable_unused for prepare clocks with special needs.
  49. * Called with prepare mutex held. This function may sleep.
  50. *
  51. * @enable: Enable the clock atomically. This must not return until the
  52. * clock is generating a valid clock signal, usable by consumer
  53. * devices. Called with enable_lock held. This function must not
  54. * sleep.
  55. *
  56. * @disable: Disable the clock atomically. Called with enable_lock held.
  57. * This function must not sleep.
  58. *
  59. * @is_enabled: Queries the hardware to determine if the clock is enabled.
  60. * This function must not sleep. Optional, if this op is not
  61. * set then the enable count will be used.
  62. *
  63. * @disable_unused: Disable the clock atomically. Only called from
  64. * clk_disable_unused for gate clocks with special needs.
  65. * Called with enable_lock held. This function must not
  66. * sleep.
  67. *
  68. * @recalc_rate Recalculate the rate of this clock, by querying hardware. The
  69. * parent rate is an input parameter. It is up to the caller to
  70. * ensure that the prepare_mutex is held across this call.
  71. * Returns the calculated rate. Optional, but recommended - if
  72. * this op is not set then clock rate will be initialized to 0.
  73. *
  74. * @round_rate: Given a target rate as input, returns the closest rate actually
  75. * supported by the clock.
  76. *
  77. * @get_parent: Queries the hardware to determine the parent of a clock. The
  78. * return value is a u8 which specifies the index corresponding to
  79. * the parent clock. This index can be applied to either the
  80. * .parent_names or .parents arrays. In short, this function
  81. * translates the parent value read from hardware into an array
  82. * index. Currently only called when the clock is initialized by
  83. * __clk_init. This callback is mandatory for clocks with
  84. * multiple parents. It is optional (and unnecessary) for clocks
  85. * with 0 or 1 parents.
  86. *
  87. * @set_parent: Change the input source of this clock; for clocks with multiple
  88. * possible parents specify a new parent by passing in the index
  89. * as a u8 corresponding to the parent in either the .parent_names
  90. * or .parents arrays. This function in affect translates an
  91. * array index into the value programmed into the hardware.
  92. * Returns 0 on success, -EERROR otherwise.
  93. *
  94. * @set_rate: Change the rate of this clock. The requested rate is specified
  95. * by the second argument, which should typically be the return
  96. * of .round_rate call. The third argument gives the parent rate
  97. * which is likely helpful for most .set_rate implementation.
  98. * Returns 0 on success, -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 implemented 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 (*is_prepared)(struct clk_hw *hw);
  115. void (*unprepare_unused)(struct clk_hw *hw);
  116. int (*enable)(struct clk_hw *hw);
  117. void (*disable)(struct clk_hw *hw);
  118. int (*is_enabled)(struct clk_hw *hw);
  119. void (*disable_unused)(struct clk_hw *hw);
  120. unsigned long (*recalc_rate)(struct clk_hw *hw,
  121. unsigned long parent_rate);
  122. long (*round_rate)(struct clk_hw *hw, unsigned long,
  123. unsigned long *);
  124. int (*set_parent)(struct clk_hw *hw, u8 index);
  125. u8 (*get_parent)(struct clk_hw *hw);
  126. int (*set_rate)(struct clk_hw *hw, unsigned long,
  127. unsigned long);
  128. void (*init)(struct clk_hw *hw);
  129. };
  130. /**
  131. * struct clk_init_data - holds init data that's common to all clocks and is
  132. * shared between the clock provider and the common clock framework.
  133. *
  134. * @name: clock name
  135. * @ops: operations this clock supports
  136. * @parent_names: array of string names for all possible parents
  137. * @num_parents: number of possible parents
  138. * @flags: framework-level hints and quirks
  139. */
  140. struct clk_init_data {
  141. const char *name;
  142. const struct clk_ops *ops;
  143. const char **parent_names;
  144. u8 num_parents;
  145. unsigned long flags;
  146. };
  147. /**
  148. * struct clk_hw - handle for traversing from a struct clk to its corresponding
  149. * hardware-specific structure. struct clk_hw should be declared within struct
  150. * clk_foo and then referenced by the struct clk instance that uses struct
  151. * clk_foo's clk_ops
  152. *
  153. * @clk: pointer to the struct clk instance that points back to this struct
  154. * clk_hw instance
  155. *
  156. * @init: pointer to struct clk_init_data that contains the init data shared
  157. * with the common clock framework.
  158. */
  159. struct clk_hw {
  160. struct clk *clk;
  161. const struct clk_init_data *init;
  162. };
  163. /*
  164. * DOC: Basic clock implementations common to many platforms
  165. *
  166. * Each basic clock hardware type is comprised of a structure describing the
  167. * clock hardware, implementations of the relevant callbacks in struct clk_ops,
  168. * unique flags for that hardware type, a registration function and an
  169. * alternative macro for static initialization
  170. */
  171. /**
  172. * struct clk_fixed_rate - fixed-rate clock
  173. * @hw: handle between common and hardware-specific interfaces
  174. * @fixed_rate: constant frequency of clock
  175. */
  176. struct clk_fixed_rate {
  177. struct clk_hw hw;
  178. unsigned long fixed_rate;
  179. u8 flags;
  180. };
  181. extern const struct clk_ops clk_fixed_rate_ops;
  182. struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
  183. const char *parent_name, unsigned long flags,
  184. unsigned long fixed_rate);
  185. void of_fixed_clk_setup(struct device_node *np);
  186. /**
  187. * struct clk_gate - gating clock
  188. *
  189. * @hw: handle between common and hardware-specific interfaces
  190. * @reg: register controlling gate
  191. * @bit_idx: single bit controlling gate
  192. * @flags: hardware-specific flags
  193. * @lock: register lock
  194. *
  195. * Clock which can gate its output. Implements .enable & .disable
  196. *
  197. * Flags:
  198. * CLK_GATE_SET_TO_DISABLE - by default this clock sets the bit at bit_idx to
  199. * enable the clock. Setting this flag does the opposite: setting the bit
  200. * disable the clock and clearing it enables the clock
  201. */
  202. struct clk_gate {
  203. struct clk_hw hw;
  204. void __iomem *reg;
  205. u8 bit_idx;
  206. u8 flags;
  207. spinlock_t *lock;
  208. };
  209. #define CLK_GATE_SET_TO_DISABLE BIT(0)
  210. extern const struct clk_ops clk_gate_ops;
  211. struct clk *clk_register_gate(struct device *dev, const char *name,
  212. const char *parent_name, unsigned long flags,
  213. void __iomem *reg, u8 bit_idx,
  214. u8 clk_gate_flags, spinlock_t *lock);
  215. struct clk_div_table {
  216. unsigned int val;
  217. unsigned int div;
  218. };
  219. /**
  220. * struct clk_divider - adjustable divider clock
  221. *
  222. * @hw: handle between common and hardware-specific interfaces
  223. * @reg: register containing the divider
  224. * @shift: shift to the divider bit field
  225. * @width: width of the divider bit field
  226. * @table: array of value/divider pairs, last entry should have div = 0
  227. * @lock: register lock
  228. *
  229. * Clock with an adjustable divider affecting its output frequency. Implements
  230. * .recalc_rate, .set_rate and .round_rate
  231. *
  232. * Flags:
  233. * CLK_DIVIDER_ONE_BASED - by default the divisor is the value read from the
  234. * register plus one. If CLK_DIVIDER_ONE_BASED is set then the divider is
  235. * the raw value read from the register, with the value of zero considered
  236. * invalid, unless CLK_DIVIDER_ALLOW_ZERO is set.
  237. * CLK_DIVIDER_POWER_OF_TWO - clock divisor is 2 raised to the value read from
  238. * the hardware register
  239. * CLK_DIVIDER_ALLOW_ZERO - Allow zero divisors. For dividers which have
  240. * CLK_DIVIDER_ONE_BASED set, it is possible to end up with a zero divisor.
  241. * Some hardware implementations gracefully handle this case and allow a
  242. * zero divisor by not modifying their input clock
  243. * (divide by one / bypass).
  244. */
  245. struct clk_divider {
  246. struct clk_hw hw;
  247. void __iomem *reg;
  248. u8 shift;
  249. u8 width;
  250. u8 flags;
  251. const struct clk_div_table *table;
  252. spinlock_t *lock;
  253. };
  254. #define CLK_DIVIDER_ONE_BASED BIT(0)
  255. #define CLK_DIVIDER_POWER_OF_TWO BIT(1)
  256. #define CLK_DIVIDER_ALLOW_ZERO BIT(2)
  257. extern const struct clk_ops clk_divider_ops;
  258. struct clk *clk_register_divider(struct device *dev, const char *name,
  259. const char *parent_name, unsigned long flags,
  260. void __iomem *reg, u8 shift, u8 width,
  261. u8 clk_divider_flags, spinlock_t *lock);
  262. struct clk *clk_register_divider_table(struct device *dev, const char *name,
  263. const char *parent_name, unsigned long flags,
  264. void __iomem *reg, u8 shift, u8 width,
  265. u8 clk_divider_flags, const struct clk_div_table *table,
  266. spinlock_t *lock);
  267. /**
  268. * struct clk_mux - multiplexer clock
  269. *
  270. * @hw: handle between common and hardware-specific interfaces
  271. * @reg: register controlling multiplexer
  272. * @shift: shift to multiplexer bit field
  273. * @width: width of mutliplexer bit field
  274. * @flags: hardware-specific flags
  275. * @lock: register lock
  276. *
  277. * Clock with multiple selectable parents. Implements .get_parent, .set_parent
  278. * and .recalc_rate
  279. *
  280. * Flags:
  281. * CLK_MUX_INDEX_ONE - register index starts at 1, not 0
  282. * CLK_MUX_INDEX_BIT - register index is a single bit (power of two)
  283. */
  284. struct clk_mux {
  285. struct clk_hw hw;
  286. void __iomem *reg;
  287. u32 *table;
  288. u32 mask;
  289. u8 shift;
  290. u8 flags;
  291. spinlock_t *lock;
  292. };
  293. #define CLK_MUX_INDEX_ONE BIT(0)
  294. #define CLK_MUX_INDEX_BIT BIT(1)
  295. extern const struct clk_ops clk_mux_ops;
  296. struct clk *clk_register_mux(struct device *dev, const char *name,
  297. const char **parent_names, u8 num_parents, unsigned long flags,
  298. void __iomem *reg, u8 shift, u8 width,
  299. u8 clk_mux_flags, spinlock_t *lock);
  300. struct clk *clk_register_mux_table(struct device *dev, const char *name,
  301. const char **parent_names, u8 num_parents, unsigned long flags,
  302. void __iomem *reg, u8 shift, u32 mask,
  303. u8 clk_mux_flags, u32 *table, spinlock_t *lock);
  304. void of_fixed_factor_clk_setup(struct device_node *node);
  305. /**
  306. * struct clk_fixed_factor - fixed multiplier and divider clock
  307. *
  308. * @hw: handle between common and hardware-specific interfaces
  309. * @mult: multiplier
  310. * @div: divider
  311. *
  312. * Clock with a fixed multiplier and divider. The output frequency is the
  313. * parent clock rate divided by div and multiplied by mult.
  314. * Implements .recalc_rate, .set_rate and .round_rate
  315. */
  316. struct clk_fixed_factor {
  317. struct clk_hw hw;
  318. unsigned int mult;
  319. unsigned int div;
  320. };
  321. extern struct clk_ops clk_fixed_factor_ops;
  322. struct clk *clk_register_fixed_factor(struct device *dev, const char *name,
  323. const char *parent_name, unsigned long flags,
  324. unsigned int mult, unsigned int div);
  325. /***
  326. * struct clk_composite - aggregate clock of mux, divider and gate clocks
  327. *
  328. * @hw: handle between common and hardware-specific interfaces
  329. * @mux_hw: handle between composite and hardware-specific mux clock
  330. * @rate_hw: handle between composite and hardware-specific rate clock
  331. * @gate_hw: handle between composite and hardware-specific gate clock
  332. * @mux_ops: clock ops for mux
  333. * @rate_ops: clock ops for rate
  334. * @gate_ops: clock ops for gate
  335. */
  336. struct clk_composite {
  337. struct clk_hw hw;
  338. struct clk_ops ops;
  339. struct clk_hw *mux_hw;
  340. struct clk_hw *rate_hw;
  341. struct clk_hw *gate_hw;
  342. const struct clk_ops *mux_ops;
  343. const struct clk_ops *rate_ops;
  344. const struct clk_ops *gate_ops;
  345. };
  346. struct clk *clk_register_composite(struct device *dev, const char *name,
  347. const char **parent_names, int num_parents,
  348. struct clk_hw *mux_hw, const struct clk_ops *mux_ops,
  349. struct clk_hw *rate_hw, const struct clk_ops *rate_ops,
  350. struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
  351. unsigned long flags);
  352. /**
  353. * clk_register - allocate a new clock, register it and return an opaque cookie
  354. * @dev: device that is registering this clock
  355. * @hw: link to hardware-specific clock data
  356. *
  357. * clk_register is the primary interface for populating the clock tree with new
  358. * clock nodes. It returns a pointer to the newly allocated struct clk which
  359. * cannot be dereferenced by driver code but may be used in conjuction with the
  360. * rest of the clock API. In the event of an error clk_register will return an
  361. * error code; drivers must test for an error code after calling clk_register.
  362. */
  363. struct clk *clk_register(struct device *dev, struct clk_hw *hw);
  364. struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw);
  365. void clk_unregister(struct clk *clk);
  366. void devm_clk_unregister(struct device *dev, struct clk *clk);
  367. /* helper functions */
  368. const char *__clk_get_name(struct clk *clk);
  369. struct clk_hw *__clk_get_hw(struct clk *clk);
  370. u8 __clk_get_num_parents(struct clk *clk);
  371. struct clk *__clk_get_parent(struct clk *clk);
  372. unsigned int __clk_get_enable_count(struct clk *clk);
  373. unsigned int __clk_get_prepare_count(struct clk *clk);
  374. unsigned long __clk_get_rate(struct clk *clk);
  375. unsigned long __clk_get_flags(struct clk *clk);
  376. bool __clk_is_prepared(struct clk *clk);
  377. bool __clk_is_enabled(struct clk *clk);
  378. struct clk *__clk_lookup(const char *name);
  379. /*
  380. * FIXME clock api without lock protection
  381. */
  382. int __clk_prepare(struct clk *clk);
  383. void __clk_unprepare(struct clk *clk);
  384. void __clk_reparent(struct clk *clk, struct clk *new_parent);
  385. unsigned long __clk_round_rate(struct clk *clk, unsigned long rate);
  386. struct of_device_id;
  387. typedef void (*of_clk_init_cb_t)(struct device_node *);
  388. int of_clk_add_provider(struct device_node *np,
  389. struct clk *(*clk_src_get)(struct of_phandle_args *args,
  390. void *data),
  391. void *data);
  392. void of_clk_del_provider(struct device_node *np);
  393. struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
  394. void *data);
  395. struct clk_onecell_data {
  396. struct clk **clks;
  397. unsigned int clk_num;
  398. };
  399. struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data);
  400. const char *of_clk_get_parent_name(struct device_node *np, int index);
  401. void of_clk_init(const struct of_device_id *matches);
  402. #define CLK_OF_DECLARE(name, compat, fn) \
  403. static const struct of_device_id __clk_of_table_##name \
  404. __used __section(__clk_of_table) \
  405. = { .compatible = compat, .data = fn };
  406. #endif /* CONFIG_COMMON_CLK */
  407. #endif /* CLK_PROVIDER_H */