clk.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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_get_by_name_nodev - Get/request a clock by name without a device.
  147. *
  148. * This is a version of clk_get_by_name() that does not use a device.
  149. *
  150. * @node: The client ofnode.
  151. * @name: The name of the clock to request, within the client's list of
  152. * clocks.
  153. * @clock: A pointer to a clock struct to initialize.
  154. * @return 0 if OK, or a negative error code.
  155. */
  156. int clk_get_by_name_nodev(ofnode node, const char *name, struct clk *clk);
  157. /**
  158. * clock_get_optional_nodev - Get/request an optinonal clock by name
  159. * without a device.
  160. * @node: The client ofnode.
  161. * @name: The name of the clock to request.
  162. * @name: The name of the clock to request, within the client's list of
  163. * clocks.
  164. * @clock: A pointer to a clock struct to initialize.
  165. *
  166. * Behaves the same as clk_get_by_name_nodev() except where there is
  167. * no clock producer, in this case, skip the error number -ENODATA, and
  168. * the function returns 0.
  169. */
  170. int clk_get_optional_nodev(ofnode node, const char *name, struct clk *clk);
  171. /**
  172. * devm_clk_get - lookup and obtain a managed reference to a clock producer.
  173. * @dev: device for clock "consumer"
  174. * @id: clock consumer ID
  175. *
  176. * Returns a struct clk corresponding to the clock producer, or
  177. * valid IS_ERR() condition containing errno. The implementation
  178. * uses @dev and @id to determine the clock consumer, and thereby
  179. * the clock producer. (IOW, @id may be identical strings, but
  180. * clk_get may return different clock producers depending on @dev.)
  181. *
  182. * Drivers must assume that the clock source is not enabled.
  183. *
  184. * devm_clk_get should not be called from within interrupt context.
  185. *
  186. * The clock will automatically be freed when the device is unbound
  187. * from the bus.
  188. */
  189. struct clk *devm_clk_get(struct udevice *dev, const char *id);
  190. /**
  191. * devm_clk_get_optional - lookup and obtain a managed reference to an optional
  192. * clock producer.
  193. * @dev: device for clock "consumer"
  194. * @id: clock consumer ID
  195. *
  196. * Behaves the same as devm_clk_get() except where there is no clock producer.
  197. * In this case, instead of returning -ENOENT, the function returns NULL.
  198. */
  199. struct clk *devm_clk_get_optional(struct udevice *dev, const char *id);
  200. /**
  201. * clk_release_all() - Disable (turn off)/Free an array of previously
  202. * requested clocks.
  203. *
  204. * For each clock contained in the clock array, this function will check if
  205. * clock has been previously requested and then will disable and free it.
  206. *
  207. * @clk: A clock struct array that was previously successfully
  208. * requested by clk_request/get_by_*().
  209. * @count Number of clock contained in the array
  210. * @return zero on success, or -ve error code.
  211. */
  212. int clk_release_all(struct clk *clk, int count);
  213. /**
  214. * devm_clk_put - "free" a managed clock source
  215. * @dev: device used to acquire the clock
  216. * @clk: clock source acquired with devm_clk_get()
  217. *
  218. * Note: drivers must ensure that all clk_enable calls made on this
  219. * clock source are balanced by clk_disable calls prior to calling
  220. * this function.
  221. *
  222. * clk_put should not be called from within interrupt context.
  223. */
  224. void devm_clk_put(struct udevice *dev, struct clk *clk);
  225. #else
  226. static inline int clk_get_by_index(struct udevice *dev, int index,
  227. struct clk *clk)
  228. {
  229. return -ENOSYS;
  230. }
  231. static inline int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk)
  232. {
  233. return -ENOSYS;
  234. }
  235. static inline int clk_get_by_name(struct udevice *dev, const char *name,
  236. struct clk *clk)
  237. {
  238. return -ENOSYS;
  239. }
  240. static inline int
  241. clk_get_by_name_nodev(ofnode node, const char *name, struct clk *clk)
  242. {
  243. return -ENOSYS;
  244. }
  245. static inline int
  246. clk_get_optional_nodev(ofnode node, const char *name, struct clk *clk)
  247. {
  248. return -ENOSYS;
  249. }
  250. static inline int clk_release_all(struct clk *clk, int count)
  251. {
  252. return -ENOSYS;
  253. }
  254. #endif
  255. #if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) && \
  256. CONFIG_IS_ENABLED(CLK)
  257. /**
  258. * clk_set_defaults - Process 'assigned-{clocks/clock-parents/clock-rates}'
  259. * properties to configure clocks
  260. *
  261. * @dev: A device to process (the ofnode associated with this device
  262. * will be processed).
  263. * @stage: A integer. 0 indicates that this is called before the device
  264. * is probed. 1 indicates that this is called just after the
  265. * device has been probed
  266. */
  267. int clk_set_defaults(struct udevice *dev, int stage);
  268. #else
  269. static inline int clk_set_defaults(struct udevice *dev, int stage)
  270. {
  271. return 0;
  272. }
  273. #endif
  274. /**
  275. * clk_release_bulk() - Disable (turn off)/Free an array of previously
  276. * requested clocks in a clock bulk struct.
  277. *
  278. * For each clock contained in the clock bulk struct, this function will check
  279. * if clock has been previously requested and then will disable and free it.
  280. *
  281. * @clk: A clock bulk struct that was previously successfully
  282. * requested by clk_get_bulk().
  283. * @return zero on success, or -ve error code.
  284. */
  285. static inline int clk_release_bulk(struct clk_bulk *bulk)
  286. {
  287. return clk_release_all(bulk->clks, bulk->count);
  288. }
  289. /**
  290. * clk_request - Request a clock by provider-specific ID.
  291. *
  292. * This requests a clock using a provider-specific ID. Generally, this function
  293. * should not be used, since clk_get_by_index/name() provide an interface that
  294. * better separates clients from intimate knowledge of clock providers.
  295. * However, this function may be useful in core SoC-specific code.
  296. *
  297. * @dev: The clock provider device.
  298. * @clock: A pointer to a clock struct to initialize. The caller must
  299. * have already initialized any field in this struct which the
  300. * clock provider uses to identify the clock.
  301. * @return 0 if OK, or a negative error code.
  302. */
  303. int clk_request(struct udevice *dev, struct clk *clk);
  304. /**
  305. * clock_free - Free a previously requested clock.
  306. *
  307. * @clock: A clock struct that was previously successfully requested by
  308. * clk_request/get_by_*().
  309. * @return 0 if OK, or a negative error code.
  310. */
  311. int clk_free(struct clk *clk);
  312. /**
  313. * clk_get_rate() - Get current clock rate.
  314. *
  315. * @clk: A clock struct that was previously successfully requested by
  316. * clk_request/get_by_*().
  317. * @return clock rate in Hz, or -ve error code.
  318. */
  319. ulong clk_get_rate(struct clk *clk);
  320. /**
  321. * clk_get_parent() - Get current clock's parent.
  322. *
  323. * @clk: A clock struct that was previously successfully requested by
  324. * clk_request/get_by_*().
  325. * @return pointer to parent's struct clk, or error code passed as pointer
  326. */
  327. struct clk *clk_get_parent(struct clk *clk);
  328. /**
  329. * clk_get_parent_rate() - Get parent of current clock rate.
  330. *
  331. * @clk: A clock struct that was previously successfully requested by
  332. * clk_request/get_by_*().
  333. * @return clock rate in Hz, or -ve error code.
  334. */
  335. long long clk_get_parent_rate(struct clk *clk);
  336. /**
  337. * clk_set_rate() - Set current clock rate.
  338. *
  339. * @clk: A clock struct that was previously successfully requested by
  340. * clk_request/get_by_*().
  341. * @rate: New clock rate in Hz.
  342. * @return new rate, or -ve error code.
  343. */
  344. ulong clk_set_rate(struct clk *clk, ulong rate);
  345. /**
  346. * clk_set_parent() - Set current clock parent.
  347. *
  348. * @clk: A clock struct that was previously successfully requested by
  349. * clk_request/get_by_*().
  350. * @parent: A clock struct that was previously successfully requested by
  351. * clk_request/get_by_*().
  352. * @return new rate, or -ve error code.
  353. */
  354. int clk_set_parent(struct clk *clk, struct clk *parent);
  355. /**
  356. * clk_enable() - Enable (turn on) a clock.
  357. *
  358. * @clk: A clock struct that was previously successfully requested by
  359. * clk_request/get_by_*().
  360. * @return zero on success, or -ve error code.
  361. */
  362. int clk_enable(struct clk *clk);
  363. /**
  364. * clk_enable_bulk() - Enable (turn on) all clocks in a clock bulk struct.
  365. *
  366. * @bulk: A clock bulk struct that was previously successfully requested
  367. * by clk_get_bulk().
  368. * @return zero on success, or -ve error code.
  369. */
  370. int clk_enable_bulk(struct clk_bulk *bulk);
  371. /**
  372. * clk_disable() - Disable (turn off) a clock.
  373. *
  374. * @clk: A clock struct that was previously successfully requested by
  375. * clk_request/get_by_*().
  376. * @return zero on success, or -ve error code.
  377. */
  378. int clk_disable(struct clk *clk);
  379. /**
  380. * clk_disable_bulk() - Disable (turn off) all clocks in a clock bulk struct.
  381. *
  382. * @bulk: A clock bulk struct that was previously successfully requested
  383. * by clk_get_bulk().
  384. * @return zero on success, or -ve error code.
  385. */
  386. int clk_disable_bulk(struct clk_bulk *bulk);
  387. /**
  388. * clk_is_match - check if two clk's point to the same hardware clock
  389. * @p: clk compared against q
  390. * @q: clk compared against p
  391. *
  392. * Returns true if the two struct clk pointers both point to the same hardware
  393. * clock node.
  394. *
  395. * Returns false otherwise. Note that two NULL clks are treated as matching.
  396. */
  397. bool clk_is_match(const struct clk *p, const struct clk *q);
  398. int soc_clk_dump(void);
  399. /**
  400. * clk_valid() - check if clk is valid
  401. *
  402. * @clk: the clock to check
  403. * @return true if valid, or false
  404. */
  405. static inline bool clk_valid(struct clk *clk)
  406. {
  407. return clk && !!clk->dev;
  408. }
  409. /**
  410. * clk_get_by_id() - Get the clock by its ID
  411. *
  412. * @id: The clock ID to search for
  413. *
  414. * @clkp: A pointer to clock struct that has been found among added clocks
  415. * to UCLASS_CLK
  416. * @return zero on success, or -ENOENT on error
  417. */
  418. int clk_get_by_id(ulong id, struct clk **clkp);
  419. /**
  420. * clk_dev_binded() - Check whether the clk has a device binded
  421. *
  422. * @clk A pointer to the clk
  423. *
  424. * @return true on binded, or false on no
  425. */
  426. bool clk_dev_binded(struct clk *clk);
  427. #endif
  428. #define clk_prepare_enable(clk) clk_enable(clk)
  429. #define clk_disable_unprepare(clk) clk_disable(clk)