clk-provider.h 37 KB

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