clk.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 <linux/errno.h>
  10. #include <linux/types.h>
  11. /**
  12. * A clock is a hardware signal that oscillates autonomously at a specific
  13. * frequency and duty cycle. Most hardware modules require one or more clock
  14. * signal to drive their operation. Clock signals are typically generated
  15. * externally to the HW module consuming them, by an entity this API calls a
  16. * clock provider. This API provides a standard means for drivers to enable and
  17. * disable clocks, and to set the rate at which they oscillate.
  18. *
  19. * A driver that implements UCLASS_CLOCK is a clock provider. A provider will
  20. * often implement multiple separate clocks, since the hardware it manages
  21. * often has this capability. clock_uclass.h describes the interface which
  22. * clock providers must implement.
  23. *
  24. * Clock consumers/clients are the HW modules driven by the clock signals. This
  25. * header file describes the API used by drivers for those HW modules.
  26. */
  27. struct udevice;
  28. /**
  29. * struct clk - A handle to (allowing control of) a single clock.
  30. *
  31. * Clients provide storage for clock handles. The content of the structure is
  32. * managed solely by the clock API and clock drivers. A clock struct is
  33. * initialized by "get"ing the clock struct. The clock struct is passed to all
  34. * other clock APIs to identify which clock signal to operate upon.
  35. *
  36. * @dev: The device which implements the clock signal.
  37. * @id: The clock signal ID within the provider.
  38. *
  39. * Currently, the clock API assumes that a single integer ID is enough to
  40. * identify and configure any clock signal for any clock provider. If this
  41. * assumption becomes invalid in the future, the struct could be expanded to
  42. * either (a) add more fields to allow clock providers to store additional
  43. * information, or (b) replace the id field with an opaque pointer, which the
  44. * provider would dynamically allocated during its .of_xlate op, and process
  45. * during is .request op. This may require the addition of an extra op to clean
  46. * up the allocation.
  47. */
  48. struct clk {
  49. struct udevice *dev;
  50. /*
  51. * Written by of_xlate. We assume a single id is enough for now. In the
  52. * future, we might add more fields here.
  53. */
  54. unsigned long id;
  55. };
  56. /**
  57. * struct clk_bulk - A handle to (allowing control of) a bulk of clocks.
  58. *
  59. * Clients provide storage for the clock bulk. The content of the structure is
  60. * managed solely by the clock API. A clock bulk struct is
  61. * initialized by "get"ing the clock bulk struct.
  62. * The clock bulk struct is passed to all other bulk clock APIs to apply
  63. * the API to all the clock in the bulk struct.
  64. *
  65. * @clks: An array of clock handles.
  66. * @count: The number of clock handles in the clks array.
  67. */
  68. struct clk_bulk {
  69. struct clk *clks;
  70. unsigned int count;
  71. };
  72. #if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(CLK)
  73. struct phandle_1_arg;
  74. int clk_get_by_index_platdata(struct udevice *dev, int index,
  75. struct phandle_1_arg *cells, struct clk *clk);
  76. /**
  77. * clock_get_by_index - Get/request a clock by integer index.
  78. *
  79. * This looks up and requests a clock. The index is relative to the client
  80. * device; each device is assumed to have n clocks associated with it somehow,
  81. * and this function finds and requests one of them. The mapping of client
  82. * device clock indices to provider clocks may be via device-tree properties,
  83. * board-provided mapping tables, or some other mechanism.
  84. *
  85. * @dev: The client device.
  86. * @index: The index of the clock to request, within the client's list of
  87. * clocks.
  88. * @clock A pointer to a clock struct to initialize.
  89. * @return 0 if OK, or a negative error code.
  90. */
  91. int clk_get_by_index(struct udevice *dev, int index, struct clk *clk);
  92. /**
  93. * clock_get_bulk - Get/request all clocks of a device.
  94. *
  95. * This looks up and requests all clocks of the client device; each device is
  96. * assumed to have n clocks associated with it somehow, and this function finds
  97. * and requests all of them in a separate structure. The mapping of client
  98. * device clock indices to provider clocks may be via device-tree properties,
  99. * board-provided mapping tables, or some other mechanism.
  100. *
  101. * @dev: The client device.
  102. * @bulk A pointer to a clock bulk struct to initialize.
  103. * @return 0 if OK, or a negative error code.
  104. */
  105. int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk);
  106. /**
  107. * clock_get_by_name - Get/request a clock by name.
  108. *
  109. * This looks up and requests a clock. The name is relative to the client
  110. * device; each device is assumed to have n clocks associated with it somehow,
  111. * and this function finds and requests one of them. The mapping of client
  112. * device clock names to provider clocks may be via device-tree properties,
  113. * board-provided mapping tables, or some other mechanism.
  114. *
  115. * @dev: The client device.
  116. * @name: The name of the clock to request, within the client's list of
  117. * clocks.
  118. * @clock: A pointer to a clock struct to initialize.
  119. * @return 0 if OK, or a negative error code.
  120. */
  121. int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk);
  122. /**
  123. * clk_release_all() - Disable (turn off)/Free an array of previously
  124. * requested clocks.
  125. *
  126. * For each clock contained in the clock array, this function will check if
  127. * clock has been previously requested and then will disable and free it.
  128. *
  129. * @clk: A clock struct array that was previously successfully
  130. * requested by clk_request/get_by_*().
  131. * @count Number of clock contained in the array
  132. * @return zero on success, or -ve error code.
  133. */
  134. int clk_release_all(struct clk *clk, int count);
  135. #else
  136. static inline int clk_get_by_index(struct udevice *dev, int index,
  137. struct clk *clk)
  138. {
  139. return -ENOSYS;
  140. }
  141. static inline int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk)
  142. {
  143. return -ENOSYS;
  144. }
  145. static inline int clk_get_by_name(struct udevice *dev, const char *name,
  146. struct clk *clk)
  147. {
  148. return -ENOSYS;
  149. }
  150. static inline int clk_release_all(struct clk *clk, int count)
  151. {
  152. return -ENOSYS;
  153. }
  154. #endif
  155. #if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) && \
  156. CONFIG_IS_ENABLED(CLK)
  157. /**
  158. * clk_set_defaults - Process 'assigned-{clocks/clock-parents/clock-rates}'
  159. * properties to configure clocks
  160. *
  161. * @dev: A device to process (the ofnode associated with this device
  162. * will be processed).
  163. */
  164. int clk_set_defaults(struct udevice *dev);
  165. #else
  166. static inline int clk_set_defaults(struct udevice *dev)
  167. {
  168. return 0;
  169. }
  170. #endif
  171. /**
  172. * clk_release_bulk() - Disable (turn off)/Free an array of previously
  173. * requested clocks in a clock bulk struct.
  174. *
  175. * For each clock contained in the clock bulk struct, this function will check
  176. * if clock has been previously requested and then will disable and free it.
  177. *
  178. * @clk: A clock bulk struct that was previously successfully
  179. * requested by clk_get_bulk().
  180. * @return zero on success, or -ve error code.
  181. */
  182. static inline int clk_release_bulk(struct clk_bulk *bulk)
  183. {
  184. return clk_release_all(bulk->clks, bulk->count);
  185. }
  186. /**
  187. * clk_request - Request a clock by provider-specific ID.
  188. *
  189. * This requests a clock using a provider-specific ID. Generally, this function
  190. * should not be used, since clk_get_by_index/name() provide an interface that
  191. * better separates clients from intimate knowledge of clock providers.
  192. * However, this function may be useful in core SoC-specific code.
  193. *
  194. * @dev: The clock provider device.
  195. * @clock: A pointer to a clock struct to initialize. The caller must
  196. * have already initialized any field in this struct which the
  197. * clock provider uses to identify the clock.
  198. * @return 0 if OK, or a negative error code.
  199. */
  200. int clk_request(struct udevice *dev, struct clk *clk);
  201. /**
  202. * clock_free - Free a previously requested clock.
  203. *
  204. * @clock: A clock struct that was previously successfully requested by
  205. * clk_request/get_by_*().
  206. * @return 0 if OK, or a negative error code.
  207. */
  208. int clk_free(struct clk *clk);
  209. /**
  210. * clk_get_rate() - Get current clock rate.
  211. *
  212. * @clk: A clock struct that was previously successfully requested by
  213. * clk_request/get_by_*().
  214. * @return clock rate in Hz, or -ve error code.
  215. */
  216. ulong clk_get_rate(struct clk *clk);
  217. /**
  218. * clk_set_rate() - Set current clock rate.
  219. *
  220. * @clk: A clock struct that was previously successfully requested by
  221. * clk_request/get_by_*().
  222. * @rate: New clock rate in Hz.
  223. * @return new rate, or -ve error code.
  224. */
  225. ulong clk_set_rate(struct clk *clk, ulong rate);
  226. /**
  227. * clk_set_parent() - Set current clock parent.
  228. *
  229. * @clk: A clock struct that was previously successfully requested by
  230. * clk_request/get_by_*().
  231. * @parent: A clock struct that was previously successfully requested by
  232. * clk_request/get_by_*().
  233. * @return new rate, or -ve error code.
  234. */
  235. int clk_set_parent(struct clk *clk, struct clk *parent);
  236. /**
  237. * clk_enable() - Enable (turn on) a clock.
  238. *
  239. * @clk: A clock struct that was previously successfully requested by
  240. * clk_request/get_by_*().
  241. * @return zero on success, or -ve error code.
  242. */
  243. int clk_enable(struct clk *clk);
  244. /**
  245. * clk_enable_bulk() - Enable (turn on) all clocks in a clock bulk struct.
  246. *
  247. * @bulk: A clock bulk struct that was previously successfully requested
  248. * by clk_get_bulk().
  249. * @return zero on success, or -ve error code.
  250. */
  251. int clk_enable_bulk(struct clk_bulk *bulk);
  252. /**
  253. * clk_disable() - Disable (turn off) a clock.
  254. *
  255. * @clk: A clock struct that was previously successfully requested by
  256. * clk_request/get_by_*().
  257. * @return zero on success, or -ve error code.
  258. */
  259. int clk_disable(struct clk *clk);
  260. /**
  261. * clk_disable_bulk() - Disable (turn off) all clocks in a clock bulk struct.
  262. *
  263. * @bulk: A clock bulk struct that was previously successfully requested
  264. * by clk_get_bulk().
  265. * @return zero on success, or -ve error code.
  266. */
  267. int clk_disable_bulk(struct clk_bulk *bulk);
  268. int soc_clk_dump(void);
  269. #endif