clk.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. * clk_release_all() - Disable (turn off)/Free an array of previously
  147. * requested clocks.
  148. *
  149. * For each clock contained in the clock array, this function will check if
  150. * clock has been previously requested and then will disable and free it.
  151. *
  152. * @clk: A clock struct array that was previously successfully
  153. * requested by clk_request/get_by_*().
  154. * @count Number of clock contained in the array
  155. * @return zero on success, or -ve error code.
  156. */
  157. int clk_release_all(struct clk *clk, int count);
  158. #else
  159. static inline int clk_get_by_index(struct udevice *dev, int index,
  160. struct clk *clk)
  161. {
  162. return -ENOSYS;
  163. }
  164. static inline int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk)
  165. {
  166. return -ENOSYS;
  167. }
  168. static inline int clk_get_by_name(struct udevice *dev, const char *name,
  169. struct clk *clk)
  170. {
  171. return -ENOSYS;
  172. }
  173. static inline int clk_release_all(struct clk *clk, int count)
  174. {
  175. return -ENOSYS;
  176. }
  177. #endif
  178. #if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) && \
  179. CONFIG_IS_ENABLED(CLK)
  180. /**
  181. * clk_set_defaults - Process 'assigned-{clocks/clock-parents/clock-rates}'
  182. * properties to configure clocks
  183. *
  184. * @dev: A device to process (the ofnode associated with this device
  185. * will be processed).
  186. */
  187. int clk_set_defaults(struct udevice *dev);
  188. #else
  189. static inline int clk_set_defaults(struct udevice *dev)
  190. {
  191. return 0;
  192. }
  193. #endif
  194. /**
  195. * clk_release_bulk() - Disable (turn off)/Free an array of previously
  196. * requested clocks in a clock bulk struct.
  197. *
  198. * For each clock contained in the clock bulk struct, this function will check
  199. * if clock has been previously requested and then will disable and free it.
  200. *
  201. * @clk: A clock bulk struct that was previously successfully
  202. * requested by clk_get_bulk().
  203. * @return zero on success, or -ve error code.
  204. */
  205. static inline int clk_release_bulk(struct clk_bulk *bulk)
  206. {
  207. return clk_release_all(bulk->clks, bulk->count);
  208. }
  209. /**
  210. * clk_request - Request a clock by provider-specific ID.
  211. *
  212. * This requests a clock using a provider-specific ID. Generally, this function
  213. * should not be used, since clk_get_by_index/name() provide an interface that
  214. * better separates clients from intimate knowledge of clock providers.
  215. * However, this function may be useful in core SoC-specific code.
  216. *
  217. * @dev: The clock provider device.
  218. * @clock: A pointer to a clock struct to initialize. The caller must
  219. * have already initialized any field in this struct which the
  220. * clock provider uses to identify the clock.
  221. * @return 0 if OK, or a negative error code.
  222. */
  223. int clk_request(struct udevice *dev, struct clk *clk);
  224. /**
  225. * clock_free - Free a previously requested clock.
  226. *
  227. * @clock: A clock struct that was previously successfully requested by
  228. * clk_request/get_by_*().
  229. * @return 0 if OK, or a negative error code.
  230. */
  231. int clk_free(struct clk *clk);
  232. /**
  233. * clk_get_rate() - Get current clock rate.
  234. *
  235. * @clk: A clock struct that was previously successfully requested by
  236. * clk_request/get_by_*().
  237. * @return clock rate in Hz, or -ve error code.
  238. */
  239. ulong clk_get_rate(struct clk *clk);
  240. /**
  241. * clk_get_parent() - Get current clock's parent.
  242. *
  243. * @clk: A clock struct that was previously successfully requested by
  244. * clk_request/get_by_*().
  245. * @return pointer to parent's struct clk, or error code passed as pointer
  246. */
  247. struct clk *clk_get_parent(struct clk *clk);
  248. /**
  249. * clk_get_parent_rate() - Get parent of current clock rate.
  250. *
  251. * @clk: A clock struct that was previously successfully requested by
  252. * clk_request/get_by_*().
  253. * @return clock rate in Hz, or -ve error code.
  254. */
  255. long long clk_get_parent_rate(struct clk *clk);
  256. /**
  257. * clk_set_rate() - Set current clock rate.
  258. *
  259. * @clk: A clock struct that was previously successfully requested by
  260. * clk_request/get_by_*().
  261. * @rate: New clock rate in Hz.
  262. * @return new rate, or -ve error code.
  263. */
  264. ulong clk_set_rate(struct clk *clk, ulong rate);
  265. /**
  266. * clk_set_parent() - Set current clock parent.
  267. *
  268. * @clk: A clock struct that was previously successfully requested by
  269. * clk_request/get_by_*().
  270. * @parent: A clock struct that was previously successfully requested by
  271. * clk_request/get_by_*().
  272. * @return new rate, or -ve error code.
  273. */
  274. int clk_set_parent(struct clk *clk, struct clk *parent);
  275. /**
  276. * clk_enable() - Enable (turn on) a clock.
  277. *
  278. * @clk: A clock struct that was previously successfully requested by
  279. * clk_request/get_by_*().
  280. * @return zero on success, or -ve error code.
  281. */
  282. int clk_enable(struct clk *clk);
  283. /**
  284. * clk_enable_bulk() - Enable (turn on) all clocks in a clock bulk struct.
  285. *
  286. * @bulk: A clock bulk struct that was previously successfully requested
  287. * by clk_get_bulk().
  288. * @return zero on success, or -ve error code.
  289. */
  290. int clk_enable_bulk(struct clk_bulk *bulk);
  291. /**
  292. * clk_disable() - Disable (turn off) a clock.
  293. *
  294. * @clk: A clock struct that was previously successfully requested by
  295. * clk_request/get_by_*().
  296. * @return zero on success, or -ve error code.
  297. */
  298. int clk_disable(struct clk *clk);
  299. /**
  300. * clk_disable_bulk() - Disable (turn off) all clocks in a clock bulk struct.
  301. *
  302. * @bulk: A clock bulk struct that was previously successfully requested
  303. * by clk_get_bulk().
  304. * @return zero on success, or -ve error code.
  305. */
  306. int clk_disable_bulk(struct clk_bulk *bulk);
  307. /**
  308. * clk_is_match - check if two clk's point to the same hardware clock
  309. * @p: clk compared against q
  310. * @q: clk compared against p
  311. *
  312. * Returns true if the two struct clk pointers both point to the same hardware
  313. * clock node.
  314. *
  315. * Returns false otherwise. Note that two NULL clks are treated as matching.
  316. */
  317. bool clk_is_match(const struct clk *p, const struct clk *q);
  318. int soc_clk_dump(void);
  319. /**
  320. * clk_valid() - check if clk is valid
  321. *
  322. * @clk: the clock to check
  323. * @return true if valid, or false
  324. */
  325. static inline bool clk_valid(struct clk *clk)
  326. {
  327. return !!clk->dev;
  328. }
  329. /**
  330. * clk_get_by_id() - Get the clock by its ID
  331. *
  332. * @id: The clock ID to search for
  333. *
  334. * @clkp: A pointer to clock struct that has been found among added clocks
  335. * to UCLASS_CLK
  336. * @return zero on success, or -ENOENT on error
  337. */
  338. int clk_get_by_id(ulong id, struct clk **clkp);
  339. /**
  340. * clk_dev_binded() - Check whether the clk has a device binded
  341. *
  342. * @clk A pointer to the clk
  343. *
  344. * @return true on binded, or false on no
  345. */
  346. bool clk_dev_binded(struct clk *clk);
  347. #endif