clk.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * linux/include/linux/clk.h
  3. *
  4. * Copyright (C) 2004 ARM Limited.
  5. * Written by Deep Blue Solutions Limited.
  6. * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #ifndef __LINUX_CLK_H
  13. #define __LINUX_CLK_H
  14. #include <linux/err.h>
  15. #include <linux/kernel.h>
  16. #include <linux/notifier.h>
  17. struct device;
  18. struct clk;
  19. /**
  20. * DOC: clk notifier callback types
  21. *
  22. * PRE_RATE_CHANGE - called immediately before the clk rate is changed,
  23. * to indicate that the rate change will proceed. Drivers must
  24. * immediately terminate any operations that will be affected by the
  25. * rate change. Callbacks may either return NOTIFY_DONE, NOTIFY_OK,
  26. * NOTIFY_STOP or NOTIFY_BAD.
  27. *
  28. * ABORT_RATE_CHANGE: called if the rate change failed for some reason
  29. * after PRE_RATE_CHANGE. In this case, all registered notifiers on
  30. * the clk will be called with ABORT_RATE_CHANGE. Callbacks must
  31. * always return NOTIFY_DONE or NOTIFY_OK.
  32. *
  33. * POST_RATE_CHANGE - called after the clk rate change has successfully
  34. * completed. Callbacks must always return NOTIFY_DONE or NOTIFY_OK.
  35. *
  36. */
  37. #define PRE_RATE_CHANGE BIT(0)
  38. #define POST_RATE_CHANGE BIT(1)
  39. #define ABORT_RATE_CHANGE BIT(2)
  40. /**
  41. * struct clk_notifier - associate a clk with a notifier
  42. * @clk: struct clk * to associate the notifier with
  43. * @notifier_head: a blocking_notifier_head for this clk
  44. * @node: linked list pointers
  45. *
  46. * A list of struct clk_notifier is maintained by the notifier code.
  47. * An entry is created whenever code registers the first notifier on a
  48. * particular @clk. Future notifiers on that @clk are added to the
  49. * @notifier_head.
  50. */
  51. struct clk_notifier {
  52. struct clk *clk;
  53. struct srcu_notifier_head notifier_head;
  54. struct list_head node;
  55. };
  56. /**
  57. * struct clk_notifier_data - rate data to pass to the notifier callback
  58. * @clk: struct clk * being changed
  59. * @old_rate: previous rate of this clk
  60. * @new_rate: new rate of this clk
  61. *
  62. * For a pre-notifier, old_rate is the clk's rate before this rate
  63. * change, and new_rate is what the rate will be in the future. For a
  64. * post-notifier, old_rate and new_rate are both set to the clk's
  65. * current rate (this was done to optimize the implementation).
  66. */
  67. struct clk_notifier_data {
  68. struct clk *clk;
  69. unsigned long old_rate;
  70. unsigned long new_rate;
  71. };
  72. #ifdef CONFIG_COMMON_CLK
  73. /**
  74. * clk_notifier_register: register a clock rate-change notifier callback
  75. * @clk: clock whose rate we are interested in
  76. * @nb: notifier block with callback function pointer
  77. *
  78. * ProTip: debugging across notifier chains can be frustrating. Make sure that
  79. * your notifier callback function prints a nice big warning in case of
  80. * failure.
  81. */
  82. int clk_notifier_register(struct clk *clk, struct notifier_block *nb);
  83. /**
  84. * clk_notifier_unregister: unregister a clock rate-change notifier callback
  85. * @clk: clock whose rate we are no longer interested in
  86. * @nb: notifier block which will be unregistered
  87. */
  88. int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb);
  89. /**
  90. * clk_get_accuracy - obtain the clock accuracy in ppb (parts per billion)
  91. * for a clock source.
  92. * @clk: clock source
  93. *
  94. * This gets the clock source accuracy expressed in ppb.
  95. * A perfect clock returns 0.
  96. */
  97. long clk_get_accuracy(struct clk *clk);
  98. /**
  99. * clk_set_phase - adjust the phase shift of a clock signal
  100. * @clk: clock signal source
  101. * @degrees: number of degrees the signal is shifted
  102. *
  103. * Shifts the phase of a clock signal by the specified degrees. Returns 0 on
  104. * success, -EERROR otherwise.
  105. */
  106. int clk_set_phase(struct clk *clk, int degrees);
  107. /**
  108. * clk_get_phase - return the phase shift of a clock signal
  109. * @clk: clock signal source
  110. *
  111. * Returns the phase shift of a clock node in degrees, otherwise returns
  112. * -EERROR.
  113. */
  114. int clk_get_phase(struct clk *clk);
  115. /**
  116. * clk_is_match - check if two clk's point to the same hardware clock
  117. * @p: clk compared against q
  118. * @q: clk compared against p
  119. *
  120. * Returns true if the two struct clk pointers both point to the same hardware
  121. * clock node. Put differently, returns true if struct clk *p and struct clk *q
  122. * share the same struct clk_core object.
  123. *
  124. * Returns false otherwise. Note that two NULL clks are treated as matching.
  125. */
  126. bool clk_is_match(const struct clk *p, const struct clk *q);
  127. #else
  128. static inline int clk_notifier_register(struct clk *clk,
  129. struct notifier_block *nb)
  130. {
  131. return -ENOTSUPP;
  132. }
  133. static inline int clk_notifier_unregister(struct clk *clk,
  134. struct notifier_block *nb)
  135. {
  136. return -ENOTSUPP;
  137. }
  138. static inline long clk_get_accuracy(struct clk *clk)
  139. {
  140. return -ENOTSUPP;
  141. }
  142. static inline long clk_set_phase(struct clk *clk, int phase)
  143. {
  144. return -ENOTSUPP;
  145. }
  146. static inline long clk_get_phase(struct clk *clk)
  147. {
  148. return -ENOTSUPP;
  149. }
  150. static inline bool clk_is_match(const struct clk *p, const struct clk *q)
  151. {
  152. return p == q;
  153. }
  154. #endif
  155. /**
  156. * clk_prepare - prepare a clock source
  157. * @clk: clock source
  158. *
  159. * This prepares the clock source for use.
  160. *
  161. * Must not be called from within atomic context.
  162. */
  163. #ifdef CONFIG_HAVE_CLK_PREPARE
  164. int clk_prepare(struct clk *clk);
  165. #else
  166. static inline int clk_prepare(struct clk *clk)
  167. {
  168. might_sleep();
  169. return 0;
  170. }
  171. #endif
  172. /**
  173. * clk_unprepare - undo preparation of a clock source
  174. * @clk: clock source
  175. *
  176. * This undoes a previously prepared clock. The caller must balance
  177. * the number of prepare and unprepare calls.
  178. *
  179. * Must not be called from within atomic context.
  180. */
  181. #ifdef CONFIG_HAVE_CLK_PREPARE
  182. void clk_unprepare(struct clk *clk);
  183. #else
  184. static inline void clk_unprepare(struct clk *clk)
  185. {
  186. might_sleep();
  187. }
  188. #endif
  189. #ifdef CONFIG_HAVE_CLK
  190. /**
  191. * clk_get - lookup and obtain a reference to a clock producer.
  192. * @dev: device for clock "consumer"
  193. * @id: clock consumer ID
  194. *
  195. * Returns a struct clk corresponding to the clock producer, or
  196. * valid IS_ERR() condition containing errno. The implementation
  197. * uses @dev and @id to determine the clock consumer, and thereby
  198. * the clock producer. (IOW, @id may be identical strings, but
  199. * clk_get may return different clock producers depending on @dev.)
  200. *
  201. * Drivers must assume that the clock source is not enabled.
  202. *
  203. * clk_get should not be called from within interrupt context.
  204. */
  205. struct clk *clk_get(struct device *dev, const char *id);
  206. /**
  207. * devm_clk_get - lookup and obtain a managed reference to a clock producer.
  208. * @dev: device for clock "consumer"
  209. * @id: clock consumer ID
  210. *
  211. * Returns a struct clk corresponding to the clock producer, or
  212. * valid IS_ERR() condition containing errno. The implementation
  213. * uses @dev and @id to determine the clock consumer, and thereby
  214. * the clock producer. (IOW, @id may be identical strings, but
  215. * clk_get may return different clock producers depending on @dev.)
  216. *
  217. * Drivers must assume that the clock source is not enabled.
  218. *
  219. * devm_clk_get should not be called from within interrupt context.
  220. *
  221. * The clock will automatically be freed when the device is unbound
  222. * from the bus.
  223. */
  224. struct clk *devm_clk_get(struct device *dev, const char *id);
  225. /**
  226. * clk_enable - inform the system when the clock source should be running.
  227. * @clk: clock source
  228. *
  229. * If the clock can not be enabled/disabled, this should return success.
  230. *
  231. * May be called from atomic contexts.
  232. *
  233. * Returns success (0) or negative errno.
  234. */
  235. int clk_enable(struct clk *clk);
  236. /**
  237. * clk_disable - inform the system when the clock source is no longer required.
  238. * @clk: clock source
  239. *
  240. * Inform the system that a clock source is no longer required by
  241. * a driver and may be shut down.
  242. *
  243. * May be called from atomic contexts.
  244. *
  245. * Implementation detail: if the clock source is shared between
  246. * multiple drivers, clk_enable() calls must be balanced by the
  247. * same number of clk_disable() calls for the clock source to be
  248. * disabled.
  249. */
  250. void clk_disable(struct clk *clk);
  251. /**
  252. * clk_get_rate - obtain the current clock rate (in Hz) for a clock source.
  253. * This is only valid once the clock source has been enabled.
  254. * @clk: clock source
  255. */
  256. unsigned long clk_get_rate(struct clk *clk);
  257. /**
  258. * clk_put - "free" the clock source
  259. * @clk: clock source
  260. *
  261. * Note: drivers must ensure that all clk_enable calls made on this
  262. * clock source are balanced by clk_disable calls prior to calling
  263. * this function.
  264. *
  265. * clk_put should not be called from within interrupt context.
  266. */
  267. void clk_put(struct clk *clk);
  268. /**
  269. * devm_clk_put - "free" a managed clock source
  270. * @dev: device used to acquire the clock
  271. * @clk: clock source acquired with devm_clk_get()
  272. *
  273. * Note: drivers must ensure that all clk_enable calls made on this
  274. * clock source are balanced by clk_disable calls prior to calling
  275. * this function.
  276. *
  277. * clk_put should not be called from within interrupt context.
  278. */
  279. void devm_clk_put(struct device *dev, struct clk *clk);
  280. /*
  281. * The remaining APIs are optional for machine class support.
  282. */
  283. /**
  284. * clk_round_rate - adjust a rate to the exact rate a clock can provide
  285. * @clk: clock source
  286. * @rate: desired clock rate in Hz
  287. *
  288. * This answers the question "if I were to pass @rate to clk_set_rate(),
  289. * what clock rate would I end up with?" without changing the hardware
  290. * in any way. In other words:
  291. *
  292. * rate = clk_round_rate(clk, r);
  293. *
  294. * and:
  295. *
  296. * clk_set_rate(clk, r);
  297. * rate = clk_get_rate(clk);
  298. *
  299. * are equivalent except the former does not modify the clock hardware
  300. * in any way.
  301. *
  302. * Returns rounded clock rate in Hz, or negative errno.
  303. */
  304. long clk_round_rate(struct clk *clk, unsigned long rate);
  305. /**
  306. * clk_set_rate - set the clock rate for a clock source
  307. * @clk: clock source
  308. * @rate: desired clock rate in Hz
  309. *
  310. * Returns success (0) or negative errno.
  311. */
  312. int clk_set_rate(struct clk *clk, unsigned long rate);
  313. /**
  314. * clk_has_parent - check if a clock is a possible parent for another
  315. * @clk: clock source
  316. * @parent: parent clock source
  317. *
  318. * This function can be used in drivers that need to check that a clock can be
  319. * the parent of another without actually changing the parent.
  320. *
  321. * Returns true if @parent is a possible parent for @clk, false otherwise.
  322. */
  323. bool clk_has_parent(struct clk *clk, struct clk *parent);
  324. /**
  325. * clk_set_rate_range - set a rate range for a clock source
  326. * @clk: clock source
  327. * @min: desired minimum clock rate in Hz, inclusive
  328. * @max: desired maximum clock rate in Hz, inclusive
  329. *
  330. * Returns success (0) or negative errno.
  331. */
  332. int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max);
  333. /**
  334. * clk_set_min_rate - set a minimum clock rate for a clock source
  335. * @clk: clock source
  336. * @rate: desired minimum clock rate in Hz, inclusive
  337. *
  338. * Returns success (0) or negative errno.
  339. */
  340. int clk_set_min_rate(struct clk *clk, unsigned long rate);
  341. /**
  342. * clk_set_max_rate - set a maximum clock rate for a clock source
  343. * @clk: clock source
  344. * @rate: desired maximum clock rate in Hz, inclusive
  345. *
  346. * Returns success (0) or negative errno.
  347. */
  348. int clk_set_max_rate(struct clk *clk, unsigned long rate);
  349. /**
  350. * clk_set_parent - set the parent clock source for this clock
  351. * @clk: clock source
  352. * @parent: parent clock source
  353. *
  354. * Returns success (0) or negative errno.
  355. */
  356. int clk_set_parent(struct clk *clk, struct clk *parent);
  357. /**
  358. * clk_get_parent - get the parent clock source for this clock
  359. * @clk: clock source
  360. *
  361. * Returns struct clk corresponding to parent clock source, or
  362. * valid IS_ERR() condition containing errno.
  363. */
  364. struct clk *clk_get_parent(struct clk *clk);
  365. /**
  366. * clk_get_sys - get a clock based upon the device name
  367. * @dev_id: device name
  368. * @con_id: connection ID
  369. *
  370. * Returns a struct clk corresponding to the clock producer, or
  371. * valid IS_ERR() condition containing errno. The implementation
  372. * uses @dev_id and @con_id to determine the clock consumer, and
  373. * thereby the clock producer. In contrast to clk_get() this function
  374. * takes the device name instead of the device itself for identification.
  375. *
  376. * Drivers must assume that the clock source is not enabled.
  377. *
  378. * clk_get_sys should not be called from within interrupt context.
  379. */
  380. struct clk *clk_get_sys(const char *dev_id, const char *con_id);
  381. #else /* !CONFIG_HAVE_CLK */
  382. static inline struct clk *clk_get(struct device *dev, const char *id)
  383. {
  384. return NULL;
  385. }
  386. static inline struct clk *devm_clk_get(struct device *dev, const char *id)
  387. {
  388. return NULL;
  389. }
  390. static inline void clk_put(struct clk *clk) {}
  391. static inline void devm_clk_put(struct device *dev, struct clk *clk) {}
  392. static inline int clk_enable(struct clk *clk)
  393. {
  394. return 0;
  395. }
  396. static inline void clk_disable(struct clk *clk) {}
  397. static inline unsigned long clk_get_rate(struct clk *clk)
  398. {
  399. return 0;
  400. }
  401. static inline int clk_set_rate(struct clk *clk, unsigned long rate)
  402. {
  403. return 0;
  404. }
  405. static inline long clk_round_rate(struct clk *clk, unsigned long rate)
  406. {
  407. return 0;
  408. }
  409. static inline bool clk_has_parent(struct clk *clk, struct clk *parent)
  410. {
  411. return true;
  412. }
  413. static inline int clk_set_parent(struct clk *clk, struct clk *parent)
  414. {
  415. return 0;
  416. }
  417. static inline struct clk *clk_get_parent(struct clk *clk)
  418. {
  419. return NULL;
  420. }
  421. static inline struct clk *clk_get_sys(const char *dev_id, const char *con_id)
  422. {
  423. return NULL;
  424. }
  425. #endif
  426. /* clk_prepare_enable helps cases using clk_enable in non-atomic context. */
  427. static inline int clk_prepare_enable(struct clk *clk)
  428. {
  429. int ret;
  430. ret = clk_prepare(clk);
  431. if (ret)
  432. return ret;
  433. ret = clk_enable(clk);
  434. if (ret)
  435. clk_unprepare(clk);
  436. return ret;
  437. }
  438. /* clk_disable_unprepare helps cases using clk_disable in non-atomic context. */
  439. static inline void clk_disable_unprepare(struct clk *clk)
  440. {
  441. clk_disable(clk);
  442. clk_unprepare(clk);
  443. }
  444. struct device_node;
  445. struct of_phandle_args;
  446. #if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK)
  447. struct clk *of_clk_get(struct device_node *np, int index);
  448. struct clk *of_clk_get_by_name(struct device_node *np, const char *name);
  449. struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec);
  450. #else
  451. static inline struct clk *of_clk_get(struct device_node *np, int index)
  452. {
  453. return ERR_PTR(-ENOENT);
  454. }
  455. static inline struct clk *of_clk_get_by_name(struct device_node *np,
  456. const char *name)
  457. {
  458. return ERR_PTR(-ENOENT);
  459. }
  460. #endif
  461. #endif