clk-provider.h 18 KB

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