clk.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (c) 2015 Google, Inc
  4. * Written by Simon Glass <sjg@chromium.org>
  5. * Copyright (c) 2016, NVIDIA CORPORATION.
  6. */
  7. #ifndef _CLK_H_
  8. #define _CLK_H_
  9. #include <dm/ofnode.h>
  10. #include <linux/err.h>
  11. #include <linux/errno.h>
  12. #include <linux/types.h>
  13. /**
  14. * A clock is a hardware signal that oscillates autonomously at a specific
  15. * frequency and duty cycle. Most hardware modules require one or more clock
  16. * signal to drive their operation. Clock signals are typically generated
  17. * externally to the HW module consuming them, by an entity this API calls a
  18. * clock provider. This API provides a standard means for drivers to enable and
  19. * disable clocks, and to set the rate at which they oscillate.
  20. *
  21. * A driver that implements UCLASS_CLK is a clock provider. A provider will
  22. * often implement multiple separate clocks, since the hardware it manages
  23. * often has this capability. clk-uclass.h describes the interface which
  24. * clock providers must implement.
  25. *
  26. * Clock consumers/clients are the HW modules driven by the clock signals. This
  27. * header file describes the API used by drivers for those HW modules.
  28. */
  29. struct udevice;
  30. /**
  31. * struct clk - A handle to (allowing control of) a single clock.
  32. *
  33. * Clients provide storage for clock handles. The content of the structure is
  34. * managed solely by the clock API and clock drivers. A clock struct is
  35. * initialized by "get"ing the clock struct. The clock struct is passed to all
  36. * other clock APIs to identify which clock signal to operate upon.
  37. *
  38. * @dev: The device which implements the clock signal.
  39. * @rate: The clock rate (in HZ).
  40. * @flags: Flags used across common clock structure (e.g. CLK_)
  41. * Clock IP blocks specific flags (i.e. mux, div, gate, etc) are defined
  42. * in struct's for those devices (e.g. struct clk_mux).
  43. * @id: The clock signal ID within the provider.
  44. * @data: An optional data field for scenarios where a single integer ID is not
  45. * sufficient. If used, it can be populated through an .of_xlate op and
  46. * processed during the various clock ops.
  47. *
  48. * Should additional information to identify and configure any clock signal
  49. * for any provider be required in the future, the struct could be expanded to
  50. * either (a) add more fields to allow clock providers to store additional
  51. * information, or (b) replace the id field with an opaque pointer, which the
  52. * provider would dynamically allocated during its .of_xlate op, and process
  53. * during is .request op. This may require the addition of an extra op to clean
  54. * up the allocation.
  55. */
  56. struct clk {
  57. struct udevice *dev;
  58. long long rate; /* in HZ */
  59. u32 flags;
  60. int enable_count;
  61. /*
  62. * Written by of_xlate. In the future, we might add more fields here.
  63. */
  64. unsigned long id;
  65. unsigned long data;
  66. };
  67. /**
  68. * struct clk_bulk - A handle to (allowing control of) a bulk of clocks.
  69. *
  70. * Clients provide storage for the clock bulk. The content of the structure is
  71. * managed solely by the clock API. A clock bulk struct is
  72. * initialized by "get"ing the clock bulk struct.
  73. * The clock bulk struct is passed to all other bulk clock APIs to apply
  74. * the API to all the clock in the bulk struct.
  75. *
  76. * @clks: An array of clock handles.
  77. * @count: The number of clock handles in the clks array.
  78. */
  79. struct clk_bulk {
  80. struct clk *clks;
  81. unsigned int count;
  82. };
  83. #if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(CLK)
  84. struct phandle_1_arg;
  85. /**
  86. * clk_get_by_phandle() - Get a clock by its phandle information (of-platadata)
  87. *
  88. * This function is used when of-platdata is enabled.
  89. *
  90. * This looks up a clock using the phandle info. With dtoc, each phandle in the
  91. * 'clocks' property is transformed into an idx representing the device. For
  92. * example:
  93. *
  94. * clocks = <&dpll_mpu_ck 23>;
  95. *
  96. * might result in:
  97. *
  98. * .clocks = {1, {23}},},
  99. *
  100. * indicating that the clock is udevice idx 1 in dt-plat.c with an argument of
  101. * 23. This function can return a valid clock given the above information. In
  102. * this example it would return a clock containing the 'dpll_mpu_ck' device and
  103. * the clock ID 23.
  104. *
  105. * @dev: Device containing the phandle
  106. * @cells: Phandle info
  107. * @clock: A pointer to a clock struct to initialise
  108. * Return: 0 if OK, or a negative error code.
  109. */
  110. int clk_get_by_phandle(struct udevice *dev, const struct phandle_1_arg *cells,
  111. struct clk *clk);
  112. /**
  113. * clk_get_by_index() - Get/request a clock by integer index.
  114. *
  115. * This looks up and requests a clock. The index is relative to the client
  116. * device; each device is assumed to have n clocks associated with it somehow,
  117. * and this function finds and requests one of them. The mapping of client
  118. * device clock indices to provider clocks may be via device-tree properties,
  119. * board-provided mapping tables, or some other mechanism.
  120. *
  121. * @dev: The client device.
  122. * @index: The index of the clock to request, within the client's list of
  123. * clocks.
  124. * @clock A pointer to a clock struct to initialize.
  125. * Return: 0 if OK, or a negative error code.
  126. */
  127. int clk_get_by_index(struct udevice *dev, int index, struct clk *clk);
  128. /**
  129. * clk_get_by_index_nodev - Get/request a clock by integer index
  130. * without a device.
  131. *
  132. * This is a version of clk_get_by_index() that does not use a device.
  133. *
  134. * @node: The client ofnode.
  135. * @index: The index of the clock to request, within the client's list of
  136. * clocks.
  137. * @clock A pointer to a clock struct to initialize.
  138. * Return: 0 if OK, or a negative error code.
  139. */
  140. int clk_get_by_index_nodev(ofnode node, int index, struct clk *clk);
  141. /**
  142. * clk_get_bulk - Get/request all clocks of a device.
  143. *
  144. * This looks up and requests all clocks of the client device; each device is
  145. * assumed to have n clocks associated with it somehow, and this function finds
  146. * and requests all of them in a separate structure. The mapping of client
  147. * device clock indices to provider clocks may be via device-tree properties,
  148. * board-provided mapping tables, or some other mechanism.
  149. *
  150. * @dev: The client device.
  151. * @bulk A pointer to a clock bulk struct to initialize.
  152. * Return: 0 if OK, or a negative error code.
  153. */
  154. int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk);
  155. /**
  156. * clk_get_by_name - Get/request a clock by name.
  157. *
  158. * This looks up and requests a clock. The name is relative to the client
  159. * device; each device is assumed to have n clocks associated with it somehow,
  160. * and this function finds and requests one of them. The mapping of client
  161. * device clock names to provider clocks may be via device-tree properties,
  162. * board-provided mapping tables, or some other mechanism.
  163. *
  164. * @dev: The client device.
  165. * @name: The name of the clock to request, within the client's list of
  166. * clocks.
  167. * @clock: A pointer to a clock struct to initialize.
  168. * Return: 0 if OK, or a negative error code.
  169. */
  170. int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk);
  171. /**
  172. * clk_get_by_name_nodev - Get/request a clock by name without a device.
  173. *
  174. * This is a version of clk_get_by_name() that does not use a device.
  175. *
  176. * @node: The client ofnode.
  177. * @name: The name of the clock to request, within the client's list of
  178. * clocks.
  179. * @clock: A pointer to a clock struct to initialize.
  180. * Return: 0 if OK, or a negative error code.
  181. */
  182. int clk_get_by_name_nodev(ofnode node, const char *name, struct clk *clk);
  183. /**
  184. * clk_get_optional_nodev - Get/request an optinonal clock by name
  185. * without a device.
  186. * @node: The client ofnode.
  187. * @name: The name of the clock to request.
  188. * @name: The name of the clock to request, within the client's list of
  189. * clocks.
  190. * @clock: A pointer to a clock struct to initialize.
  191. *
  192. * Behaves the same as clk_get_by_name_nodev() except where there is
  193. * no clock producer, in this case, skip the error number -ENODATA, and
  194. * the function returns 0.
  195. */
  196. int clk_get_optional_nodev(ofnode node, const char *name, struct clk *clk);
  197. /**
  198. * devm_clk_get - lookup and obtain a managed reference to a clock producer.
  199. * @dev: device for clock "consumer"
  200. * @id: clock consumer ID
  201. *
  202. * Returns a struct clk corresponding to the clock producer, or
  203. * valid IS_ERR() condition containing errno. The implementation
  204. * uses @dev and @id to determine the clock consumer, and thereby
  205. * the clock producer. (IOW, @id may be identical strings, but
  206. * clk_get may return different clock producers depending on @dev.)
  207. *
  208. * Drivers must assume that the clock source is not enabled.
  209. *
  210. * devm_clk_get should not be called from within interrupt context.
  211. *
  212. * The clock will automatically be freed when the device is unbound
  213. * from the bus.
  214. */
  215. struct clk *devm_clk_get(struct udevice *dev, const char *id);
  216. /**
  217. * devm_clk_get_optional - lookup and obtain a managed reference to an optional
  218. * clock producer.
  219. * @dev: device for clock "consumer"
  220. * @id: clock consumer ID
  221. *
  222. * Behaves the same as devm_clk_get() except where there is no clock producer.
  223. * In this case, instead of returning -ENOENT, the function returns NULL.
  224. */
  225. struct clk *devm_clk_get_optional(struct udevice *dev, const char *id);
  226. /**
  227. * clk_release_all() - Disable (turn off)/Free an array of previously
  228. * requested clocks.
  229. *
  230. * For each clock contained in the clock array, this function will check if
  231. * clock has been previously requested and then will disable and free it.
  232. *
  233. * @clk: A clock struct array that was previously successfully
  234. * requested by clk_request/get_by_*().
  235. * @count Number of clock contained in the array
  236. * Return: zero on success, or -ve error code.
  237. */
  238. int clk_release_all(struct clk *clk, int count);
  239. /**
  240. * devm_clk_put - "free" a managed clock source
  241. * @dev: device used to acquire the clock
  242. * @clk: clock source acquired with devm_clk_get()
  243. *
  244. * Note: drivers must ensure that all clk_enable calls made on this
  245. * clock source are balanced by clk_disable calls prior to calling
  246. * this function.
  247. *
  248. * clk_put should not be called from within interrupt context.
  249. */
  250. void devm_clk_put(struct udevice *dev, struct clk *clk);
  251. #else
  252. static inline int clk_get_by_index(struct udevice *dev, int index,
  253. struct clk *clk)
  254. {
  255. return -ENOSYS;
  256. }
  257. static inline int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk)
  258. {
  259. return -ENOSYS;
  260. }
  261. static inline int clk_get_by_name(struct udevice *dev, const char *name,
  262. struct clk *clk)
  263. {
  264. return -ENOSYS;
  265. }
  266. static inline int
  267. clk_get_by_name_nodev(ofnode node, const char *name, struct clk *clk)
  268. {
  269. return -ENOSYS;
  270. }
  271. static inline int
  272. clk_get_optional_nodev(ofnode node, const char *name, struct clk *clk)
  273. {
  274. return -ENOSYS;
  275. }
  276. static inline int clk_release_all(struct clk *clk, int count)
  277. {
  278. return -ENOSYS;
  279. }
  280. #endif
  281. /**
  282. * enum clk_defaults_stage - What stage clk_set_defaults() is called at
  283. * @CLK_DEFAULTS_PRE: Called before probe. Setting of defaults for clocks owned
  284. * by this clock driver will be defered until after probing.
  285. * @CLK_DEFAULTS_POST: Called after probe. Only defaults for clocks owned by
  286. * this clock driver will be set.
  287. * @CLK_DEFAULTS_POST_FORCE: Called after probe, and always set defaults, even
  288. * before relocation. Usually, defaults are not set
  289. * pre-relocation to avoid setting them twice (when
  290. * the device is probed again post-relocation). This
  291. * may incur a performance cost as device tree
  292. * properties must be parsed for a second time.
  293. * However, when not using SPL, pre-relocation may be
  294. * the only time we can set defaults for some clocks
  295. * (such as those used for the RAM we will relocate
  296. * into).
  297. */
  298. enum clk_defaults_stage {
  299. CLK_DEFAULTS_PRE = 0,
  300. CLK_DEFAULTS_POST = 1,
  301. CLK_DEFAULTS_POST_FORCE,
  302. };
  303. #if CONFIG_IS_ENABLED(OF_REAL) && CONFIG_IS_ENABLED(CLK)
  304. /**
  305. * clk_set_defaults - Process 'assigned-{clocks/clock-parents/clock-rates}'
  306. * properties to configure clocks
  307. *
  308. * @dev: A device to process (the ofnode associated with this device
  309. * will be processed).
  310. * @stage: The stage of the probing process this function is called during.
  311. */
  312. int clk_set_defaults(struct udevice *dev, enum clk_defaults_stage stage);
  313. #else
  314. static inline int clk_set_defaults(struct udevice *dev, int stage)
  315. {
  316. return 0;
  317. }
  318. #endif
  319. /**
  320. * clk_release_bulk() - Disable (turn off)/Free an array of previously
  321. * requested clocks in a clock bulk struct.
  322. *
  323. * For each clock contained in the clock bulk struct, this function will check
  324. * if clock has been previously requested and then will disable and free it.
  325. *
  326. * @clk: A clock bulk struct that was previously successfully
  327. * requested by clk_get_bulk().
  328. * Return: zero on success, or -ve error code.
  329. */
  330. static inline int clk_release_bulk(struct clk_bulk *bulk)
  331. {
  332. return clk_release_all(bulk->clks, bulk->count);
  333. }
  334. #if CONFIG_IS_ENABLED(CLK)
  335. /**
  336. * clk_request - Request a clock by provider-specific ID.
  337. *
  338. * This requests a clock using a provider-specific ID. Generally, this function
  339. * should not be used, since clk_get_by_index/name() provide an interface that
  340. * better separates clients from intimate knowledge of clock providers.
  341. * However, this function may be useful in core SoC-specific code.
  342. *
  343. * @dev: The clock provider device.
  344. * @clock: A pointer to a clock struct to initialize. The caller must
  345. * have already initialized any field in this struct which the
  346. * clock provider uses to identify the clock.
  347. * Return: 0 if OK, or a negative error code.
  348. */
  349. int clk_request(struct udevice *dev, struct clk *clk);
  350. /**
  351. * clk_free - Free a previously requested clock.
  352. *
  353. * @clock: A clock struct that was previously successfully requested by
  354. * clk_request/get_by_*().
  355. * Return: 0 if OK, or a negative error code.
  356. */
  357. int clk_free(struct clk *clk);
  358. /**
  359. * clk_get_rate() - Get current clock rate.
  360. *
  361. * @clk: A clock struct that was previously successfully requested by
  362. * clk_request/get_by_*().
  363. * Return: clock rate in Hz on success, 0 for invalid clock, or -ve error code
  364. * for other errors.
  365. */
  366. ulong clk_get_rate(struct clk *clk);
  367. /**
  368. * clk_get_parent() - Get current clock's parent.
  369. *
  370. * @clk: A clock struct that was previously successfully requested by
  371. * clk_request/get_by_*().
  372. * Return: pointer to parent's struct clk, or error code passed as pointer
  373. */
  374. struct clk *clk_get_parent(struct clk *clk);
  375. /**
  376. * clk_get_parent_rate() - Get parent of current clock rate.
  377. *
  378. * @clk: A clock struct that was previously successfully requested by
  379. * clk_request/get_by_*().
  380. * Return: clock rate in Hz, or -ve error code.
  381. */
  382. long long clk_get_parent_rate(struct clk *clk);
  383. /**
  384. * clk_round_rate() - Adjust a rate to the exact rate a clock can provide
  385. *
  386. * This answers the question "if I were to pass @rate to clk_set_rate(),
  387. * what clock rate would I end up with?" without changing the hardware
  388. * in any way. In other words:
  389. *
  390. * rate = clk_round_rate(clk, r);
  391. *
  392. * and:
  393. *
  394. * rate = clk_set_rate(clk, r);
  395. *
  396. * are equivalent except the former does not modify the clock hardware
  397. * in any way.
  398. *
  399. * @clk: A clock struct that was previously successfully requested by
  400. * clk_request/get_by_*().
  401. * @rate: desired clock rate in Hz.
  402. * Return: rounded rate in Hz, or -ve error code.
  403. */
  404. ulong clk_round_rate(struct clk *clk, ulong rate);
  405. /**
  406. * clk_set_rate() - Set current clock rate.
  407. *
  408. * @clk: A clock struct that was previously successfully requested by
  409. * clk_request/get_by_*().
  410. * @rate: New clock rate in Hz.
  411. * Return: new rate, or -ve error code.
  412. */
  413. ulong clk_set_rate(struct clk *clk, ulong rate);
  414. /**
  415. * clk_set_parent() - Set current clock parent.
  416. *
  417. * @clk: A clock struct that was previously successfully requested by
  418. * clk_request/get_by_*().
  419. * @parent: A clock struct that was previously successfully requested by
  420. * clk_request/get_by_*().
  421. * Return: new rate, or -ve error code.
  422. */
  423. int clk_set_parent(struct clk *clk, struct clk *parent);
  424. /**
  425. * clk_enable() - Enable (turn on) a clock.
  426. *
  427. * @clk: A clock struct that was previously successfully requested by
  428. * clk_request/get_by_*().
  429. * Return: zero on success, or -ve error code.
  430. */
  431. int clk_enable(struct clk *clk);
  432. /**
  433. * clk_enable_bulk() - Enable (turn on) all clocks in a clock bulk struct.
  434. *
  435. * @bulk: A clock bulk struct that was previously successfully requested
  436. * by clk_get_bulk().
  437. * Return: zero on success, or -ve error code.
  438. */
  439. int clk_enable_bulk(struct clk_bulk *bulk);
  440. /**
  441. * clk_disable() - Disable (turn off) a clock.
  442. *
  443. * @clk: A clock struct that was previously successfully requested by
  444. * clk_request/get_by_*().
  445. * Return: zero on success, or -ve error code.
  446. */
  447. int clk_disable(struct clk *clk);
  448. /**
  449. * clk_disable_bulk() - Disable (turn off) all clocks in a clock bulk struct.
  450. *
  451. * @bulk: A clock bulk struct that was previously successfully requested
  452. * by clk_get_bulk().
  453. * Return: zero on success, or -ve error code.
  454. */
  455. int clk_disable_bulk(struct clk_bulk *bulk);
  456. /**
  457. * clk_is_match - check if two clk's point to the same hardware clock
  458. * @p: clk compared against q
  459. * @q: clk compared against p
  460. *
  461. * Returns true if the two struct clk pointers both point to the same hardware
  462. * clock node.
  463. *
  464. * Returns false otherwise. Note that two NULL clks are treated as matching.
  465. */
  466. bool clk_is_match(const struct clk *p, const struct clk *q);
  467. /**
  468. * clk_get_by_id() - Get the clock by its ID
  469. *
  470. * @id: The clock ID to search for
  471. *
  472. * @clkp: A pointer to clock struct that has been found among added clocks
  473. * to UCLASS_CLK
  474. * Return: zero on success, or -ENOENT on error
  475. */
  476. int clk_get_by_id(ulong id, struct clk **clkp);
  477. /**
  478. * clk_dev_binded() - Check whether the clk has a device binded
  479. *
  480. * @clk A pointer to the clk
  481. *
  482. * Return: true on binded, or false on no
  483. */
  484. bool clk_dev_binded(struct clk *clk);
  485. #else /* CONFIG_IS_ENABLED(CLK) */
  486. static inline int clk_request(struct udevice *dev, struct clk *clk)
  487. {
  488. return -ENOSYS;
  489. }
  490. static inline int clk_free(struct clk *clk)
  491. {
  492. return 0;
  493. }
  494. static inline ulong clk_get_rate(struct clk *clk)
  495. {
  496. return -ENOSYS;
  497. }
  498. static inline struct clk *clk_get_parent(struct clk *clk)
  499. {
  500. return ERR_PTR(-ENOSYS);
  501. }
  502. static inline long long clk_get_parent_rate(struct clk *clk)
  503. {
  504. return -ENOSYS;
  505. }
  506. static inline ulong clk_round_rate(struct clk *clk, ulong rate)
  507. {
  508. return -ENOSYS;
  509. }
  510. static inline ulong clk_set_rate(struct clk *clk, ulong rate)
  511. {
  512. return -ENOSYS;
  513. }
  514. static inline int clk_set_parent(struct clk *clk, struct clk *parent)
  515. {
  516. return -ENOSYS;
  517. }
  518. static inline int clk_enable(struct clk *clk)
  519. {
  520. return 0;
  521. }
  522. static inline int clk_enable_bulk(struct clk_bulk *bulk)
  523. {
  524. return 0;
  525. }
  526. static inline int clk_disable(struct clk *clk)
  527. {
  528. return 0;
  529. }
  530. static inline int clk_disable_bulk(struct clk_bulk *bulk)
  531. {
  532. return 0;
  533. }
  534. static inline bool clk_is_match(const struct clk *p, const struct clk *q)
  535. {
  536. return false;
  537. }
  538. static inline int clk_get_by_id(ulong id, struct clk **clkp)
  539. {
  540. return -ENOSYS;
  541. }
  542. static inline bool clk_dev_binded(struct clk *clk)
  543. {
  544. return false;
  545. }
  546. #endif /* CONFIG_IS_ENABLED(CLK) */
  547. /**
  548. * clk_valid() - check if clk is valid
  549. *
  550. * @clk: the clock to check
  551. * Return: true if valid, or false
  552. */
  553. static inline bool clk_valid(struct clk *clk)
  554. {
  555. return clk && !!clk->dev;
  556. }
  557. int soc_clk_dump(void);
  558. #endif
  559. #define clk_prepare_enable(clk) clk_enable(clk)
  560. #define clk_disable_unprepare(clk) clk_disable(clk)