clk.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. int clk_get_by_driver_info(struct udevice *dev,
  86. struct phandle_1_arg *cells, struct clk *clk);
  87. /**
  88. * clk_get_by_index - Get/request a clock by integer index.
  89. *
  90. * This looks up and requests a clock. The index is relative to the client
  91. * device; each device is assumed to have n clocks associated with it somehow,
  92. * and this function finds and requests one of them. The mapping of client
  93. * device clock indices to provider clocks may be via device-tree properties,
  94. * board-provided mapping tables, or some other mechanism.
  95. *
  96. * @dev: The client device.
  97. * @index: The index of the clock to request, within the client's list of
  98. * clocks.
  99. * @clock A pointer to a clock struct to initialize.
  100. * @return 0 if OK, or a negative error code.
  101. */
  102. int clk_get_by_index(struct udevice *dev, int index, struct clk *clk);
  103. /**
  104. * clk_get_by_index_nodev - Get/request a clock by integer index
  105. * without a device.
  106. *
  107. * This is a version of clk_get_by_index() that does not use a device.
  108. *
  109. * @node: The client ofnode.
  110. * @index: The index of the clock to request, within the client's list of
  111. * clocks.
  112. * @clock A pointer to a clock struct to initialize.
  113. * @return 0 if OK, or a negative error code.
  114. */
  115. int clk_get_by_index_nodev(ofnode node, int index, struct clk *clk);
  116. /**
  117. * clk_get_bulk - Get/request all clocks of a device.
  118. *
  119. * This looks up and requests all clocks of the client device; each device is
  120. * assumed to have n clocks associated with it somehow, and this function finds
  121. * and requests all of them in a separate structure. The mapping of client
  122. * device clock indices to provider clocks may be via device-tree properties,
  123. * board-provided mapping tables, or some other mechanism.
  124. *
  125. * @dev: The client device.
  126. * @bulk A pointer to a clock bulk struct to initialize.
  127. * @return 0 if OK, or a negative error code.
  128. */
  129. int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk);
  130. /**
  131. * clk_get_by_name - Get/request a clock by name.
  132. *
  133. * This looks up and requests a clock. The name is relative to the client
  134. * device; each device is assumed to have n clocks associated with it somehow,
  135. * and this function finds and requests one of them. The mapping of client
  136. * device clock names to provider clocks may be via device-tree properties,
  137. * board-provided mapping tables, or some other mechanism.
  138. *
  139. * @dev: The client device.
  140. * @name: The name of the clock to request, within the client's list of
  141. * clocks.
  142. * @clock: A pointer to a clock struct to initialize.
  143. * @return 0 if OK, or a negative error code.
  144. */
  145. int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk);
  146. /**
  147. * clk_get_by_name_nodev - Get/request a clock by name without a device.
  148. *
  149. * This is a version of clk_get_by_name() that does not use a device.
  150. *
  151. * @node: The client ofnode.
  152. * @name: The name of the clock to request, within the client's list of
  153. * clocks.
  154. * @clock: A pointer to a clock struct to initialize.
  155. * @return 0 if OK, or a negative error code.
  156. */
  157. int clk_get_by_name_nodev(ofnode node, const char *name, struct clk *clk);
  158. /**
  159. * clk_get_optional_nodev - Get/request an optinonal clock by name
  160. * without a device.
  161. * @node: The client ofnode.
  162. * @name: The name of the clock to request.
  163. * @name: The name of the clock to request, within the client's list of
  164. * clocks.
  165. * @clock: A pointer to a clock struct to initialize.
  166. *
  167. * Behaves the same as clk_get_by_name_nodev() except where there is
  168. * no clock producer, in this case, skip the error number -ENODATA, and
  169. * the function returns 0.
  170. */
  171. int clk_get_optional_nodev(ofnode node, const char *name, struct clk *clk);
  172. /**
  173. * devm_clk_get - lookup and obtain a managed reference to a clock producer.
  174. * @dev: device for clock "consumer"
  175. * @id: clock consumer ID
  176. *
  177. * Returns a struct clk corresponding to the clock producer, or
  178. * valid IS_ERR() condition containing errno. The implementation
  179. * uses @dev and @id to determine the clock consumer, and thereby
  180. * the clock producer. (IOW, @id may be identical strings, but
  181. * clk_get may return different clock producers depending on @dev.)
  182. *
  183. * Drivers must assume that the clock source is not enabled.
  184. *
  185. * devm_clk_get should not be called from within interrupt context.
  186. *
  187. * The clock will automatically be freed when the device is unbound
  188. * from the bus.
  189. */
  190. struct clk *devm_clk_get(struct udevice *dev, const char *id);
  191. /**
  192. * devm_clk_get_optional - lookup and obtain a managed reference to an optional
  193. * clock producer.
  194. * @dev: device for clock "consumer"
  195. * @id: clock consumer ID
  196. *
  197. * Behaves the same as devm_clk_get() except where there is no clock producer.
  198. * In this case, instead of returning -ENOENT, the function returns NULL.
  199. */
  200. struct clk *devm_clk_get_optional(struct udevice *dev, const char *id);
  201. /**
  202. * clk_release_all() - Disable (turn off)/Free an array of previously
  203. * requested clocks.
  204. *
  205. * For each clock contained in the clock array, this function will check if
  206. * clock has been previously requested and then will disable and free it.
  207. *
  208. * @clk: A clock struct array that was previously successfully
  209. * requested by clk_request/get_by_*().
  210. * @count Number of clock contained in the array
  211. * @return zero on success, or -ve error code.
  212. */
  213. int clk_release_all(struct clk *clk, int count);
  214. /**
  215. * devm_clk_put - "free" a managed clock source
  216. * @dev: device used to acquire the clock
  217. * @clk: clock source acquired with devm_clk_get()
  218. *
  219. * Note: drivers must ensure that all clk_enable calls made on this
  220. * clock source are balanced by clk_disable calls prior to calling
  221. * this function.
  222. *
  223. * clk_put should not be called from within interrupt context.
  224. */
  225. void devm_clk_put(struct udevice *dev, struct clk *clk);
  226. #else
  227. static inline int clk_get_by_index(struct udevice *dev, int index,
  228. struct clk *clk)
  229. {
  230. return -ENOSYS;
  231. }
  232. static inline int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk)
  233. {
  234. return -ENOSYS;
  235. }
  236. static inline int clk_get_by_name(struct udevice *dev, const char *name,
  237. struct clk *clk)
  238. {
  239. return -ENOSYS;
  240. }
  241. static inline int
  242. clk_get_by_name_nodev(ofnode node, const char *name, struct clk *clk)
  243. {
  244. return -ENOSYS;
  245. }
  246. static inline int
  247. clk_get_optional_nodev(ofnode node, const char *name, struct clk *clk)
  248. {
  249. return -ENOSYS;
  250. }
  251. static inline int clk_release_all(struct clk *clk, int count)
  252. {
  253. return -ENOSYS;
  254. }
  255. #endif
  256. #if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) && \
  257. CONFIG_IS_ENABLED(CLK)
  258. /**
  259. * clk_set_defaults - Process 'assigned-{clocks/clock-parents/clock-rates}'
  260. * properties to configure clocks
  261. *
  262. * @dev: A device to process (the ofnode associated with this device
  263. * will be processed).
  264. * @stage: A integer. 0 indicates that this is called before the device
  265. * is probed. 1 indicates that this is called just after the
  266. * device has been probed
  267. */
  268. int clk_set_defaults(struct udevice *dev, int stage);
  269. #else
  270. static inline int clk_set_defaults(struct udevice *dev, int stage)
  271. {
  272. return 0;
  273. }
  274. #endif
  275. /**
  276. * clk_release_bulk() - Disable (turn off)/Free an array of previously
  277. * requested clocks in a clock bulk struct.
  278. *
  279. * For each clock contained in the clock bulk struct, this function will check
  280. * if clock has been previously requested and then will disable and free it.
  281. *
  282. * @clk: A clock bulk struct that was previously successfully
  283. * requested by clk_get_bulk().
  284. * @return zero on success, or -ve error code.
  285. */
  286. static inline int clk_release_bulk(struct clk_bulk *bulk)
  287. {
  288. return clk_release_all(bulk->clks, bulk->count);
  289. }
  290. #if CONFIG_IS_ENABLED(CLK)
  291. /**
  292. * clk_request - Request a clock by provider-specific ID.
  293. *
  294. * This requests a clock using a provider-specific ID. Generally, this function
  295. * should not be used, since clk_get_by_index/name() provide an interface that
  296. * better separates clients from intimate knowledge of clock providers.
  297. * However, this function may be useful in core SoC-specific code.
  298. *
  299. * @dev: The clock provider device.
  300. * @clock: A pointer to a clock struct to initialize. The caller must
  301. * have already initialized any field in this struct which the
  302. * clock provider uses to identify the clock.
  303. * @return 0 if OK, or a negative error code.
  304. */
  305. int clk_request(struct udevice *dev, struct clk *clk);
  306. /**
  307. * clk_free - Free a previously requested clock.
  308. *
  309. * @clock: A clock struct that was previously successfully requested by
  310. * clk_request/get_by_*().
  311. * @return 0 if OK, or a negative error code.
  312. */
  313. int clk_free(struct clk *clk);
  314. /**
  315. * clk_get_rate() - Get current clock rate.
  316. *
  317. * @clk: A clock struct that was previously successfully requested by
  318. * clk_request/get_by_*().
  319. * @return clock rate in Hz, or -ve error code.
  320. */
  321. ulong clk_get_rate(struct clk *clk);
  322. /**
  323. * clk_get_parent() - Get current clock's parent.
  324. *
  325. * @clk: A clock struct that was previously successfully requested by
  326. * clk_request/get_by_*().
  327. * @return pointer to parent's struct clk, or error code passed as pointer
  328. */
  329. struct clk *clk_get_parent(struct clk *clk);
  330. /**
  331. * clk_get_parent_rate() - Get parent of current clock rate.
  332. *
  333. * @clk: A clock struct that was previously successfully requested by
  334. * clk_request/get_by_*().
  335. * @return clock rate in Hz, or -ve error code.
  336. */
  337. long long clk_get_parent_rate(struct clk *clk);
  338. /**
  339. * clk_set_rate() - Set current clock rate.
  340. *
  341. * @clk: A clock struct that was previously successfully requested by
  342. * clk_request/get_by_*().
  343. * @rate: New clock rate in Hz.
  344. * @return new rate, or -ve error code.
  345. */
  346. ulong clk_set_rate(struct clk *clk, ulong rate);
  347. /**
  348. * clk_set_parent() - Set current clock parent.
  349. *
  350. * @clk: A clock struct that was previously successfully requested by
  351. * clk_request/get_by_*().
  352. * @parent: A clock struct that was previously successfully requested by
  353. * clk_request/get_by_*().
  354. * @return new rate, or -ve error code.
  355. */
  356. int clk_set_parent(struct clk *clk, struct clk *parent);
  357. /**
  358. * clk_enable() - Enable (turn on) a clock.
  359. *
  360. * @clk: A clock struct that was previously successfully requested by
  361. * clk_request/get_by_*().
  362. * @return zero on success, or -ve error code.
  363. */
  364. int clk_enable(struct clk *clk);
  365. /**
  366. * clk_enable_bulk() - Enable (turn on) all clocks in a clock bulk struct.
  367. *
  368. * @bulk: A clock bulk struct that was previously successfully requested
  369. * by clk_get_bulk().
  370. * @return zero on success, or -ve error code.
  371. */
  372. int clk_enable_bulk(struct clk_bulk *bulk);
  373. /**
  374. * clk_disable() - Disable (turn off) a clock.
  375. *
  376. * @clk: A clock struct that was previously successfully requested by
  377. * clk_request/get_by_*().
  378. * @return zero on success, or -ve error code.
  379. */
  380. int clk_disable(struct clk *clk);
  381. /**
  382. * clk_disable_bulk() - Disable (turn off) all clocks in a clock bulk struct.
  383. *
  384. * @bulk: A clock bulk struct that was previously successfully requested
  385. * by clk_get_bulk().
  386. * @return zero on success, or -ve error code.
  387. */
  388. int clk_disable_bulk(struct clk_bulk *bulk);
  389. /**
  390. * clk_is_match - check if two clk's point to the same hardware clock
  391. * @p: clk compared against q
  392. * @q: clk compared against p
  393. *
  394. * Returns true if the two struct clk pointers both point to the same hardware
  395. * clock node.
  396. *
  397. * Returns false otherwise. Note that two NULL clks are treated as matching.
  398. */
  399. bool clk_is_match(const struct clk *p, const struct clk *q);
  400. /**
  401. * clk_get_by_id() - Get the clock by its ID
  402. *
  403. * @id: The clock ID to search for
  404. *
  405. * @clkp: A pointer to clock struct that has been found among added clocks
  406. * to UCLASS_CLK
  407. * @return zero on success, or -ENOENT on error
  408. */
  409. int clk_get_by_id(ulong id, struct clk **clkp);
  410. /**
  411. * clk_dev_binded() - Check whether the clk has a device binded
  412. *
  413. * @clk A pointer to the clk
  414. *
  415. * @return true on binded, or false on no
  416. */
  417. bool clk_dev_binded(struct clk *clk);
  418. #else /* CONFIG_IS_ENABLED(CLK) */
  419. static inline int clk_request(struct udevice *dev, struct clk *clk)
  420. {
  421. return -ENOSYS;
  422. }
  423. static inline int clk_free(struct clk *clk)
  424. {
  425. return 0;
  426. }
  427. static inline ulong clk_get_rate(struct clk *clk)
  428. {
  429. return -ENOSYS;
  430. }
  431. static inline struct clk *clk_get_parent(struct clk *clk)
  432. {
  433. return ERR_PTR(-ENOSYS);
  434. }
  435. static inline long long clk_get_parent_rate(struct clk *clk)
  436. {
  437. return -ENOSYS;
  438. }
  439. static inline ulong clk_set_rate(struct clk *clk, ulong rate)
  440. {
  441. return -ENOSYS;
  442. }
  443. static inline int clk_set_parent(struct clk *clk, struct clk *parent)
  444. {
  445. return -ENOSYS;
  446. }
  447. static inline int clk_enable(struct clk *clk)
  448. {
  449. return 0;
  450. }
  451. static inline int clk_enable_bulk(struct clk_bulk *bulk)
  452. {
  453. return 0;
  454. }
  455. static inline int clk_disable(struct clk *clk)
  456. {
  457. return 0;
  458. }
  459. static inline int clk_disable_bulk(struct clk_bulk *bulk)
  460. {
  461. return 0;
  462. }
  463. static inline bool clk_is_match(const struct clk *p, const struct clk *q)
  464. {
  465. return false;
  466. }
  467. static inline int clk_get_by_id(ulong id, struct clk **clkp)
  468. {
  469. return -ENOSYS;
  470. }
  471. static inline bool clk_dev_binded(struct clk *clk)
  472. {
  473. return false;
  474. }
  475. #endif /* CONFIG_IS_ENABLED(CLK) */
  476. /**
  477. * clk_valid() - check if clk is valid
  478. *
  479. * @clk: the clock to check
  480. * @return true if valid, or false
  481. */
  482. static inline bool clk_valid(struct clk *clk)
  483. {
  484. return clk && !!clk->dev;
  485. }
  486. int soc_clk_dump(void);
  487. #endif
  488. #define clk_prepare_enable(clk) clk_enable(clk)
  489. #define clk_disable_unprepare(clk) clk_disable(clk)