clk-provider.h 18 KB

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