clk.h 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * linux/include/linux/clk.h
  4. *
  5. * Copyright (C) 2004 ARM Limited.
  6. * Written by Deep Blue Solutions Limited.
  7. * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
  8. */
  9. #ifndef __LINUX_CLK_H
  10. #define __LINUX_CLK_H
  11. #include <linux/err.h>
  12. #include <linux/kernel.h>
  13. #include <linux/notifier.h>
  14. struct device;
  15. struct clk;
  16. struct device_node;
  17. struct of_phandle_args;
  18. /**
  19. * DOC: clk notifier callback types
  20. *
  21. * PRE_RATE_CHANGE - called immediately before the clk rate is changed,
  22. * to indicate that the rate change will proceed. Drivers must
  23. * immediately terminate any operations that will be affected by the
  24. * rate change. Callbacks may either return NOTIFY_DONE, NOTIFY_OK,
  25. * NOTIFY_STOP or NOTIFY_BAD.
  26. *
  27. * ABORT_RATE_CHANGE: called if the rate change failed for some reason
  28. * after PRE_RATE_CHANGE. In this case, all registered notifiers on
  29. * the clk will be called with ABORT_RATE_CHANGE. Callbacks must
  30. * always return NOTIFY_DONE or NOTIFY_OK.
  31. *
  32. * POST_RATE_CHANGE - called after the clk rate change has successfully
  33. * completed. Callbacks must always return NOTIFY_DONE or NOTIFY_OK.
  34. *
  35. */
  36. #define PRE_RATE_CHANGE BIT(0)
  37. #define POST_RATE_CHANGE BIT(1)
  38. #define ABORT_RATE_CHANGE BIT(2)
  39. /**
  40. * struct clk_notifier - associate a clk with a notifier
  41. * @clk: struct clk * to associate the notifier with
  42. * @notifier_head: a blocking_notifier_head for this clk
  43. * @node: linked list pointers
  44. *
  45. * A list of struct clk_notifier is maintained by the notifier code.
  46. * An entry is created whenever code registers the first notifier on a
  47. * particular @clk. Future notifiers on that @clk are added to the
  48. * @notifier_head.
  49. */
  50. struct clk_notifier {
  51. struct clk *clk;
  52. struct srcu_notifier_head notifier_head;
  53. struct list_head node;
  54. };
  55. /**
  56. * struct clk_notifier_data - rate data to pass to the notifier callback
  57. * @clk: struct clk * being changed
  58. * @old_rate: previous rate of this clk
  59. * @new_rate: new rate of this clk
  60. *
  61. * For a pre-notifier, old_rate is the clk's rate before this rate
  62. * change, and new_rate is what the rate will be in the future. For a
  63. * post-notifier, old_rate and new_rate are both set to the clk's
  64. * current rate (this was done to optimize the implementation).
  65. */
  66. struct clk_notifier_data {
  67. struct clk *clk;
  68. unsigned long old_rate;
  69. unsigned long new_rate;
  70. };
  71. /**
  72. * struct clk_bulk_data - Data used for bulk clk operations.
  73. *
  74. * @id: clock consumer ID
  75. * @clk: struct clk * to store the associated clock
  76. *
  77. * The CLK APIs provide a series of clk_bulk_() API calls as
  78. * a convenience to consumers which require multiple clks. This
  79. * structure is used to manage data for these calls.
  80. */
  81. struct clk_bulk_data {
  82. const char *id;
  83. struct clk *clk;
  84. };
  85. #ifdef CONFIG_COMMON_CLK
  86. /**
  87. * clk_notifier_register: register a clock rate-change notifier callback
  88. * @clk: clock whose rate we are interested in
  89. * @nb: notifier block with callback function pointer
  90. *
  91. * ProTip: debugging across notifier chains can be frustrating. Make sure that
  92. * your notifier callback function prints a nice big warning in case of
  93. * failure.
  94. */
  95. int clk_notifier_register(struct clk *clk, struct notifier_block *nb);
  96. /**
  97. * clk_notifier_unregister: unregister a clock rate-change notifier callback
  98. * @clk: clock whose rate we are no longer interested in
  99. * @nb: notifier block which will be unregistered
  100. */
  101. int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb);
  102. /**
  103. * devm_clk_notifier_register - register a managed rate-change notifier callback
  104. * @dev: device for clock "consumer"
  105. * @clk: clock whose rate we are interested in
  106. * @nb: notifier block with callback function pointer
  107. *
  108. * Returns 0 on success, -EERROR otherwise
  109. */
  110. int devm_clk_notifier_register(struct device *dev, struct clk *clk,
  111. struct notifier_block *nb);
  112. /**
  113. * clk_get_accuracy - obtain the clock accuracy in ppb (parts per billion)
  114. * for a clock source.
  115. * @clk: clock source
  116. *
  117. * This gets the clock source accuracy expressed in ppb.
  118. * A perfect clock returns 0.
  119. */
  120. long clk_get_accuracy(struct clk *clk);
  121. /**
  122. * clk_set_phase - adjust the phase shift of a clock signal
  123. * @clk: clock signal source
  124. * @degrees: number of degrees the signal is shifted
  125. *
  126. * Shifts the phase of a clock signal by the specified degrees. Returns 0 on
  127. * success, -EERROR otherwise.
  128. */
  129. int clk_set_phase(struct clk *clk, int degrees);
  130. /**
  131. * clk_get_phase - return the phase shift of a clock signal
  132. * @clk: clock signal source
  133. *
  134. * Returns the phase shift of a clock node in degrees, otherwise returns
  135. * -EERROR.
  136. */
  137. int clk_get_phase(struct clk *clk);
  138. /**
  139. * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
  140. * @clk: clock signal source
  141. * @num: numerator of the duty cycle ratio to be applied
  142. * @den: denominator of the duty cycle ratio to be applied
  143. *
  144. * Adjust the duty cycle of a clock signal by the specified ratio. Returns 0 on
  145. * success, -EERROR otherwise.
  146. */
  147. int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den);
  148. /**
  149. * clk_get_duty_cycle - return the duty cycle ratio of a clock signal
  150. * @clk: clock signal source
  151. * @scale: scaling factor to be applied to represent the ratio as an integer
  152. *
  153. * Returns the duty cycle ratio multiplied by the scale provided, otherwise
  154. * returns -EERROR.
  155. */
  156. int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale);
  157. /**
  158. * clk_is_match - check if two clk's point to the same hardware clock
  159. * @p: clk compared against q
  160. * @q: clk compared against p
  161. *
  162. * Returns true if the two struct clk pointers both point to the same hardware
  163. * clock node. Put differently, returns true if @p and @q
  164. * share the same &struct clk_core object.
  165. *
  166. * Returns false otherwise. Note that two NULL clks are treated as matching.
  167. */
  168. bool clk_is_match(const struct clk *p, const struct clk *q);
  169. #else
  170. static inline int clk_notifier_register(struct clk *clk,
  171. struct notifier_block *nb)
  172. {
  173. return -ENOTSUPP;
  174. }
  175. static inline int clk_notifier_unregister(struct clk *clk,
  176. struct notifier_block *nb)
  177. {
  178. return -ENOTSUPP;
  179. }
  180. static inline int devm_clk_notifier_register(struct device *dev,
  181. struct clk *clk,
  182. struct notifier_block *nb)
  183. {
  184. return -ENOTSUPP;
  185. }
  186. static inline long clk_get_accuracy(struct clk *clk)
  187. {
  188. return -ENOTSUPP;
  189. }
  190. static inline long clk_set_phase(struct clk *clk, int phase)
  191. {
  192. return -ENOTSUPP;
  193. }
  194. static inline long clk_get_phase(struct clk *clk)
  195. {
  196. return -ENOTSUPP;
  197. }
  198. static inline int clk_set_duty_cycle(struct clk *clk, unsigned int num,
  199. unsigned int den)
  200. {
  201. return -ENOTSUPP;
  202. }
  203. static inline unsigned int clk_get_scaled_duty_cycle(struct clk *clk,
  204. unsigned int scale)
  205. {
  206. return 0;
  207. }
  208. static inline bool clk_is_match(const struct clk *p, const struct clk *q)
  209. {
  210. return p == q;
  211. }
  212. #endif
  213. /**
  214. * clk_prepare - prepare a clock source
  215. * @clk: clock source
  216. *
  217. * This prepares the clock source for use.
  218. *
  219. * Must not be called from within atomic context.
  220. */
  221. #ifdef CONFIG_HAVE_CLK_PREPARE
  222. int clk_prepare(struct clk *clk);
  223. int __must_check clk_bulk_prepare(int num_clks,
  224. const struct clk_bulk_data *clks);
  225. #else
  226. static inline int clk_prepare(struct clk *clk)
  227. {
  228. might_sleep();
  229. return 0;
  230. }
  231. static inline int __must_check
  232. clk_bulk_prepare(int num_clks, const struct clk_bulk_data *clks)
  233. {
  234. might_sleep();
  235. return 0;
  236. }
  237. #endif
  238. /**
  239. * clk_unprepare - undo preparation of a clock source
  240. * @clk: clock source
  241. *
  242. * This undoes a previously prepared clock. The caller must balance
  243. * the number of prepare and unprepare calls.
  244. *
  245. * Must not be called from within atomic context.
  246. */
  247. #ifdef CONFIG_HAVE_CLK_PREPARE
  248. void clk_unprepare(struct clk *clk);
  249. void clk_bulk_unprepare(int num_clks, const struct clk_bulk_data *clks);
  250. #else
  251. static inline void clk_unprepare(struct clk *clk)
  252. {
  253. might_sleep();
  254. }
  255. static inline void clk_bulk_unprepare(int num_clks,
  256. const struct clk_bulk_data *clks)
  257. {
  258. might_sleep();
  259. }
  260. #endif
  261. #ifdef CONFIG_HAVE_CLK
  262. /**
  263. * clk_get - lookup and obtain a reference to a clock producer.
  264. * @dev: device for clock "consumer"
  265. * @id: clock consumer ID
  266. *
  267. * Returns a struct clk corresponding to the clock producer, or
  268. * valid IS_ERR() condition containing errno. The implementation
  269. * uses @dev and @id to determine the clock consumer, and thereby
  270. * the clock producer. (IOW, @id may be identical strings, but
  271. * clk_get may return different clock producers depending on @dev.)
  272. *
  273. * Drivers must assume that the clock source is not enabled.
  274. *
  275. * clk_get should not be called from within interrupt context.
  276. */
  277. struct clk *clk_get(struct device *dev, const char *id);
  278. /**
  279. * clk_bulk_get - lookup and obtain a number of references to clock producer.
  280. * @dev: device for clock "consumer"
  281. * @num_clks: the number of clk_bulk_data
  282. * @clks: the clk_bulk_data table of consumer
  283. *
  284. * This helper function allows drivers to get several clk consumers in one
  285. * operation. If any of the clk cannot be acquired then any clks
  286. * that were obtained will be freed before returning to the caller.
  287. *
  288. * Returns 0 if all clocks specified in clk_bulk_data table are obtained
  289. * successfully, or valid IS_ERR() condition containing errno.
  290. * The implementation uses @dev and @clk_bulk_data.id to determine the
  291. * clock consumer, and thereby the clock producer.
  292. * The clock returned is stored in each @clk_bulk_data.clk field.
  293. *
  294. * Drivers must assume that the clock source is not enabled.
  295. *
  296. * clk_bulk_get should not be called from within interrupt context.
  297. */
  298. int __must_check clk_bulk_get(struct device *dev, int num_clks,
  299. struct clk_bulk_data *clks);
  300. /**
  301. * clk_bulk_get_all - lookup and obtain all available references to clock
  302. * producer.
  303. * @dev: device for clock "consumer"
  304. * @clks: pointer to the clk_bulk_data table of consumer
  305. *
  306. * This helper function allows drivers to get all clk consumers in one
  307. * operation. If any of the clk cannot be acquired then any clks
  308. * that were obtained will be freed before returning to the caller.
  309. *
  310. * Returns a positive value for the number of clocks obtained while the
  311. * clock references are stored in the clk_bulk_data table in @clks field.
  312. * Returns 0 if there're none and a negative value if something failed.
  313. *
  314. * Drivers must assume that the clock source is not enabled.
  315. *
  316. * clk_bulk_get should not be called from within interrupt context.
  317. */
  318. int __must_check clk_bulk_get_all(struct device *dev,
  319. struct clk_bulk_data **clks);
  320. /**
  321. * clk_bulk_get_optional - lookup and obtain a number of references to clock producer
  322. * @dev: device for clock "consumer"
  323. * @num_clks: the number of clk_bulk_data
  324. * @clks: the clk_bulk_data table of consumer
  325. *
  326. * Behaves the same as clk_bulk_get() except where there is no clock producer.
  327. * In this case, instead of returning -ENOENT, the function returns 0 and
  328. * NULL for a clk for which a clock producer could not be determined.
  329. */
  330. int __must_check clk_bulk_get_optional(struct device *dev, int num_clks,
  331. struct clk_bulk_data *clks);
  332. /**
  333. * devm_clk_bulk_get - managed get multiple clk consumers
  334. * @dev: device for clock "consumer"
  335. * @num_clks: the number of clk_bulk_data
  336. * @clks: the clk_bulk_data table of consumer
  337. *
  338. * Return 0 on success, an errno on failure.
  339. *
  340. * This helper function allows drivers to get several clk
  341. * consumers in one operation with management, the clks will
  342. * automatically be freed when the device is unbound.
  343. */
  344. int __must_check devm_clk_bulk_get(struct device *dev, int num_clks,
  345. struct clk_bulk_data *clks);
  346. /**
  347. * devm_clk_bulk_get_optional - managed get multiple optional consumer clocks
  348. * @dev: device for clock "consumer"
  349. * @num_clks: the number of clk_bulk_data
  350. * @clks: pointer to the clk_bulk_data table of consumer
  351. *
  352. * Behaves the same as devm_clk_bulk_get() except where there is no clock
  353. * producer. In this case, instead of returning -ENOENT, the function returns
  354. * NULL for given clk. It is assumed all clocks in clk_bulk_data are optional.
  355. *
  356. * Returns 0 if all clocks specified in clk_bulk_data table are obtained
  357. * successfully or for any clk there was no clk provider available, otherwise
  358. * returns valid IS_ERR() condition containing errno.
  359. * The implementation uses @dev and @clk_bulk_data.id to determine the
  360. * clock consumer, and thereby the clock producer.
  361. * The clock returned is stored in each @clk_bulk_data.clk field.
  362. *
  363. * Drivers must assume that the clock source is not enabled.
  364. *
  365. * clk_bulk_get should not be called from within interrupt context.
  366. */
  367. int __must_check devm_clk_bulk_get_optional(struct device *dev, int num_clks,
  368. struct clk_bulk_data *clks);
  369. /**
  370. * devm_clk_bulk_get_all - managed get multiple clk consumers
  371. * @dev: device for clock "consumer"
  372. * @clks: pointer to the clk_bulk_data table of consumer
  373. *
  374. * Returns a positive value for the number of clocks obtained while the
  375. * clock references are stored in the clk_bulk_data table in @clks field.
  376. * Returns 0 if there're none and a negative value if something failed.
  377. *
  378. * This helper function allows drivers to get several clk
  379. * consumers in one operation with management, the clks will
  380. * automatically be freed when the device is unbound.
  381. */
  382. int __must_check devm_clk_bulk_get_all(struct device *dev,
  383. struct clk_bulk_data **clks);
  384. /**
  385. * devm_clk_get - lookup and obtain a managed reference to a clock producer.
  386. * @dev: device for clock "consumer"
  387. * @id: clock consumer ID
  388. *
  389. * Returns a struct clk corresponding to the clock producer, or
  390. * valid IS_ERR() condition containing errno. The implementation
  391. * uses @dev and @id to determine the clock consumer, and thereby
  392. * the clock producer. (IOW, @id may be identical strings, but
  393. * clk_get may return different clock producers depending on @dev.)
  394. *
  395. * Drivers must assume that the clock source is not enabled.
  396. *
  397. * devm_clk_get should not be called from within interrupt context.
  398. *
  399. * The clock will automatically be freed when the device is unbound
  400. * from the bus.
  401. */
  402. struct clk *devm_clk_get(struct device *dev, const char *id);
  403. /**
  404. * devm_clk_get_optional - lookup and obtain a managed reference to an optional
  405. * clock producer.
  406. * @dev: device for clock "consumer"
  407. * @id: clock consumer ID
  408. *
  409. * Behaves the same as devm_clk_get() except where there is no clock producer.
  410. * In this case, instead of returning -ENOENT, the function returns NULL.
  411. */
  412. struct clk *devm_clk_get_optional(struct device *dev, const char *id);
  413. /**
  414. * devm_get_clk_from_child - lookup and obtain a managed reference to a
  415. * clock producer from child node.
  416. * @dev: device for clock "consumer"
  417. * @np: pointer to clock consumer node
  418. * @con_id: clock consumer ID
  419. *
  420. * This function parses the clocks, and uses them to look up the
  421. * struct clk from the registered list of clock providers by using
  422. * @np and @con_id
  423. *
  424. * The clock will automatically be freed when the device is unbound
  425. * from the bus.
  426. */
  427. struct clk *devm_get_clk_from_child(struct device *dev,
  428. struct device_node *np, const char *con_id);
  429. /**
  430. * clk_rate_exclusive_get - get exclusivity over the rate control of a
  431. * producer
  432. * @clk: clock source
  433. *
  434. * This function allows drivers to get exclusive control over the rate of a
  435. * provider. It prevents any other consumer to execute, even indirectly,
  436. * opereation which could alter the rate of the provider or cause glitches
  437. *
  438. * If exlusivity is claimed more than once on clock, even by the same driver,
  439. * the rate effectively gets locked as exclusivity can't be preempted.
  440. *
  441. * Must not be called from within atomic context.
  442. *
  443. * Returns success (0) or negative errno.
  444. */
  445. int clk_rate_exclusive_get(struct clk *clk);
  446. /**
  447. * clk_rate_exclusive_put - release exclusivity over the rate control of a
  448. * producer
  449. * @clk: clock source
  450. *
  451. * This function allows drivers to release the exclusivity it previously got
  452. * from clk_rate_exclusive_get()
  453. *
  454. * The caller must balance the number of clk_rate_exclusive_get() and
  455. * clk_rate_exclusive_put() calls.
  456. *
  457. * Must not be called from within atomic context.
  458. */
  459. void clk_rate_exclusive_put(struct clk *clk);
  460. /**
  461. * clk_enable - inform the system when the clock source should be running.
  462. * @clk: clock source
  463. *
  464. * If the clock can not be enabled/disabled, this should return success.
  465. *
  466. * May be called from atomic contexts.
  467. *
  468. * Returns success (0) or negative errno.
  469. */
  470. int clk_enable(struct clk *clk);
  471. /**
  472. * clk_bulk_enable - inform the system when the set of clks should be running.
  473. * @num_clks: the number of clk_bulk_data
  474. * @clks: the clk_bulk_data table of consumer
  475. *
  476. * May be called from atomic contexts.
  477. *
  478. * Returns success (0) or negative errno.
  479. */
  480. int __must_check clk_bulk_enable(int num_clks,
  481. const struct clk_bulk_data *clks);
  482. /**
  483. * clk_disable - inform the system when the clock source is no longer required.
  484. * @clk: clock source
  485. *
  486. * Inform the system that a clock source is no longer required by
  487. * a driver and may be shut down.
  488. *
  489. * May be called from atomic contexts.
  490. *
  491. * Implementation detail: if the clock source is shared between
  492. * multiple drivers, clk_enable() calls must be balanced by the
  493. * same number of clk_disable() calls for the clock source to be
  494. * disabled.
  495. */
  496. void clk_disable(struct clk *clk);
  497. /**
  498. * clk_bulk_disable - inform the system when the set of clks is no
  499. * longer required.
  500. * @num_clks: the number of clk_bulk_data
  501. * @clks: the clk_bulk_data table of consumer
  502. *
  503. * Inform the system that a set of clks is no longer required by
  504. * a driver and may be shut down.
  505. *
  506. * May be called from atomic contexts.
  507. *
  508. * Implementation detail: if the set of clks is shared between
  509. * multiple drivers, clk_bulk_enable() calls must be balanced by the
  510. * same number of clk_bulk_disable() calls for the clock source to be
  511. * disabled.
  512. */
  513. void clk_bulk_disable(int num_clks, const struct clk_bulk_data *clks);
  514. /**
  515. * clk_get_rate - obtain the current clock rate (in Hz) for a clock source.
  516. * This is only valid once the clock source has been enabled.
  517. * @clk: clock source
  518. */
  519. unsigned long clk_get_rate(struct clk *clk);
  520. /**
  521. * clk_put - "free" the clock source
  522. * @clk: clock source
  523. *
  524. * Note: drivers must ensure that all clk_enable calls made on this
  525. * clock source are balanced by clk_disable calls prior to calling
  526. * this function.
  527. *
  528. * clk_put should not be called from within interrupt context.
  529. */
  530. void clk_put(struct clk *clk);
  531. /**
  532. * clk_bulk_put - "free" the clock source
  533. * @num_clks: the number of clk_bulk_data
  534. * @clks: the clk_bulk_data table of consumer
  535. *
  536. * Note: drivers must ensure that all clk_bulk_enable calls made on this
  537. * clock source are balanced by clk_bulk_disable calls prior to calling
  538. * this function.
  539. *
  540. * clk_bulk_put should not be called from within interrupt context.
  541. */
  542. void clk_bulk_put(int num_clks, struct clk_bulk_data *clks);
  543. /**
  544. * clk_bulk_put_all - "free" all the clock source
  545. * @num_clks: the number of clk_bulk_data
  546. * @clks: the clk_bulk_data table of consumer
  547. *
  548. * Note: drivers must ensure that all clk_bulk_enable calls made on this
  549. * clock source are balanced by clk_bulk_disable calls prior to calling
  550. * this function.
  551. *
  552. * clk_bulk_put_all should not be called from within interrupt context.
  553. */
  554. void clk_bulk_put_all(int num_clks, struct clk_bulk_data *clks);
  555. /**
  556. * devm_clk_put - "free" a managed clock source
  557. * @dev: device used to acquire the clock
  558. * @clk: clock source acquired with devm_clk_get()
  559. *
  560. * Note: drivers must ensure that all clk_enable calls made on this
  561. * clock source are balanced by clk_disable calls prior to calling
  562. * this function.
  563. *
  564. * clk_put should not be called from within interrupt context.
  565. */
  566. void devm_clk_put(struct device *dev, struct clk *clk);
  567. /*
  568. * The remaining APIs are optional for machine class support.
  569. */
  570. /**
  571. * clk_round_rate - adjust a rate to the exact rate a clock can provide
  572. * @clk: clock source
  573. * @rate: desired clock rate in Hz
  574. *
  575. * This answers the question "if I were to pass @rate to clk_set_rate(),
  576. * what clock rate would I end up with?" without changing the hardware
  577. * in any way. In other words:
  578. *
  579. * rate = clk_round_rate(clk, r);
  580. *
  581. * and:
  582. *
  583. * clk_set_rate(clk, r);
  584. * rate = clk_get_rate(clk);
  585. *
  586. * are equivalent except the former does not modify the clock hardware
  587. * in any way.
  588. *
  589. * Returns rounded clock rate in Hz, or negative errno.
  590. */
  591. long clk_round_rate(struct clk *clk, unsigned long rate);
  592. /**
  593. * clk_set_rate - set the clock rate for a clock source
  594. * @clk: clock source
  595. * @rate: desired clock rate in Hz
  596. *
  597. * Updating the rate starts at the top-most affected clock and then
  598. * walks the tree down to the bottom-most clock that needs updating.
  599. *
  600. * Returns success (0) or negative errno.
  601. */
  602. int clk_set_rate(struct clk *clk, unsigned long rate);
  603. /**
  604. * clk_set_rate_exclusive- set the clock rate and claim exclusivity over
  605. * clock source
  606. * @clk: clock source
  607. * @rate: desired clock rate in Hz
  608. *
  609. * This helper function allows drivers to atomically set the rate of a producer
  610. * and claim exclusivity over the rate control of the producer.
  611. *
  612. * It is essentially a combination of clk_set_rate() and
  613. * clk_rate_exclusite_get(). Caller must balance this call with a call to
  614. * clk_rate_exclusive_put()
  615. *
  616. * Returns success (0) or negative errno.
  617. */
  618. int clk_set_rate_exclusive(struct clk *clk, unsigned long rate);
  619. /**
  620. * clk_has_parent - check if a clock is a possible parent for another
  621. * @clk: clock source
  622. * @parent: parent clock source
  623. *
  624. * This function can be used in drivers that need to check that a clock can be
  625. * the parent of another without actually changing the parent.
  626. *
  627. * Returns true if @parent is a possible parent for @clk, false otherwise.
  628. */
  629. bool clk_has_parent(struct clk *clk, struct clk *parent);
  630. /**
  631. * clk_set_rate_range - set a rate range for a clock source
  632. * @clk: clock source
  633. * @min: desired minimum clock rate in Hz, inclusive
  634. * @max: desired maximum clock rate in Hz, inclusive
  635. *
  636. * Returns success (0) or negative errno.
  637. */
  638. int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max);
  639. /**
  640. * clk_set_min_rate - set a minimum clock rate for a clock source
  641. * @clk: clock source
  642. * @rate: desired minimum clock rate in Hz, inclusive
  643. *
  644. * Returns success (0) or negative errno.
  645. */
  646. int clk_set_min_rate(struct clk *clk, unsigned long rate);
  647. /**
  648. * clk_set_max_rate - set a maximum clock rate for a clock source
  649. * @clk: clock source
  650. * @rate: desired maximum clock rate in Hz, inclusive
  651. *
  652. * Returns success (0) or negative errno.
  653. */
  654. int clk_set_max_rate(struct clk *clk, unsigned long rate);
  655. /**
  656. * clk_set_parent - set the parent clock source for this clock
  657. * @clk: clock source
  658. * @parent: parent clock source
  659. *
  660. * Returns success (0) or negative errno.
  661. */
  662. int clk_set_parent(struct clk *clk, struct clk *parent);
  663. /**
  664. * clk_get_parent - get the parent clock source for this clock
  665. * @clk: clock source
  666. *
  667. * Returns struct clk corresponding to parent clock source, or
  668. * valid IS_ERR() condition containing errno.
  669. */
  670. struct clk *clk_get_parent(struct clk *clk);
  671. /**
  672. * clk_get_sys - get a clock based upon the device name
  673. * @dev_id: device name
  674. * @con_id: connection ID
  675. *
  676. * Returns a struct clk corresponding to the clock producer, or
  677. * valid IS_ERR() condition containing errno. The implementation
  678. * uses @dev_id and @con_id to determine the clock consumer, and
  679. * thereby the clock producer. In contrast to clk_get() this function
  680. * takes the device name instead of the device itself for identification.
  681. *
  682. * Drivers must assume that the clock source is not enabled.
  683. *
  684. * clk_get_sys should not be called from within interrupt context.
  685. */
  686. struct clk *clk_get_sys(const char *dev_id, const char *con_id);
  687. /**
  688. * clk_save_context - save clock context for poweroff
  689. *
  690. * Saves the context of the clock register for powerstates in which the
  691. * contents of the registers will be lost. Occurs deep within the suspend
  692. * code so locking is not necessary.
  693. */
  694. int clk_save_context(void);
  695. /**
  696. * clk_restore_context - restore clock context after poweroff
  697. *
  698. * This occurs with all clocks enabled. Occurs deep within the resume code
  699. * so locking is not necessary.
  700. */
  701. void clk_restore_context(void);
  702. #else /* !CONFIG_HAVE_CLK */
  703. static inline struct clk *clk_get(struct device *dev, const char *id)
  704. {
  705. return NULL;
  706. }
  707. static inline int __must_check clk_bulk_get(struct device *dev, int num_clks,
  708. struct clk_bulk_data *clks)
  709. {
  710. return 0;
  711. }
  712. static inline int __must_check clk_bulk_get_optional(struct device *dev,
  713. int num_clks, struct clk_bulk_data *clks)
  714. {
  715. return 0;
  716. }
  717. static inline int __must_check clk_bulk_get_all(struct device *dev,
  718. struct clk_bulk_data **clks)
  719. {
  720. return 0;
  721. }
  722. static inline struct clk *devm_clk_get(struct device *dev, const char *id)
  723. {
  724. return NULL;
  725. }
  726. static inline struct clk *devm_clk_get_optional(struct device *dev,
  727. const char *id)
  728. {
  729. return NULL;
  730. }
  731. static inline int __must_check devm_clk_bulk_get(struct device *dev, int num_clks,
  732. struct clk_bulk_data *clks)
  733. {
  734. return 0;
  735. }
  736. static inline int __must_check devm_clk_bulk_get_optional(struct device *dev,
  737. int num_clks, struct clk_bulk_data *clks)
  738. {
  739. return 0;
  740. }
  741. static inline int __must_check devm_clk_bulk_get_all(struct device *dev,
  742. struct clk_bulk_data **clks)
  743. {
  744. return 0;
  745. }
  746. static inline struct clk *devm_get_clk_from_child(struct device *dev,
  747. struct device_node *np, const char *con_id)
  748. {
  749. return NULL;
  750. }
  751. static inline void clk_put(struct clk *clk) {}
  752. static inline void clk_bulk_put(int num_clks, struct clk_bulk_data *clks) {}
  753. static inline void clk_bulk_put_all(int num_clks, struct clk_bulk_data *clks) {}
  754. static inline void devm_clk_put(struct device *dev, struct clk *clk) {}
  755. static inline int clk_rate_exclusive_get(struct clk *clk)
  756. {
  757. return 0;
  758. }
  759. static inline void clk_rate_exclusive_put(struct clk *clk) {}
  760. static inline int clk_enable(struct clk *clk)
  761. {
  762. return 0;
  763. }
  764. static inline int __must_check clk_bulk_enable(int num_clks,
  765. const struct clk_bulk_data *clks)
  766. {
  767. return 0;
  768. }
  769. static inline void clk_disable(struct clk *clk) {}
  770. static inline void clk_bulk_disable(int num_clks,
  771. const struct clk_bulk_data *clks) {}
  772. static inline unsigned long clk_get_rate(struct clk *clk)
  773. {
  774. return 0;
  775. }
  776. static inline int clk_set_rate(struct clk *clk, unsigned long rate)
  777. {
  778. return 0;
  779. }
  780. static inline int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
  781. {
  782. return 0;
  783. }
  784. static inline long clk_round_rate(struct clk *clk, unsigned long rate)
  785. {
  786. return 0;
  787. }
  788. static inline bool clk_has_parent(struct clk *clk, struct clk *parent)
  789. {
  790. return true;
  791. }
  792. static inline int clk_set_rate_range(struct clk *clk, unsigned long min,
  793. unsigned long max)
  794. {
  795. return 0;
  796. }
  797. static inline int clk_set_min_rate(struct clk *clk, unsigned long rate)
  798. {
  799. return 0;
  800. }
  801. static inline int clk_set_max_rate(struct clk *clk, unsigned long rate)
  802. {
  803. return 0;
  804. }
  805. static inline int clk_set_parent(struct clk *clk, struct clk *parent)
  806. {
  807. return 0;
  808. }
  809. static inline struct clk *clk_get_parent(struct clk *clk)
  810. {
  811. return NULL;
  812. }
  813. static inline struct clk *clk_get_sys(const char *dev_id, const char *con_id)
  814. {
  815. return NULL;
  816. }
  817. static inline int clk_save_context(void)
  818. {
  819. return 0;
  820. }
  821. static inline void clk_restore_context(void) {}
  822. #endif
  823. /* clk_prepare_enable helps cases using clk_enable in non-atomic context. */
  824. static inline int clk_prepare_enable(struct clk *clk)
  825. {
  826. int ret;
  827. ret = clk_prepare(clk);
  828. if (ret)
  829. return ret;
  830. ret = clk_enable(clk);
  831. if (ret)
  832. clk_unprepare(clk);
  833. return ret;
  834. }
  835. /* clk_disable_unprepare helps cases using clk_disable in non-atomic context. */
  836. static inline void clk_disable_unprepare(struct clk *clk)
  837. {
  838. clk_disable(clk);
  839. clk_unprepare(clk);
  840. }
  841. static inline int __must_check
  842. clk_bulk_prepare_enable(int num_clks, const struct clk_bulk_data *clks)
  843. {
  844. int ret;
  845. ret = clk_bulk_prepare(num_clks, clks);
  846. if (ret)
  847. return ret;
  848. ret = clk_bulk_enable(num_clks, clks);
  849. if (ret)
  850. clk_bulk_unprepare(num_clks, clks);
  851. return ret;
  852. }
  853. static inline void clk_bulk_disable_unprepare(int num_clks,
  854. const struct clk_bulk_data *clks)
  855. {
  856. clk_bulk_disable(num_clks, clks);
  857. clk_bulk_unprepare(num_clks, clks);
  858. }
  859. /**
  860. * clk_get_optional - lookup and obtain a reference to an optional clock
  861. * producer.
  862. * @dev: device for clock "consumer"
  863. * @id: clock consumer ID
  864. *
  865. * Behaves the same as clk_get() except where there is no clock producer. In
  866. * this case, instead of returning -ENOENT, the function returns NULL.
  867. */
  868. static inline struct clk *clk_get_optional(struct device *dev, const char *id)
  869. {
  870. struct clk *clk = clk_get(dev, id);
  871. if (clk == ERR_PTR(-ENOENT))
  872. return NULL;
  873. return clk;
  874. }
  875. #if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK)
  876. struct clk *of_clk_get(struct device_node *np, int index);
  877. struct clk *of_clk_get_by_name(struct device_node *np, const char *name);
  878. struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec);
  879. #else
  880. static inline struct clk *of_clk_get(struct device_node *np, int index)
  881. {
  882. return ERR_PTR(-ENOENT);
  883. }
  884. static inline struct clk *of_clk_get_by_name(struct device_node *np,
  885. const char *name)
  886. {
  887. return ERR_PTR(-ENOENT);
  888. }
  889. static inline struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
  890. {
  891. return ERR_PTR(-ENOENT);
  892. }
  893. #endif
  894. #endif