clk-provider.h 33 KB

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