clk-provider.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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/io.h>
  14. #include <linux/of.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. /* unused */
  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. #define CLK_GET_ACCURACY_NOCACHE BIT(8) /* do not use the cached clk accuracy */
  30. #define CLK_RECALC_NEW_RATES BIT(9) /* recalc rates after notifications */
  31. #define CLK_SET_RATE_UNGATE BIT(10) /* clock needs to run to set rate */
  32. #define CLK_IS_CRITICAL BIT(11) /* do not gate, ever */
  33. /* parents need enable during gate/ungate, set rate and re-parent */
  34. #define CLK_OPS_PARENT_ENABLE BIT(12)
  35. struct clk;
  36. struct clk_hw;
  37. struct clk_core;
  38. struct dentry;
  39. /**
  40. * struct clk_rate_request - Structure encoding the clk constraints that
  41. * a clock user might require.
  42. *
  43. * @rate: Requested clock rate. This field will be adjusted by
  44. * clock drivers according to hardware capabilities.
  45. * @min_rate: Minimum rate imposed by clk users.
  46. * @max_rate: Maximum rate imposed by clk users.
  47. * @best_parent_rate: The best parent rate a parent can provide to fulfill the
  48. * requested constraints.
  49. * @best_parent_hw: The most appropriate parent clock that fulfills the
  50. * requested constraints.
  51. *
  52. */
  53. struct clk_rate_request {
  54. unsigned long rate;
  55. unsigned long min_rate;
  56. unsigned long max_rate;
  57. unsigned long best_parent_rate;
  58. struct clk_hw *best_parent_hw;
  59. };
  60. /**
  61. * struct clk_ops - Callback operations for hardware clocks; these are to
  62. * be provided by the clock implementation, and will be called by drivers
  63. * through the clk_* api.
  64. *
  65. * @prepare: Prepare the clock for enabling. This must not return until
  66. * the clock is fully prepared, and it's safe to call clk_enable.
  67. * This callback is intended to allow clock implementations to
  68. * do any initialisation that may sleep. Called with
  69. * prepare_lock held.
  70. *
  71. * @unprepare: Release the clock from its prepared state. This will typically
  72. * undo any work done in the @prepare callback. Called with
  73. * prepare_lock held.
  74. *
  75. * @is_prepared: Queries the hardware to determine if the clock is prepared.
  76. * This function is allowed to sleep. Optional, if this op is not
  77. * set then the prepare count will be used.
  78. *
  79. * @unprepare_unused: Unprepare the clock atomically. Only called from
  80. * clk_disable_unused for prepare clocks with special needs.
  81. * Called with prepare mutex held. This function may sleep.
  82. *
  83. * @enable: Enable the clock atomically. This must not return until the
  84. * clock is generating a valid clock signal, usable by consumer
  85. * devices. Called with enable_lock held. This function must not
  86. * sleep.
  87. *
  88. * @disable: Disable the clock atomically. Called with enable_lock held.
  89. * This function must not sleep.
  90. *
  91. * @is_enabled: Queries the hardware to determine if the clock is enabled.
  92. * This function must not sleep. Optional, if this op is not
  93. * set then the enable count will be used.
  94. *
  95. * @disable_unused: Disable the clock atomically. Only called from
  96. * clk_disable_unused for gate clocks with special needs.
  97. * Called with enable_lock held. This function must not
  98. * sleep.
  99. *
  100. * @save_context: Save the context of the clock in prepration for poweroff.
  101. *
  102. * @restore_context: Restore the context of the clock after a restoration
  103. * of power.
  104. *
  105. * @recalc_rate Recalculate the rate of this clock, by querying hardware. The
  106. * parent rate is an input parameter. It is up to the caller to
  107. * ensure that the prepare_mutex is held across this call.
  108. * Returns the calculated rate. Optional, but recommended - if
  109. * this op is not set then clock rate will be initialized to 0.
  110. *
  111. * @round_rate: Given a target rate as input, returns the closest rate actually
  112. * supported by the clock. The parent rate is an input/output
  113. * parameter.
  114. *
  115. * @determine_rate: Given a target rate as input, returns the closest rate
  116. * actually supported by the clock, and optionally the parent clock
  117. * that should be used to provide the clock rate.
  118. *
  119. * @set_parent: Change the input source of this clock; for clocks with multiple
  120. * possible parents specify a new parent by passing in the index
  121. * as a u8 corresponding to the parent in either the .parent_names
  122. * or .parents arrays. This function in affect translates an
  123. * array index into the value programmed into the hardware.
  124. * Returns 0 on success, -EERROR otherwise.
  125. *
  126. * @get_parent: Queries the hardware to determine the parent of a clock. The
  127. * return value is a u8 which specifies the index corresponding to
  128. * the parent clock. This index can be applied to either the
  129. * .parent_names or .parents arrays. In short, this function
  130. * translates the parent value read from hardware into an array
  131. * index. Currently only called when the clock is initialized by
  132. * __clk_init. This callback is mandatory for clocks with
  133. * multiple parents. It is optional (and unnecessary) for clocks
  134. * with 0 or 1 parents.
  135. *
  136. * @set_rate: Change the rate of this clock. The requested rate is specified
  137. * by the second argument, which should typically be the return
  138. * of .round_rate call. The third argument gives the parent rate
  139. * which is likely helpful for most .set_rate implementation.
  140. * Returns 0 on success, -EERROR otherwise.
  141. *
  142. * @set_rate_and_parent: Change the rate and the parent of this clock. The
  143. * requested rate is specified by the second argument, which
  144. * should typically be the return of .round_rate call. The
  145. * third argument gives the parent rate which is likely helpful
  146. * for most .set_rate_and_parent implementation. The fourth
  147. * argument gives the parent index. This callback is optional (and
  148. * unnecessary) for clocks with 0 or 1 parents as well as
  149. * for clocks that can tolerate switching the rate and the parent
  150. * separately via calls to .set_parent and .set_rate.
  151. * Returns 0 on success, -EERROR otherwise.
  152. *
  153. * @recalc_accuracy: Recalculate the accuracy of this clock. The clock accuracy
  154. * is expressed in ppb (parts per billion). The parent accuracy is
  155. * an input parameter.
  156. * Returns the calculated accuracy. Optional - if this op is not
  157. * set then clock accuracy will be initialized to parent accuracy
  158. * or 0 (perfect clock) if clock has no parent.
  159. *
  160. * @get_phase: Queries the hardware to get the current phase of a clock.
  161. * Returned values are 0-359 degrees on success, negative
  162. * error codes on failure.
  163. *
  164. * @set_phase: Shift the phase this clock signal in degrees specified
  165. * by the second argument. Valid values for degrees are
  166. * 0-359. Return 0 on success, otherwise -EERROR.
  167. *
  168. * @init: Perform platform-specific initialization magic.
  169. * This is not not used by any of the basic clock types.
  170. * Please consider other ways of solving initialization problems
  171. * before using this callback, as its use is discouraged.
  172. *
  173. * @debug_init: Set up type-specific debugfs entries for this clock. This
  174. * is called once, after the debugfs directory entry for this
  175. * clock has been created. The dentry pointer representing that
  176. * directory is provided as an argument. Called with
  177. * prepare_lock held. Returns 0 on success, -EERROR otherwise.
  178. *
  179. *
  180. * The clk_enable/clk_disable and clk_prepare/clk_unprepare pairs allow
  181. * implementations to split any work between atomic (enable) and sleepable
  182. * (prepare) contexts. If enabling a clock requires code that might sleep,
  183. * this must be done in clk_prepare. Clock enable code that will never be
  184. * called in a sleepable context may be implemented in clk_enable.
  185. *
  186. * Typically, drivers will call clk_prepare when a clock may be needed later
  187. * (eg. when a device is opened), and clk_enable when the clock is actually
  188. * required (eg. from an interrupt). Note that clk_prepare MUST have been
  189. * called before clk_enable.
  190. */
  191. struct clk_ops {
  192. int (*prepare)(struct clk_hw *hw);
  193. void (*unprepare)(struct clk_hw *hw);
  194. int (*is_prepared)(struct clk_hw *hw);
  195. void (*unprepare_unused)(struct clk_hw *hw);
  196. int (*enable)(struct clk_hw *hw);
  197. void (*disable)(struct clk_hw *hw);
  198. int (*is_enabled)(struct clk_hw *hw);
  199. void (*disable_unused)(struct clk_hw *hw);
  200. int (*save_context)(struct clk_hw *hw);
  201. void (*restore_context)(struct clk_hw *hw);
  202. unsigned long (*recalc_rate)(struct clk_hw *hw,
  203. unsigned long parent_rate);
  204. long (*round_rate)(struct clk_hw *hw, unsigned long rate,
  205. unsigned long *parent_rate);
  206. int (*determine_rate)(struct clk_hw *hw,
  207. struct clk_rate_request *req);
  208. int (*set_parent)(struct clk_hw *hw, u8 index);
  209. u8 (*get_parent)(struct clk_hw *hw);
  210. int (*set_rate)(struct clk_hw *hw, unsigned long rate,
  211. unsigned long parent_rate);
  212. int (*set_rate_and_parent)(struct clk_hw *hw,
  213. unsigned long rate,
  214. unsigned long parent_rate, u8 index);
  215. unsigned long (*recalc_accuracy)(struct clk_hw *hw,
  216. unsigned long parent_accuracy);
  217. int (*get_phase)(struct clk_hw *hw);
  218. int (*set_phase)(struct clk_hw *hw, int degrees);
  219. void (*init)(struct clk_hw *hw);
  220. int (*debug_init)(struct clk_hw *hw, struct dentry *dentry);
  221. };
  222. /**
  223. * struct clk_init_data - holds init data that's common to all clocks and is
  224. * shared between the clock provider and the common clock framework.
  225. *
  226. * @name: clock name
  227. * @ops: operations this clock supports
  228. * @parent_names: array of string names for all possible parents
  229. * @num_parents: number of possible parents
  230. * @flags: framework-level hints and quirks
  231. */
  232. struct clk_init_data {
  233. const char *name;
  234. const struct clk_ops *ops;
  235. const char * const *parent_names;
  236. u8 num_parents;
  237. unsigned long flags;
  238. };
  239. /**
  240. * struct clk_hw - handle for traversing from a struct clk to its corresponding
  241. * hardware-specific structure. struct clk_hw should be declared within struct
  242. * clk_foo and then referenced by the struct clk instance that uses struct
  243. * clk_foo's clk_ops
  244. *
  245. * @core: pointer to the struct clk_core instance that points back to this
  246. * struct clk_hw instance
  247. *
  248. * @clk: pointer to the per-user struct clk instance that can be used to call
  249. * into the clk API
  250. *
  251. * @init: pointer to struct clk_init_data that contains the init data shared
  252. * with the common clock framework.
  253. */
  254. struct clk_hw {
  255. struct clk_core *core;
  256. struct clk *clk;
  257. const struct clk_init_data *init;
  258. };
  259. /*
  260. * DOC: Basic clock implementations common to many platforms
  261. *
  262. * Each basic clock hardware type is comprised of a structure describing the
  263. * clock hardware, implementations of the relevant callbacks in struct clk_ops,
  264. * unique flags for that hardware type, a registration function and an
  265. * alternative macro for static initialization
  266. */
  267. /**
  268. * struct clk_fixed_rate - fixed-rate clock
  269. * @hw: handle between common and hardware-specific interfaces
  270. * @fixed_rate: constant frequency of clock
  271. */
  272. struct clk_fixed_rate {
  273. struct clk_hw hw;
  274. unsigned long fixed_rate;
  275. unsigned long fixed_accuracy;
  276. u8 flags;
  277. };
  278. #define to_clk_fixed_rate(_hw) container_of(_hw, struct clk_fixed_rate, hw)
  279. extern const struct clk_ops clk_fixed_rate_ops;
  280. struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
  281. const char *parent_name, unsigned long flags,
  282. unsigned long fixed_rate);
  283. struct clk_hw *clk_hw_register_fixed_rate(struct device *dev, const char *name,
  284. const char *parent_name, unsigned long flags,
  285. unsigned long fixed_rate);
  286. struct clk *clk_register_fixed_rate_with_accuracy(struct device *dev,
  287. const char *name, const char *parent_name, unsigned long flags,
  288. unsigned long fixed_rate, unsigned long fixed_accuracy);
  289. void clk_unregister_fixed_rate(struct clk *clk);
  290. struct clk_hw *clk_hw_register_fixed_rate_with_accuracy(struct device *dev,
  291. const char *name, const char *parent_name, unsigned long flags,
  292. unsigned long fixed_rate, unsigned long fixed_accuracy);
  293. void clk_hw_unregister_fixed_rate(struct clk_hw *hw);
  294. void of_fixed_clk_setup(struct device_node *np);
  295. /**
  296. * struct clk_gate - gating clock
  297. *
  298. * @hw: handle between common and hardware-specific interfaces
  299. * @reg: register controlling gate
  300. * @bit_idx: single bit controlling gate
  301. * @flags: hardware-specific flags
  302. * @lock: register lock
  303. *
  304. * Clock which can gate its output. Implements .enable & .disable
  305. *
  306. * Flags:
  307. * CLK_GATE_SET_TO_DISABLE - by default this clock sets the bit at bit_idx to
  308. * enable the clock. Setting this flag does the opposite: setting the bit
  309. * disable the clock and clearing it enables the clock
  310. * CLK_GATE_HIWORD_MASK - The gate settings are only in lower 16-bit
  311. * of this register, and mask of gate bits are in higher 16-bit of this
  312. * register. While setting the gate bits, higher 16-bit should also be
  313. * updated to indicate changing gate bits.
  314. */
  315. struct clk_gate {
  316. struct clk_hw hw;
  317. void __iomem *reg;
  318. u8 bit_idx;
  319. u8 flags;
  320. spinlock_t *lock;
  321. };
  322. #define to_clk_gate(_hw) container_of(_hw, struct clk_gate, hw)
  323. #define CLK_GATE_SET_TO_DISABLE BIT(0)
  324. #define CLK_GATE_HIWORD_MASK BIT(1)
  325. extern const struct clk_ops clk_gate_ops;
  326. struct clk *clk_register_gate(struct device *dev, const char *name,
  327. const char *parent_name, unsigned long flags,
  328. void __iomem *reg, u8 bit_idx,
  329. u8 clk_gate_flags, spinlock_t *lock);
  330. struct clk_hw *clk_hw_register_gate(struct device *dev, const char *name,
  331. const char *parent_name, unsigned long flags,
  332. void __iomem *reg, u8 bit_idx,
  333. u8 clk_gate_flags, spinlock_t *lock);
  334. void clk_unregister_gate(struct clk *clk);
  335. void clk_hw_unregister_gate(struct clk_hw *hw);
  336. struct clk_div_table {
  337. unsigned int val;
  338. unsigned int div;
  339. };
  340. /**
  341. * struct clk_divider - adjustable divider clock
  342. *
  343. * @hw: handle between common and hardware-specific interfaces
  344. * @reg: register containing the divider
  345. * @shift: shift to the divider bit field
  346. * @width: width of the divider bit field
  347. * @table: array of value/divider pairs, last entry should have div = 0
  348. * @lock: register lock
  349. *
  350. * Clock with an adjustable divider affecting its output frequency. Implements
  351. * .recalc_rate, .set_rate and .round_rate
  352. *
  353. * Flags:
  354. * CLK_DIVIDER_ONE_BASED - by default the divisor is the value read from the
  355. * register plus one. If CLK_DIVIDER_ONE_BASED is set then the divider is
  356. * the raw value read from the register, with the value of zero considered
  357. * invalid, unless CLK_DIVIDER_ALLOW_ZERO is set.
  358. * CLK_DIVIDER_POWER_OF_TWO - clock divisor is 2 raised to the value read from
  359. * the hardware register
  360. * CLK_DIVIDER_ALLOW_ZERO - Allow zero divisors. For dividers which have
  361. * CLK_DIVIDER_ONE_BASED set, it is possible to end up with a zero divisor.
  362. * Some hardware implementations gracefully handle this case and allow a
  363. * zero divisor by not modifying their input clock
  364. * (divide by one / bypass).
  365. * CLK_DIVIDER_HIWORD_MASK - The divider settings are only in lower 16-bit
  366. * of this register, and mask of divider bits are in higher 16-bit of this
  367. * register. While setting the divider bits, higher 16-bit should also be
  368. * updated to indicate changing divider bits.
  369. * CLK_DIVIDER_ROUND_CLOSEST - Makes the best calculated divider to be rounded
  370. * to the closest integer instead of the up one.
  371. * CLK_DIVIDER_READ_ONLY - The divider settings are preconfigured and should
  372. * not be changed by the clock framework.
  373. * CLK_DIVIDER_MAX_AT_ZERO - For dividers which are like CLK_DIVIDER_ONE_BASED
  374. * except when the value read from the register is zero, the divisor is
  375. * 2^width of the field.
  376. */
  377. struct clk_divider {
  378. struct clk_hw hw;
  379. void __iomem *reg;
  380. u8 shift;
  381. u8 width;
  382. u8 flags;
  383. const struct clk_div_table *table;
  384. spinlock_t *lock;
  385. };
  386. #define to_clk_divider(_hw) container_of(_hw, struct clk_divider, hw)
  387. #define CLK_DIVIDER_ONE_BASED BIT(0)
  388. #define CLK_DIVIDER_POWER_OF_TWO BIT(1)
  389. #define CLK_DIVIDER_ALLOW_ZERO BIT(2)
  390. #define CLK_DIVIDER_HIWORD_MASK BIT(3)
  391. #define CLK_DIVIDER_ROUND_CLOSEST BIT(4)
  392. #define CLK_DIVIDER_READ_ONLY BIT(5)
  393. #define CLK_DIVIDER_MAX_AT_ZERO BIT(6)
  394. extern const struct clk_ops clk_divider_ops;
  395. extern const struct clk_ops clk_divider_ro_ops;
  396. unsigned long divider_recalc_rate(struct clk_hw *hw, unsigned long parent_rate,
  397. unsigned int val, const struct clk_div_table *table,
  398. unsigned long flags);
  399. long divider_round_rate(struct clk_hw *hw, unsigned long rate,
  400. unsigned long *prate, const struct clk_div_table *table,
  401. u8 width, unsigned long flags);
  402. int divider_get_val(unsigned long rate, unsigned long parent_rate,
  403. const struct clk_div_table *table, u8 width,
  404. unsigned long flags);
  405. struct clk *clk_register_divider(struct device *dev, const char *name,
  406. const char *parent_name, unsigned long flags,
  407. void __iomem *reg, u8 shift, u8 width,
  408. u8 clk_divider_flags, spinlock_t *lock);
  409. struct clk_hw *clk_hw_register_divider(struct device *dev, const char *name,
  410. const char *parent_name, unsigned long flags,
  411. void __iomem *reg, u8 shift, u8 width,
  412. u8 clk_divider_flags, spinlock_t *lock);
  413. struct clk *clk_register_divider_table(struct device *dev, const char *name,
  414. const char *parent_name, unsigned long flags,
  415. void __iomem *reg, u8 shift, u8 width,
  416. u8 clk_divider_flags, const struct clk_div_table *table,
  417. spinlock_t *lock);
  418. struct clk_hw *clk_hw_register_divider_table(struct device *dev,
  419. const char *name, const char *parent_name, unsigned long flags,
  420. void __iomem *reg, u8 shift, u8 width,
  421. u8 clk_divider_flags, const struct clk_div_table *table,
  422. spinlock_t *lock);
  423. void clk_unregister_divider(struct clk *clk);
  424. void clk_hw_unregister_divider(struct clk_hw *hw);
  425. /**
  426. * struct clk_mux - multiplexer clock
  427. *
  428. * @hw: handle between common and hardware-specific interfaces
  429. * @reg: register controlling multiplexer
  430. * @shift: shift to multiplexer bit field
  431. * @width: width of mutliplexer bit field
  432. * @flags: hardware-specific flags
  433. * @lock: register lock
  434. *
  435. * Clock with multiple selectable parents. Implements .get_parent, .set_parent
  436. * and .recalc_rate
  437. *
  438. * Flags:
  439. * CLK_MUX_INDEX_ONE - register index starts at 1, not 0
  440. * CLK_MUX_INDEX_BIT - register index is a single bit (power of two)
  441. * CLK_MUX_HIWORD_MASK - The mux settings are only in lower 16-bit of this
  442. * register, and mask of mux bits are in higher 16-bit of this register.
  443. * While setting the mux bits, higher 16-bit should also be updated to
  444. * indicate changing mux bits.
  445. * CLK_MUX_ROUND_CLOSEST - Use the parent rate that is closest to the desired
  446. * frequency.
  447. */
  448. struct clk_mux {
  449. struct clk_hw hw;
  450. void __iomem *reg;
  451. u32 *table;
  452. u32 mask;
  453. u8 shift;
  454. u8 flags;
  455. spinlock_t *lock;
  456. u8 saved_parent;
  457. };
  458. #define to_clk_mux(_hw) container_of(_hw, struct clk_mux, hw)
  459. #define CLK_MUX_INDEX_ONE BIT(0)
  460. #define CLK_MUX_INDEX_BIT BIT(1)
  461. #define CLK_MUX_HIWORD_MASK BIT(2)
  462. #define CLK_MUX_READ_ONLY BIT(3) /* mux can't be changed */
  463. #define CLK_MUX_ROUND_CLOSEST BIT(4)
  464. extern const struct clk_ops clk_mux_ops;
  465. extern const struct clk_ops clk_mux_ro_ops;
  466. struct clk *clk_register_mux(struct device *dev, const char *name,
  467. const char * const *parent_names, u8 num_parents,
  468. unsigned long flags,
  469. void __iomem *reg, u8 shift, u8 width,
  470. u8 clk_mux_flags, spinlock_t *lock);
  471. struct clk_hw *clk_hw_register_mux(struct device *dev, const char *name,
  472. const char * const *parent_names, u8 num_parents,
  473. unsigned long flags,
  474. void __iomem *reg, u8 shift, u8 width,
  475. u8 clk_mux_flags, spinlock_t *lock);
  476. struct clk *clk_register_mux_table(struct device *dev, const char *name,
  477. const char * const *parent_names, u8 num_parents,
  478. unsigned long flags,
  479. void __iomem *reg, u8 shift, u32 mask,
  480. u8 clk_mux_flags, u32 *table, spinlock_t *lock);
  481. struct clk_hw *clk_hw_register_mux_table(struct device *dev, const char *name,
  482. const char * const *parent_names, u8 num_parents,
  483. unsigned long flags,
  484. void __iomem *reg, u8 shift, u32 mask,
  485. u8 clk_mux_flags, u32 *table, spinlock_t *lock);
  486. void clk_unregister_mux(struct clk *clk);
  487. void clk_hw_unregister_mux(struct clk_hw *hw);
  488. void of_fixed_factor_clk_setup(struct device_node *node);
  489. /**
  490. * struct clk_fixed_factor - fixed multiplier and divider clock
  491. *
  492. * @hw: handle between common and hardware-specific interfaces
  493. * @mult: multiplier
  494. * @div: divider
  495. *
  496. * Clock with a fixed multiplier and divider. The output frequency is the
  497. * parent clock rate divided by div and multiplied by mult.
  498. * Implements .recalc_rate, .set_rate and .round_rate
  499. */
  500. struct clk_fixed_factor {
  501. struct clk_hw hw;
  502. unsigned int mult;
  503. unsigned int div;
  504. };
  505. #define to_clk_fixed_factor(_hw) container_of(_hw, struct clk_fixed_factor, hw)
  506. extern const struct clk_ops clk_fixed_factor_ops;
  507. struct clk *clk_register_fixed_factor(struct device *dev, const char *name,
  508. const char *parent_name, unsigned long flags,
  509. unsigned int mult, unsigned int div);
  510. void clk_unregister_fixed_factor(struct clk *clk);
  511. struct clk_hw *clk_hw_register_fixed_factor(struct device *dev,
  512. const char *name, const char *parent_name, unsigned long flags,
  513. unsigned int mult, unsigned int div);
  514. void clk_hw_unregister_fixed_factor(struct clk_hw *hw);
  515. /**
  516. * struct clk_fractional_divider - adjustable fractional divider clock
  517. *
  518. * @hw: handle between common and hardware-specific interfaces
  519. * @reg: register containing the divider
  520. * @mshift: shift to the numerator bit field
  521. * @mwidth: width of the numerator bit field
  522. * @nshift: shift to the denominator bit field
  523. * @nwidth: width of the denominator bit field
  524. * @lock: register lock
  525. *
  526. * Clock with adjustable fractional divider affecting its output frequency.
  527. */
  528. struct clk_fractional_divider {
  529. struct clk_hw hw;
  530. void __iomem *reg;
  531. u8 mshift;
  532. u8 mwidth;
  533. u32 mmask;
  534. u8 nshift;
  535. u8 nwidth;
  536. u32 nmask;
  537. u8 flags;
  538. spinlock_t *lock;
  539. };
  540. #define to_clk_fd(_hw) container_of(_hw, struct clk_fractional_divider, hw)
  541. extern const struct clk_ops clk_fractional_divider_ops;
  542. struct clk *clk_register_fractional_divider(struct device *dev,
  543. const char *name, const char *parent_name, unsigned long flags,
  544. void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth,
  545. u8 clk_divider_flags, spinlock_t *lock);
  546. struct clk_hw *clk_hw_register_fractional_divider(struct device *dev,
  547. const char *name, const char *parent_name, unsigned long flags,
  548. void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth,
  549. u8 clk_divider_flags, spinlock_t *lock);
  550. void clk_hw_unregister_fractional_divider(struct clk_hw *hw);
  551. /**
  552. * struct clk_multiplier - adjustable multiplier clock
  553. *
  554. * @hw: handle between common and hardware-specific interfaces
  555. * @reg: register containing the multiplier
  556. * @shift: shift to the multiplier bit field
  557. * @width: width of the multiplier bit field
  558. * @lock: register lock
  559. *
  560. * Clock with an adjustable multiplier affecting its output frequency.
  561. * Implements .recalc_rate, .set_rate and .round_rate
  562. *
  563. * Flags:
  564. * CLK_MULTIPLIER_ZERO_BYPASS - By default, the multiplier is the value read
  565. * from the register, with 0 being a valid value effectively
  566. * zeroing the output clock rate. If CLK_MULTIPLIER_ZERO_BYPASS is
  567. * set, then a null multiplier will be considered as a bypass,
  568. * leaving the parent rate unmodified.
  569. * CLK_MULTIPLIER_ROUND_CLOSEST - Makes the best calculated divider to be
  570. * rounded to the closest integer instead of the down one.
  571. */
  572. struct clk_multiplier {
  573. struct clk_hw hw;
  574. void __iomem *reg;
  575. u8 shift;
  576. u8 width;
  577. u8 flags;
  578. spinlock_t *lock;
  579. };
  580. #define to_clk_multiplier(_hw) container_of(_hw, struct clk_multiplier, hw)
  581. #define CLK_MULTIPLIER_ZERO_BYPASS BIT(0)
  582. #define CLK_MULTIPLIER_ROUND_CLOSEST BIT(1)
  583. extern const struct clk_ops clk_multiplier_ops;
  584. /***
  585. * struct clk_composite - aggregate clock of mux, divider and gate clocks
  586. *
  587. * @hw: handle between common and hardware-specific interfaces
  588. * @mux_hw: handle between composite and hardware-specific mux clock
  589. * @rate_hw: handle between composite and hardware-specific rate clock
  590. * @gate_hw: handle between composite and hardware-specific gate clock
  591. * @mux_ops: clock ops for mux
  592. * @rate_ops: clock ops for rate
  593. * @gate_ops: clock ops for gate
  594. */
  595. struct clk_composite {
  596. struct clk_hw hw;
  597. struct clk_ops ops;
  598. struct clk_hw *mux_hw;
  599. struct clk_hw *rate_hw;
  600. struct clk_hw *gate_hw;
  601. const struct clk_ops *mux_ops;
  602. const struct clk_ops *rate_ops;
  603. const struct clk_ops *gate_ops;
  604. };
  605. #define to_clk_composite(_hw) container_of(_hw, struct clk_composite, hw)
  606. struct clk *clk_register_composite(struct device *dev, const char *name,
  607. const char * const *parent_names, int num_parents,
  608. struct clk_hw *mux_hw, const struct clk_ops *mux_ops,
  609. struct clk_hw *rate_hw, const struct clk_ops *rate_ops,
  610. struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
  611. unsigned long flags);
  612. void clk_unregister_composite(struct clk *clk);
  613. struct clk_hw *clk_hw_register_composite(struct device *dev, const char *name,
  614. const char * const *parent_names, int num_parents,
  615. struct clk_hw *mux_hw, const struct clk_ops *mux_ops,
  616. struct clk_hw *rate_hw, const struct clk_ops *rate_ops,
  617. struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
  618. unsigned long flags);
  619. void clk_hw_unregister_composite(struct clk_hw *hw);
  620. /***
  621. * struct clk_gpio_gate - gpio gated clock
  622. *
  623. * @hw: handle between common and hardware-specific interfaces
  624. * @gpiod: gpio descriptor
  625. *
  626. * Clock with a gpio control for enabling and disabling the parent clock.
  627. * Implements .enable, .disable and .is_enabled
  628. */
  629. struct clk_gpio {
  630. struct clk_hw hw;
  631. struct gpio_desc *gpiod;
  632. };
  633. #define to_clk_gpio(_hw) container_of(_hw, struct clk_gpio, hw)
  634. extern const struct clk_ops clk_gpio_gate_ops;
  635. struct clk *clk_register_gpio_gate(struct device *dev, const char *name,
  636. const char *parent_name, unsigned gpio, bool active_low,
  637. unsigned long flags);
  638. struct clk_hw *clk_hw_register_gpio_gate(struct device *dev, const char *name,
  639. const char *parent_name, unsigned gpio, bool active_low,
  640. unsigned long flags);
  641. void clk_hw_unregister_gpio_gate(struct clk_hw *hw);
  642. /**
  643. * struct clk_gpio_mux - gpio controlled clock multiplexer
  644. *
  645. * @hw: see struct clk_gpio
  646. * @gpiod: gpio descriptor to select the parent of this clock multiplexer
  647. *
  648. * Clock with a gpio control for selecting the parent clock.
  649. * Implements .get_parent, .set_parent and .determine_rate
  650. */
  651. extern const struct clk_ops clk_gpio_mux_ops;
  652. struct clk *clk_register_gpio_mux(struct device *dev, const char *name,
  653. const char * const *parent_names, u8 num_parents, unsigned gpio,
  654. bool active_low, unsigned long flags);
  655. struct clk_hw *clk_hw_register_gpio_mux(struct device *dev, const char *name,
  656. const char * const *parent_names, u8 num_parents, unsigned gpio,
  657. bool active_low, unsigned long flags);
  658. void clk_hw_unregister_gpio_mux(struct clk_hw *hw);
  659. /**
  660. * clk_register - allocate a new clock, register it and return an opaque cookie
  661. * @dev: device that is registering this clock
  662. * @hw: link to hardware-specific clock data
  663. *
  664. * clk_register is the primary interface for populating the clock tree with new
  665. * clock nodes. It returns a pointer to the newly allocated struct clk which
  666. * cannot be dereferenced by driver code but may be used in conjuction with the
  667. * rest of the clock API. In the event of an error clk_register will return an
  668. * error code; drivers must test for an error code after calling clk_register.
  669. */
  670. struct clk *clk_register(struct device *dev, struct clk_hw *hw);
  671. struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw);
  672. int __must_check clk_hw_register(struct device *dev, struct clk_hw *hw);
  673. int __must_check devm_clk_hw_register(struct device *dev, struct clk_hw *hw);
  674. void clk_unregister(struct clk *clk);
  675. void devm_clk_unregister(struct device *dev, struct clk *clk);
  676. void clk_hw_unregister(struct clk_hw *hw);
  677. void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw);
  678. /* helper functions */
  679. const char *__clk_get_name(const struct clk *clk);
  680. const char *clk_hw_get_name(const struct clk_hw *hw);
  681. struct clk_hw *__clk_get_hw(struct clk *clk);
  682. unsigned int clk_hw_get_num_parents(const struct clk_hw *hw);
  683. struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw);
  684. struct clk_hw *clk_hw_get_parent_by_index(const struct clk_hw *hw,
  685. unsigned int index);
  686. unsigned int __clk_get_enable_count(struct clk *clk);
  687. unsigned long clk_hw_get_rate(const struct clk_hw *hw);
  688. unsigned long __clk_get_flags(struct clk *clk);
  689. unsigned long clk_hw_get_flags(const struct clk_hw *hw);
  690. bool clk_hw_is_prepared(const struct clk_hw *hw);
  691. bool clk_hw_is_enabled(const struct clk_hw *hw);
  692. bool __clk_is_enabled(struct clk *clk);
  693. struct clk *__clk_lookup(const char *name);
  694. int __clk_mux_determine_rate(struct clk_hw *hw,
  695. struct clk_rate_request *req);
  696. int __clk_determine_rate(struct clk_hw *core, struct clk_rate_request *req);
  697. int __clk_mux_determine_rate_closest(struct clk_hw *hw,
  698. struct clk_rate_request *req);
  699. void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent);
  700. void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
  701. unsigned long max_rate);
  702. static inline void __clk_hw_set_clk(struct clk_hw *dst, struct clk_hw *src)
  703. {
  704. dst->clk = src->clk;
  705. dst->core = src->core;
  706. }
  707. /*
  708. * FIXME clock api without lock protection
  709. */
  710. unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate);
  711. struct of_device_id;
  712. typedef void (*of_clk_init_cb_t)(struct device_node *);
  713. struct clk_onecell_data {
  714. struct clk **clks;
  715. unsigned int clk_num;
  716. };
  717. struct clk_hw_onecell_data {
  718. unsigned int num;
  719. struct clk_hw *hws[];
  720. };
  721. extern struct of_device_id __clk_of_table;
  722. #define CLK_OF_DECLARE(name, compat, fn) OF_DECLARE_1(clk, name, compat, fn)
  723. /*
  724. * Use this macro when you have a driver that requires two initialization
  725. * routines, one at of_clk_init(), and one at platform device probe
  726. */
  727. #define CLK_OF_DECLARE_DRIVER(name, compat, fn) \
  728. static void __init name##_of_clk_init_driver(struct device_node *np) \
  729. { \
  730. of_node_clear_flag(np, OF_POPULATED); \
  731. fn(np); \
  732. } \
  733. OF_DECLARE_1(clk, name, compat, name##_of_clk_init_driver)
  734. #ifdef CONFIG_OF
  735. int of_clk_add_provider(struct device_node *np,
  736. struct clk *(*clk_src_get)(struct of_phandle_args *args,
  737. void *data),
  738. void *data);
  739. int of_clk_add_hw_provider(struct device_node *np,
  740. struct clk_hw *(*get)(struct of_phandle_args *clkspec,
  741. void *data),
  742. void *data);
  743. void of_clk_del_provider(struct device_node *np);
  744. struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
  745. void *data);
  746. struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec,
  747. void *data);
  748. struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data);
  749. struct clk_hw *of_clk_hw_onecell_get(struct of_phandle_args *clkspec,
  750. void *data);
  751. unsigned int of_clk_get_parent_count(struct device_node *np);
  752. int of_clk_parent_fill(struct device_node *np, const char **parents,
  753. unsigned int size);
  754. const char *of_clk_get_parent_name(struct device_node *np, int index);
  755. int of_clk_detect_critical(struct device_node *np, int index,
  756. unsigned long *flags);
  757. void of_clk_init(const struct of_device_id *matches);
  758. #else /* !CONFIG_OF */
  759. static inline int of_clk_add_provider(struct device_node *np,
  760. struct clk *(*clk_src_get)(struct of_phandle_args *args,
  761. void *data),
  762. void *data)
  763. {
  764. return 0;
  765. }
  766. static inline int of_clk_add_hw_provider(struct device_node *np,
  767. struct clk_hw *(*get)(struct of_phandle_args *clkspec,
  768. void *data),
  769. void *data)
  770. {
  771. return 0;
  772. }
  773. static inline void of_clk_del_provider(struct device_node *np) {}
  774. static inline struct clk *of_clk_src_simple_get(
  775. struct of_phandle_args *clkspec, void *data)
  776. {
  777. return ERR_PTR(-ENOENT);
  778. }
  779. static inline struct clk_hw *
  780. of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
  781. {
  782. return ERR_PTR(-ENOENT);
  783. }
  784. static inline struct clk *of_clk_src_onecell_get(
  785. struct of_phandle_args *clkspec, void *data)
  786. {
  787. return ERR_PTR(-ENOENT);
  788. }
  789. static inline struct clk_hw *
  790. of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
  791. {
  792. return ERR_PTR(-ENOENT);
  793. }
  794. static inline unsigned int of_clk_get_parent_count(struct device_node *np)
  795. {
  796. return 0;
  797. }
  798. static inline int of_clk_parent_fill(struct device_node *np,
  799. const char **parents, unsigned int size)
  800. {
  801. return 0;
  802. }
  803. static inline const char *of_clk_get_parent_name(struct device_node *np,
  804. int index)
  805. {
  806. return NULL;
  807. }
  808. static inline int of_clk_detect_critical(struct device_node *np, int index,
  809. unsigned long *flags)
  810. {
  811. return 0;
  812. }
  813. static inline void of_clk_init(const struct of_device_id *matches) {}
  814. #endif /* CONFIG_OF */
  815. /*
  816. * wrap access to peripherals in accessor routines
  817. * for improved portability across platforms
  818. */
  819. #if IS_ENABLED(CONFIG_PPC)
  820. static inline u32 clk_readl(u32 __iomem *reg)
  821. {
  822. return ioread32be(reg);
  823. }
  824. static inline void clk_writel(u32 val, u32 __iomem *reg)
  825. {
  826. iowrite32be(val, reg);
  827. }
  828. #else /* platform dependent I/O accessors */
  829. static inline u32 clk_readl(u32 __iomem *reg)
  830. {
  831. return readl(reg);
  832. }
  833. static inline void clk_writel(u32 val, u32 __iomem *reg)
  834. {
  835. writel(val, reg);
  836. }
  837. #endif /* platform dependent I/O accessors */
  838. #ifdef CONFIG_DEBUG_FS
  839. struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
  840. void *data, const struct file_operations *fops);
  841. #endif
  842. void clk_dflt_restore_context(struct clk_hw *hw);
  843. #endif /* CONFIG_COMMON_CLK */
  844. #endif /* CLK_PROVIDER_H */