clk-provider.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. * CLK_GATE_HIWORD_MASK - The gate settings are only in lower 16-bit
  202. * of this register, and mask of gate bits are in higher 16-bit of this
  203. * register. While setting the gate bits, higher 16-bit should also be
  204. * updated to indicate changing gate bits.
  205. */
  206. struct clk_gate {
  207. struct clk_hw hw;
  208. void __iomem *reg;
  209. u8 bit_idx;
  210. u8 flags;
  211. spinlock_t *lock;
  212. };
  213. #define CLK_GATE_SET_TO_DISABLE BIT(0)
  214. #define CLK_GATE_HIWORD_MASK BIT(1)
  215. extern const struct clk_ops clk_gate_ops;
  216. struct clk *clk_register_gate(struct device *dev, const char *name,
  217. const char *parent_name, unsigned long flags,
  218. void __iomem *reg, u8 bit_idx,
  219. u8 clk_gate_flags, spinlock_t *lock);
  220. struct clk_div_table {
  221. unsigned int val;
  222. unsigned int div;
  223. };
  224. /**
  225. * struct clk_divider - adjustable divider clock
  226. *
  227. * @hw: handle between common and hardware-specific interfaces
  228. * @reg: register containing the divider
  229. * @shift: shift to the divider bit field
  230. * @width: width of the divider bit field
  231. * @table: array of value/divider pairs, last entry should have div = 0
  232. * @lock: register lock
  233. *
  234. * Clock with an adjustable divider affecting its output frequency. Implements
  235. * .recalc_rate, .set_rate and .round_rate
  236. *
  237. * Flags:
  238. * CLK_DIVIDER_ONE_BASED - by default the divisor is the value read from the
  239. * register plus one. If CLK_DIVIDER_ONE_BASED is set then the divider is
  240. * the raw value read from the register, with the value of zero considered
  241. * invalid, unless CLK_DIVIDER_ALLOW_ZERO is set.
  242. * CLK_DIVIDER_POWER_OF_TWO - clock divisor is 2 raised to the value read from
  243. * the hardware register
  244. * CLK_DIVIDER_ALLOW_ZERO - Allow zero divisors. For dividers which have
  245. * CLK_DIVIDER_ONE_BASED set, it is possible to end up with a zero divisor.
  246. * Some hardware implementations gracefully handle this case and allow a
  247. * zero divisor by not modifying their input clock
  248. * (divide by one / bypass).
  249. * CLK_DIVIDER_HIWORD_MASK - The divider settings are only in lower 16-bit
  250. * of this register, and mask of divider bits are in higher 16-bit of this
  251. * register. While setting the divider bits, higher 16-bit should also be
  252. * updated to indicate changing divider bits.
  253. */
  254. struct clk_divider {
  255. struct clk_hw hw;
  256. void __iomem *reg;
  257. u8 shift;
  258. u8 width;
  259. u8 flags;
  260. const struct clk_div_table *table;
  261. spinlock_t *lock;
  262. };
  263. #define CLK_DIVIDER_ONE_BASED BIT(0)
  264. #define CLK_DIVIDER_POWER_OF_TWO BIT(1)
  265. #define CLK_DIVIDER_ALLOW_ZERO BIT(2)
  266. #define CLK_DIVIDER_HIWORD_MASK BIT(3)
  267. extern const struct clk_ops clk_divider_ops;
  268. struct clk *clk_register_divider(struct device *dev, const char *name,
  269. const char *parent_name, unsigned long flags,
  270. void __iomem *reg, u8 shift, u8 width,
  271. u8 clk_divider_flags, spinlock_t *lock);
  272. struct clk *clk_register_divider_table(struct device *dev, const char *name,
  273. const char *parent_name, unsigned long flags,
  274. void __iomem *reg, u8 shift, u8 width,
  275. u8 clk_divider_flags, const struct clk_div_table *table,
  276. spinlock_t *lock);
  277. /**
  278. * struct clk_mux - multiplexer clock
  279. *
  280. * @hw: handle between common and hardware-specific interfaces
  281. * @reg: register controlling multiplexer
  282. * @shift: shift to multiplexer bit field
  283. * @width: width of mutliplexer bit field
  284. * @flags: hardware-specific flags
  285. * @lock: register lock
  286. *
  287. * Clock with multiple selectable parents. Implements .get_parent, .set_parent
  288. * and .recalc_rate
  289. *
  290. * Flags:
  291. * CLK_MUX_INDEX_ONE - register index starts at 1, not 0
  292. * CLK_MUX_INDEX_BIT - register index is a single bit (power of two)
  293. * CLK_MUX_HIWORD_MASK - The mux settings are only in lower 16-bit of this
  294. * register, and mask of mux bits are in higher 16-bit of this register.
  295. * While setting the mux bits, higher 16-bit should also be updated to
  296. * indicate changing mux bits.
  297. */
  298. struct clk_mux {
  299. struct clk_hw hw;
  300. void __iomem *reg;
  301. u32 *table;
  302. u32 mask;
  303. u8 shift;
  304. u8 flags;
  305. spinlock_t *lock;
  306. };
  307. #define CLK_MUX_INDEX_ONE BIT(0)
  308. #define CLK_MUX_INDEX_BIT BIT(1)
  309. #define CLK_MUX_HIWORD_MASK BIT(2)
  310. #define CLK_MUX_READ_ONLY BIT(3) /* mux setting cannot be changed */
  311. extern const struct clk_ops clk_mux_ops;
  312. extern const struct clk_ops clk_mux_ro_ops;
  313. struct clk *clk_register_mux(struct device *dev, const char *name,
  314. const char **parent_names, u8 num_parents, unsigned long flags,
  315. void __iomem *reg, u8 shift, u8 width,
  316. u8 clk_mux_flags, spinlock_t *lock);
  317. struct clk *clk_register_mux_table(struct device *dev, const char *name,
  318. const char **parent_names, u8 num_parents, unsigned long flags,
  319. void __iomem *reg, u8 shift, u32 mask,
  320. u8 clk_mux_flags, u32 *table, spinlock_t *lock);
  321. void of_fixed_factor_clk_setup(struct device_node *node);
  322. /**
  323. * struct clk_fixed_factor - fixed multiplier and divider clock
  324. *
  325. * @hw: handle between common and hardware-specific interfaces
  326. * @mult: multiplier
  327. * @div: divider
  328. *
  329. * Clock with a fixed multiplier and divider. The output frequency is the
  330. * parent clock rate divided by div and multiplied by mult.
  331. * Implements .recalc_rate, .set_rate and .round_rate
  332. */
  333. struct clk_fixed_factor {
  334. struct clk_hw hw;
  335. unsigned int mult;
  336. unsigned int div;
  337. };
  338. extern struct clk_ops clk_fixed_factor_ops;
  339. struct clk *clk_register_fixed_factor(struct device *dev, const char *name,
  340. const char *parent_name, unsigned long flags,
  341. unsigned int mult, unsigned int div);
  342. /***
  343. * struct clk_composite - aggregate clock of mux, divider and gate clocks
  344. *
  345. * @hw: handle between common and hardware-specific interfaces
  346. * @mux_hw: handle between composite and hardware-specific mux clock
  347. * @rate_hw: handle between composite and hardware-specific rate clock
  348. * @gate_hw: handle between composite and hardware-specific gate clock
  349. * @mux_ops: clock ops for mux
  350. * @rate_ops: clock ops for rate
  351. * @gate_ops: clock ops for gate
  352. */
  353. struct clk_composite {
  354. struct clk_hw hw;
  355. struct clk_ops ops;
  356. struct clk_hw *mux_hw;
  357. struct clk_hw *rate_hw;
  358. struct clk_hw *gate_hw;
  359. const struct clk_ops *mux_ops;
  360. const struct clk_ops *rate_ops;
  361. const struct clk_ops *gate_ops;
  362. };
  363. struct clk *clk_register_composite(struct device *dev, const char *name,
  364. const char **parent_names, int num_parents,
  365. struct clk_hw *mux_hw, const struct clk_ops *mux_ops,
  366. struct clk_hw *rate_hw, const struct clk_ops *rate_ops,
  367. struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
  368. unsigned long flags);
  369. /**
  370. * clk_register - allocate a new clock, register it and return an opaque cookie
  371. * @dev: device that is registering this clock
  372. * @hw: link to hardware-specific clock data
  373. *
  374. * clk_register is the primary interface for populating the clock tree with new
  375. * clock nodes. It returns a pointer to the newly allocated struct clk which
  376. * cannot be dereferenced by driver code but may be used in conjuction with the
  377. * rest of the clock API. In the event of an error clk_register will return an
  378. * error code; drivers must test for an error code after calling clk_register.
  379. */
  380. struct clk *clk_register(struct device *dev, struct clk_hw *hw);
  381. struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw);
  382. void clk_unregister(struct clk *clk);
  383. void devm_clk_unregister(struct device *dev, struct clk *clk);
  384. /* helper functions */
  385. const char *__clk_get_name(struct clk *clk);
  386. struct clk_hw *__clk_get_hw(struct clk *clk);
  387. u8 __clk_get_num_parents(struct clk *clk);
  388. struct clk *__clk_get_parent(struct clk *clk);
  389. unsigned int __clk_get_enable_count(struct clk *clk);
  390. unsigned int __clk_get_prepare_count(struct clk *clk);
  391. unsigned long __clk_get_rate(struct clk *clk);
  392. unsigned long __clk_get_flags(struct clk *clk);
  393. bool __clk_is_prepared(struct clk *clk);
  394. bool __clk_is_enabled(struct clk *clk);
  395. struct clk *__clk_lookup(const char *name);
  396. /*
  397. * FIXME clock api without lock protection
  398. */
  399. int __clk_prepare(struct clk *clk);
  400. void __clk_unprepare(struct clk *clk);
  401. void __clk_reparent(struct clk *clk, struct clk *new_parent);
  402. unsigned long __clk_round_rate(struct clk *clk, unsigned long rate);
  403. struct of_device_id;
  404. typedef void (*of_clk_init_cb_t)(struct device_node *);
  405. struct clk_onecell_data {
  406. struct clk **clks;
  407. unsigned int clk_num;
  408. };
  409. #define CLK_OF_DECLARE(name, compat, fn) \
  410. static const struct of_device_id __clk_of_table_##name \
  411. __used __section(__clk_of_table) \
  412. = { .compatible = compat, .data = fn };
  413. #ifdef CONFIG_OF
  414. int of_clk_add_provider(struct device_node *np,
  415. struct clk *(*clk_src_get)(struct of_phandle_args *args,
  416. void *data),
  417. void *data);
  418. void of_clk_del_provider(struct device_node *np);
  419. struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
  420. void *data);
  421. struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data);
  422. const char *of_clk_get_parent_name(struct device_node *np, int index);
  423. void of_clk_init(const struct of_device_id *matches);
  424. #else /* !CONFIG_OF */
  425. static inline int of_clk_add_provider(struct device_node *np,
  426. struct clk *(*clk_src_get)(struct of_phandle_args *args,
  427. void *data),
  428. void *data)
  429. {
  430. return 0;
  431. }
  432. #define of_clk_del_provider(np) \
  433. { while (0); }
  434. static inline struct clk *of_clk_src_simple_get(
  435. struct of_phandle_args *clkspec, void *data)
  436. {
  437. return ERR_PTR(-ENOENT);
  438. }
  439. static inline struct clk *of_clk_src_onecell_get(
  440. struct of_phandle_args *clkspec, void *data)
  441. {
  442. return ERR_PTR(-ENOENT);
  443. }
  444. static inline const char *of_clk_get_parent_name(struct device_node *np,
  445. int index)
  446. {
  447. return NULL;
  448. }
  449. #define of_clk_init(matches) \
  450. { while (0); }
  451. #endif /* CONFIG_OF */
  452. #endif /* CONFIG_COMMON_CLK */
  453. #endif /* CLK_PROVIDER_H */