clk-provider.h 36 KB

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