clk.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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/errno.h>
  11. #include <linux/types.h>
  12. /**
  13. * A clock is a hardware signal that oscillates autonomously at a specific
  14. * frequency and duty cycle. Most hardware modules require one or more clock
  15. * signal to drive their operation. Clock signals are typically generated
  16. * externally to the HW module consuming them, by an entity this API calls a
  17. * clock provider. This API provides a standard means for drivers to enable and
  18. * disable clocks, and to set the rate at which they oscillate.
  19. *
  20. * A driver that implements UCLASS_CLK is a clock provider. A provider will
  21. * often implement multiple separate clocks, since the hardware it manages
  22. * often has this capability. clk-uclass.h describes the interface which
  23. * clock providers must implement.
  24. *
  25. * Clock consumers/clients are the HW modules driven by the clock signals. This
  26. * header file describes the API used by drivers for those HW modules.
  27. */
  28. struct udevice;
  29. /**
  30. * struct clk - A handle to (allowing control of) a single clock.
  31. *
  32. * Clients provide storage for clock handles. The content of the structure is
  33. * managed solely by the clock API and clock drivers. A clock struct is
  34. * initialized by "get"ing the clock struct. The clock struct is passed to all
  35. * other clock APIs to identify which clock signal to operate upon.
  36. *
  37. * @dev: The device which implements the clock signal.
  38. * @rate: The clock rate (in HZ).
  39. * @flags: Flags used across common clock structure (e.g. CLK_)
  40. * Clock IP blocks specific flags (i.e. mux, div, gate, etc) are defined
  41. * in struct's for those devices (e.g. struct clk_mux).
  42. * @id: The clock signal ID within the provider.
  43. * @data: An optional data field for scenarios where a single integer ID is not
  44. * sufficient. If used, it can be populated through an .of_xlate op and
  45. * processed during the various clock ops.
  46. *
  47. * Should additional information to identify and configure any clock signal
  48. * for any provider be required in the future, the struct could be expanded to
  49. * either (a) add more fields to allow clock providers to store additional
  50. * information, or (b) replace the id field with an opaque pointer, which the
  51. * provider would dynamically allocated during its .of_xlate op, and process
  52. * during is .request op. This may require the addition of an extra op to clean
  53. * up the allocation.
  54. */
  55. struct clk {
  56. struct udevice *dev;
  57. long long rate; /* in HZ */
  58. u32 flags;
  59. int enable_count;
  60. /*
  61. * Written by of_xlate. In the future, we might add more fields here.
  62. */
  63. unsigned long id;
  64. unsigned long data;
  65. };
  66. /**
  67. * struct clk_bulk - A handle to (allowing control of) a bulk of clocks.
  68. *
  69. * Clients provide storage for the clock bulk. The content of the structure is
  70. * managed solely by the clock API. A clock bulk struct is
  71. * initialized by "get"ing the clock bulk struct.
  72. * The clock bulk struct is passed to all other bulk clock APIs to apply
  73. * the API to all the clock in the bulk struct.
  74. *
  75. * @clks: An array of clock handles.
  76. * @count: The number of clock handles in the clks array.
  77. */
  78. struct clk_bulk {
  79. struct clk *clks;
  80. unsigned int count;
  81. };
  82. #if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(CLK)
  83. struct phandle_1_arg;
  84. int clk_get_by_index_platdata(struct udevice *dev, int index,
  85. struct phandle_1_arg *cells, struct clk *clk);
  86. /**
  87. * clock_get_by_index - Get/request a clock by integer index.
  88. *
  89. * This looks up and requests a clock. The index is relative to the client
  90. * device; each device is assumed to have n clocks associated with it somehow,
  91. * and this function finds and requests one of them. The mapping of client
  92. * device clock indices to provider clocks may be via device-tree properties,
  93. * board-provided mapping tables, or some other mechanism.
  94. *
  95. * @dev: The client device.
  96. * @index: The index of the clock to request, within the client's list of
  97. * clocks.
  98. * @clock A pointer to a clock struct to initialize.
  99. * @return 0 if OK, or a negative error code.
  100. */
  101. int clk_get_by_index(struct udevice *dev, int index, struct clk *clk);
  102. /**
  103. * clock_get_by_index_nodev - Get/request a clock by integer index
  104. * without a device.
  105. *
  106. * This is a version of clk_get_by_index() that does not use a device.
  107. *
  108. * @node: The client ofnode.
  109. * @index: The index of the clock to request, within the client's list of
  110. * clocks.
  111. * @clock A pointer to a clock struct to initialize.
  112. * @return 0 if OK, or a negative error code.
  113. */
  114. int clk_get_by_index_nodev(ofnode node, int index, struct clk *clk);
  115. /**
  116. * clock_get_bulk - Get/request all clocks of a device.
  117. *
  118. * This looks up and requests all clocks of the client device; each device is
  119. * assumed to have n clocks associated with it somehow, and this function finds
  120. * and requests all of them in a separate structure. The mapping of client
  121. * device clock indices to provider clocks may be via device-tree properties,
  122. * board-provided mapping tables, or some other mechanism.
  123. *
  124. * @dev: The client device.
  125. * @bulk A pointer to a clock bulk struct to initialize.
  126. * @return 0 if OK, or a negative error code.
  127. */
  128. int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk);
  129. /**
  130. * clock_get_by_name - Get/request a clock by name.
  131. *
  132. * This looks up and requests a clock. The name is relative to the client
  133. * device; each device is assumed to have n clocks associated with it somehow,
  134. * and this function finds and requests one of them. The mapping of client
  135. * device clock names to provider clocks may be via device-tree properties,
  136. * board-provided mapping tables, or some other mechanism.
  137. *
  138. * @dev: The client device.
  139. * @name: The name of the clock to request, within the client's list of
  140. * clocks.
  141. * @clock: A pointer to a clock struct to initialize.
  142. * @return 0 if OK, or a negative error code.
  143. */
  144. int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk);
  145. /**
  146. * devm_clk_get - lookup and obtain a managed reference to a clock producer.
  147. * @dev: device for clock "consumer"
  148. * @id: clock consumer ID
  149. *
  150. * Returns a struct clk corresponding to the clock producer, or
  151. * valid IS_ERR() condition containing errno. The implementation
  152. * uses @dev and @id to determine the clock consumer, and thereby
  153. * the clock producer. (IOW, @id may be identical strings, but
  154. * clk_get may return different clock producers depending on @dev.)
  155. *
  156. * Drivers must assume that the clock source is not enabled.
  157. *
  158. * devm_clk_get should not be called from within interrupt context.
  159. *
  160. * The clock will automatically be freed when the device is unbound
  161. * from the bus.
  162. */
  163. struct clk *devm_clk_get(struct udevice *dev, const char *id);
  164. /**
  165. * devm_clk_get_optional - lookup and obtain a managed reference to an optional
  166. * clock producer.
  167. * @dev: device for clock "consumer"
  168. * @id: clock consumer ID
  169. *
  170. * Behaves the same as devm_clk_get() except where there is no clock producer.
  171. * In this case, instead of returning -ENOENT, the function returns NULL.
  172. */
  173. struct clk *devm_clk_get_optional(struct udevice *dev, const char *id);
  174. /**
  175. * clk_release_all() - Disable (turn off)/Free an array of previously
  176. * requested clocks.
  177. *
  178. * For each clock contained in the clock array, this function will check if
  179. * clock has been previously requested and then will disable and free it.
  180. *
  181. * @clk: A clock struct array that was previously successfully
  182. * requested by clk_request/get_by_*().
  183. * @count Number of clock contained in the array
  184. * @return zero on success, or -ve error code.
  185. */
  186. int clk_release_all(struct clk *clk, int count);
  187. /**
  188. * devm_clk_put - "free" a managed clock source
  189. * @dev: device used to acquire the clock
  190. * @clk: clock source acquired with devm_clk_get()
  191. *
  192. * Note: drivers must ensure that all clk_enable calls made on this
  193. * clock source are balanced by clk_disable calls prior to calling
  194. * this function.
  195. *
  196. * clk_put should not be called from within interrupt context.
  197. */
  198. void devm_clk_put(struct udevice *dev, struct clk *clk);
  199. #else
  200. static inline int clk_get_by_index(struct udevice *dev, int index,
  201. struct clk *clk)
  202. {
  203. return -ENOSYS;
  204. }
  205. static inline int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk)
  206. {
  207. return -ENOSYS;
  208. }
  209. static inline int clk_get_by_name(struct udevice *dev, const char *name,
  210. struct clk *clk)
  211. {
  212. return -ENOSYS;
  213. }
  214. static inline int clk_release_all(struct clk *clk, int count)
  215. {
  216. return -ENOSYS;
  217. }
  218. #endif
  219. #if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) && \
  220. CONFIG_IS_ENABLED(CLK)
  221. /**
  222. * clk_set_defaults - Process 'assigned-{clocks/clock-parents/clock-rates}'
  223. * properties to configure clocks
  224. *
  225. * @dev: A device to process (the ofnode associated with this device
  226. * will be processed).
  227. * @stage: A integer. 0 indicates that this is called before the device
  228. * is probed. 1 indicates that this is called just after the
  229. * device has been probed
  230. */
  231. int clk_set_defaults(struct udevice *dev, int stage);
  232. #else
  233. static inline int clk_set_defaults(struct udevice *dev, int stage)
  234. {
  235. return 0;
  236. }
  237. #endif
  238. /**
  239. * clk_release_bulk() - Disable (turn off)/Free an array of previously
  240. * requested clocks in a clock bulk struct.
  241. *
  242. * For each clock contained in the clock bulk struct, this function will check
  243. * if clock has been previously requested and then will disable and free it.
  244. *
  245. * @clk: A clock bulk struct that was previously successfully
  246. * requested by clk_get_bulk().
  247. * @return zero on success, or -ve error code.
  248. */
  249. static inline int clk_release_bulk(struct clk_bulk *bulk)
  250. {
  251. return clk_release_all(bulk->clks, bulk->count);
  252. }
  253. /**
  254. * clk_request - Request a clock by provider-specific ID.
  255. *
  256. * This requests a clock using a provider-specific ID. Generally, this function
  257. * should not be used, since clk_get_by_index/name() provide an interface that
  258. * better separates clients from intimate knowledge of clock providers.
  259. * However, this function may be useful in core SoC-specific code.
  260. *
  261. * @dev: The clock provider device.
  262. * @clock: A pointer to a clock struct to initialize. The caller must
  263. * have already initialized any field in this struct which the
  264. * clock provider uses to identify the clock.
  265. * @return 0 if OK, or a negative error code.
  266. */
  267. int clk_request(struct udevice *dev, struct clk *clk);
  268. /**
  269. * clock_free - Free a previously requested clock.
  270. *
  271. * @clock: A clock struct that was previously successfully requested by
  272. * clk_request/get_by_*().
  273. * @return 0 if OK, or a negative error code.
  274. */
  275. int clk_free(struct clk *clk);
  276. /**
  277. * clk_get_rate() - Get current clock rate.
  278. *
  279. * @clk: A clock struct that was previously successfully requested by
  280. * clk_request/get_by_*().
  281. * @return clock rate in Hz, or -ve error code.
  282. */
  283. ulong clk_get_rate(struct clk *clk);
  284. /**
  285. * clk_get_parent() - Get current clock's parent.
  286. *
  287. * @clk: A clock struct that was previously successfully requested by
  288. * clk_request/get_by_*().
  289. * @return pointer to parent's struct clk, or error code passed as pointer
  290. */
  291. struct clk *clk_get_parent(struct clk *clk);
  292. /**
  293. * clk_get_parent_rate() - Get parent of current clock rate.
  294. *
  295. * @clk: A clock struct that was previously successfully requested by
  296. * clk_request/get_by_*().
  297. * @return clock rate in Hz, or -ve error code.
  298. */
  299. long long clk_get_parent_rate(struct clk *clk);
  300. /**
  301. * clk_set_rate() - Set current clock rate.
  302. *
  303. * @clk: A clock struct that was previously successfully requested by
  304. * clk_request/get_by_*().
  305. * @rate: New clock rate in Hz.
  306. * @return new rate, or -ve error code.
  307. */
  308. ulong clk_set_rate(struct clk *clk, ulong rate);
  309. /**
  310. * clk_set_parent() - Set current clock parent.
  311. *
  312. * @clk: A clock struct that was previously successfully requested by
  313. * clk_request/get_by_*().
  314. * @parent: A clock struct that was previously successfully requested by
  315. * clk_request/get_by_*().
  316. * @return new rate, or -ve error code.
  317. */
  318. int clk_set_parent(struct clk *clk, struct clk *parent);
  319. /**
  320. * clk_enable() - Enable (turn on) a clock.
  321. *
  322. * @clk: A clock struct that was previously successfully requested by
  323. * clk_request/get_by_*().
  324. * @return zero on success, or -ve error code.
  325. */
  326. int clk_enable(struct clk *clk);
  327. /**
  328. * clk_enable_bulk() - Enable (turn on) all clocks in a clock bulk struct.
  329. *
  330. * @bulk: A clock bulk struct that was previously successfully requested
  331. * by clk_get_bulk().
  332. * @return zero on success, or -ve error code.
  333. */
  334. int clk_enable_bulk(struct clk_bulk *bulk);
  335. /**
  336. * clk_disable() - Disable (turn off) a clock.
  337. *
  338. * @clk: A clock struct that was previously successfully requested by
  339. * clk_request/get_by_*().
  340. * @return zero on success, or -ve error code.
  341. */
  342. int clk_disable(struct clk *clk);
  343. /**
  344. * clk_disable_bulk() - Disable (turn off) all clocks in a clock bulk struct.
  345. *
  346. * @bulk: A clock bulk struct that was previously successfully requested
  347. * by clk_get_bulk().
  348. * @return zero on success, or -ve error code.
  349. */
  350. int clk_disable_bulk(struct clk_bulk *bulk);
  351. /**
  352. * clk_is_match - check if two clk's point to the same hardware clock
  353. * @p: clk compared against q
  354. * @q: clk compared against p
  355. *
  356. * Returns true if the two struct clk pointers both point to the same hardware
  357. * clock node.
  358. *
  359. * Returns false otherwise. Note that two NULL clks are treated as matching.
  360. */
  361. bool clk_is_match(const struct clk *p, const struct clk *q);
  362. int soc_clk_dump(void);
  363. /**
  364. * clk_valid() - check if clk is valid
  365. *
  366. * @clk: the clock to check
  367. * @return true if valid, or false
  368. */
  369. static inline bool clk_valid(struct clk *clk)
  370. {
  371. return clk && !!clk->dev;
  372. }
  373. /**
  374. * clk_get_by_id() - Get the clock by its ID
  375. *
  376. * @id: The clock ID to search for
  377. *
  378. * @clkp: A pointer to clock struct that has been found among added clocks
  379. * to UCLASS_CLK
  380. * @return zero on success, or -ENOENT on error
  381. */
  382. int clk_get_by_id(ulong id, struct clk **clkp);
  383. /**
  384. * clk_dev_binded() - Check whether the clk has a device binded
  385. *
  386. * @clk A pointer to the clk
  387. *
  388. * @return true on binded, or false on no
  389. */
  390. bool clk_dev_binded(struct clk *clk);
  391. #endif
  392. #define clk_prepare_enable(clk) clk_enable(clk)
  393. #define clk_disable_unprepare(clk) clk_disable(clk)