clk.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright (c) 2006-2025 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-11-26 GuEe-GUI first version
  9. * 2025-01-24 wumingzi add doxygen comment
  10. * 2024-05-01 GuEe-GUI make cell for hareware clock
  11. */
  12. #ifndef __CLK_H__
  13. #define __CLK_H__
  14. #include <rthw.h>
  15. #include <ref.h>
  16. #include <drivers/ofw.h>
  17. /**
  18. * @defgroup group_driver_clock Clock
  19. * @brief Clock driver API
  20. * @ingroup group_device_driver
  21. */
  22. /**
  23. * @addtogroup group_driver_clock
  24. * @{
  25. */
  26. #define RT_CLK_NODE_OBJ_NAME "CLKNP"
  27. struct rt_clk;
  28. struct rt_clk_ops;
  29. struct rt_clk_cell;
  30. /**
  31. * @brief Clock provider node - represents a hardware clock controller.
  32. *
  33. * A @ref rt_clk_node corresponds to one hardware clock provider in the
  34. * system, such as a PLL controller, a clock multiplexer, or a composite
  35. * clock block defined in the device tree.
  36. *
  37. * Each clock node may contain multiple hardware clock outputs, described
  38. * as @ref rt_clk_cell structures, which represent individual leaf clocks.
  39. *
  40. * Members:
  41. * - `parent` — embedded @ref rt_object header for RT-Thread object system.
  42. * - `dev` — back-reference to the hardware device providing this clock domain.
  43. * - `parents_clk` — optional array of parent clock handles.
  44. * - `multi_clk` — number of clock outputs exported by this provider.
  45. * - `cells` — list of @ref rt_clk_cell pointers representing each output.
  46. * - `ofw_parse` — callback used to parse clock arguments from device tree
  47. * (`#clock-cells`) and select the corresponding @ref rt_clk_cell.
  48. * - `priv` — implementation-specific private data.
  49. *
  50. * Typical usage:
  51. * 1. Define a @ref rt_clk_node describing the hardware clock controller.
  52. * 2. Implement `ofw_parse()` to resolve device tree `phandle` arguments.
  53. * 3. Register the node using @ref rt_clk_register().
  54. */
  55. struct rt_clk_node
  56. {
  57. struct rt_object parent;
  58. struct rt_device *dev;
  59. struct rt_clk_array *parents_clk;
  60. rt_size_t multi_clk;
  61. rt_size_t cells_nr;
  62. struct rt_clk_cell **cells;
  63. struct rt_clk_cell *(*ofw_parse)(struct rt_clk_node *clk_np, struct rt_ofw_cell_args *args);
  64. void *priv;
  65. };
  66. #define RT_CLK_F_SET_RATE_GATE RT_BIT(0) /**< Must be gated across rate change */
  67. #define RT_CLK_F_SET_PARENT_GATE RT_BIT(1) /**< Must be gated across re-parent */
  68. #define RT_CLK_F_SET_RATE_PARENT RT_BIT(2) /**< Propagate rate change up one level */
  69. #define RT_CLK_F_IGNORE_UNUSED RT_BIT(3) /**< Do not gate even if unused */
  70. #define RT_CLK_F_SET_RATE_UNGATE RT_BIT(4) /**< Clock needs to run to set rate */
  71. #define RT_CLK_F_IS_CRITICAL RT_BIT(5) /**< Do not gate, ever */
  72. #define RT_CLK_F_GET_RATE_NOCACHE RT_BIT(6) /**< Do not get rate by cache */
  73. /**
  74. * @brief Clock cell - represents a single hardware clock element.
  75. *
  76. * A clk_cell is the fundamental unit of a clock tree, such as a PLL, divider,
  77. * mux, or gate. It maintains its relationship to parent clocks using pointers,
  78. * not lists, for lightweight hierarchy management.
  79. *
  80. * The 'ops' field defines hardware-specific callbacks. The framework invokes
  81. * these during enable, disable, and rate changes.
  82. */
  83. struct rt_clk_cell
  84. {
  85. struct rt_clk_node *clk_np;
  86. const char *name;
  87. const struct rt_clk_ops *ops;
  88. rt_uint8_t parents_nr;
  89. union
  90. {
  91. const char *parent_name; /**< When parents_nr = 1 */
  92. const char *const *parent_names;
  93. };
  94. rt_ubase_t rate; /**< Cached or fixed rate (not always accurate) */
  95. struct rt_clk *clk;
  96. struct rt_clk *parent;
  97. int prepare_count;
  98. int enable_count;
  99. rt_uint32_t flags;
  100. void *priv;
  101. };
  102. /**
  103. * @brief Fixed-rate clock descriptor.
  104. *
  105. * Used for constant-frequency clocks without configurable parents or dividers.
  106. */
  107. struct rt_clk_fixed_rate
  108. {
  109. struct rt_clk_cell cell;
  110. rt_ubase_t fixed_rate;
  111. rt_ubase_t fixed_accuracy;
  112. };
  113. /**
  114. * @brief Clock handle - represents a consumer reference to a clock.
  115. *
  116. * Each consumer obtains an rt_clk instance bound to a specific clk_cell.
  117. * The handle stores consumer-specific constraints such as min/max rate.
  118. */
  119. struct rt_clk
  120. {
  121. struct rt_clk_cell *cell;
  122. const char *dev_id; /**< Device identifier using this clock */
  123. const char *con_id; /**< Connection identifier (name) */
  124. rt_ubase_t min_rate;
  125. rt_ubase_t max_rate;
  126. };
  127. /**
  128. * @brief Clock array container.
  129. *
  130. * Represents a group of rt_clk handles, typically used for devices that
  131. * require multiple clock inputs.
  132. */
  133. struct rt_clk_array
  134. {
  135. rt_size_t count;
  136. struct rt_clk *clks[];
  137. };
  138. struct rt_clk_ops
  139. {
  140. rt_err_t (*prepare)(struct rt_clk_cell *cell);
  141. void (*unprepare)(struct rt_clk_cell *cell);
  142. rt_bool_t (*is_prepared)(struct rt_clk_cell *cell);
  143. rt_err_t (*enable)(struct rt_clk_cell *cell);
  144. void (*disable)(struct rt_clk_cell *cell);
  145. rt_bool_t (*is_enabled)(struct rt_clk_cell *cell);
  146. rt_ubase_t (*recalc_rate)(struct rt_clk_cell *cell, rt_ubase_t parent_rate);
  147. rt_base_t (*round_rate)(struct rt_clk_cell *cell, rt_ubase_t drate, rt_ubase_t *prate);
  148. rt_err_t (*set_rate)(struct rt_clk_cell *cell, rt_ubase_t rate, rt_ubase_t parent_rate);
  149. rt_err_t (*set_parent)(struct rt_clk_cell *cell, rt_uint8_t idx);
  150. rt_uint8_t (*get_parent)(struct rt_clk_cell *cell);
  151. rt_err_t (*set_phase)(struct rt_clk_cell *cell, int degrees);
  152. rt_base_t (*get_phase)(struct rt_clk_cell *cell);
  153. };
  154. struct rt_clk_notifier;
  155. #define RT_CLK_MSG_PRE_RATE_CHANGE RT_BIT(0)
  156. #define RT_CLK_MSG_POST_RATE_CHANGE RT_BIT(1)
  157. #define RT_CLK_MSG_ABORT_RATE_CHANGE RT_BIT(2)
  158. typedef rt_err_t (*rt_clk_notifier_callback)(struct rt_clk_notifier *notifier,
  159. rt_ubase_t msg, rt_ubase_t old_rate, rt_ubase_t new_rate);
  160. /**
  161. * @brief Clock notifier descriptor.
  162. *
  163. * Used to register callbacks for clock events (rate change, abort, etc).
  164. * Each notifier is linked to a specific clock and triggered on rate changes.
  165. */
  166. struct rt_clk_notifier
  167. {
  168. rt_list_t list;
  169. struct rt_clk *clk;
  170. rt_clk_notifier_callback callback;
  171. void *priv;
  172. };
  173. rt_err_t rt_clk_register(struct rt_clk_node *clk_np);
  174. rt_err_t rt_clk_unregister(struct rt_clk_node *clk_np);
  175. rt_err_t rt_clk_notifier_register(struct rt_clk *clk, struct rt_clk_notifier *notifier);
  176. rt_err_t rt_clk_notifier_unregister(struct rt_clk *clk, struct rt_clk_notifier *notifier);
  177. rt_err_t rt_clk_prepare(struct rt_clk *clk);
  178. void rt_clk_unprepare(struct rt_clk *clk);
  179. rt_err_t rt_clk_enable(struct rt_clk *clk);
  180. void rt_clk_disable(struct rt_clk *clk);
  181. rt_err_t rt_clk_prepare_enable(struct rt_clk *clk);
  182. void rt_clk_disable_unprepare(struct rt_clk *clk);
  183. rt_err_t rt_clk_array_prepare(struct rt_clk_array *clk_arr);
  184. void rt_clk_array_unprepare(struct rt_clk_array *clk_arr);
  185. rt_err_t rt_clk_array_enable(struct rt_clk_array *clk_arr);
  186. void rt_clk_array_disable(struct rt_clk_array *clk_arr);
  187. rt_err_t rt_clk_array_prepare_enable(struct rt_clk_array *clk_arr);
  188. void rt_clk_array_disable_unprepare(struct rt_clk_array *clk_arr);
  189. rt_err_t rt_clk_set_rate_range(struct rt_clk *clk, rt_ubase_t min, rt_ubase_t max);
  190. rt_err_t rt_clk_set_min_rate(struct rt_clk *clk, rt_ubase_t rate);
  191. rt_err_t rt_clk_set_max_rate(struct rt_clk *clk, rt_ubase_t rate);
  192. rt_err_t rt_clk_set_rate(struct rt_clk *clk, rt_ubase_t rate);
  193. rt_ubase_t rt_clk_get_rate(struct rt_clk *clk);
  194. rt_base_t rt_clk_round_rate(struct rt_clk *clk, rt_ubase_t rate);
  195. rt_err_t rt_clk_set_parent(struct rt_clk *clk, struct rt_clk *clk_parent);
  196. struct rt_clk *rt_clk_get_parent(struct rt_clk *clk);
  197. rt_err_t rt_clk_set_phase(struct rt_clk *clk, int degrees);
  198. rt_base_t rt_clk_get_phase(struct rt_clk *clk);
  199. struct rt_clk *rt_clk_cell_get_clk(const struct rt_clk_cell *cell, const char *con_id);
  200. rt_bool_t rt_clk_cell_is_prepared(const struct rt_clk_cell *cell);
  201. rt_bool_t rt_clk_cell_is_enabled(const struct rt_clk_cell *cell);
  202. rt_ubase_t rt_clk_cell_get_rate(const struct rt_clk_cell *cell);
  203. rt_ubase_t rt_clk_cell_round_rate(struct rt_clk_cell *cell, rt_ubase_t rate);
  204. struct rt_clk_cell *rt_clk_cell_get_parent(const struct rt_clk_cell *cell);
  205. struct rt_clk_cell *rt_clk_cell_get_parent_by_index(const struct rt_clk_cell *cell, rt_uint8_t idx);
  206. rt_uint8_t rt_clk_cell_get_parent_index(struct rt_clk_cell *cell);
  207. rt_err_t rt_clk_cell_set_parent(struct rt_clk_cell *cell, struct rt_clk_cell *parent);
  208. struct rt_clk_array *rt_clk_get_array(struct rt_device *dev);
  209. struct rt_clk *rt_clk_get_by_index(struct rt_device *dev, int index);
  210. struct rt_clk *rt_clk_get_by_name(struct rt_device *dev, const char *name);
  211. void rt_clk_array_put(struct rt_clk_array *clk_arr);
  212. void rt_clk_put(struct rt_clk *clk);
  213. #ifdef RT_USING_OFW
  214. struct rt_clk_array *rt_ofw_get_clk_array(struct rt_ofw_node *np);
  215. struct rt_clk *rt_ofw_get_clk(struct rt_ofw_node *np, int index);
  216. struct rt_clk *rt_ofw_get_clk_by_name(struct rt_ofw_node *np, const char *name);
  217. rt_ssize_t rt_ofw_count_of_clk(struct rt_ofw_node *clk_ofw_np);
  218. const char *rt_ofw_clk_get_parent_name(struct rt_ofw_node *np, int index);
  219. rt_err_t rt_ofw_clk_set_defaults(struct rt_ofw_node *np);
  220. #else
  221. rt_inline struct rt_clk *rt_ofw_get_clk(struct rt_ofw_node *np, int index)
  222. {
  223. return RT_NULL;
  224. }
  225. rt_inline struct rt_clk *rt_ofw_get_clk_by_name(struct rt_ofw_node *np, const char *name)
  226. {
  227. return RT_NULL;
  228. }
  229. rt_inline rt_ssize_t rt_ofw_count_of_clk(struct rt_ofw_node *clk_ofw_np)
  230. {
  231. return 0;
  232. }
  233. rt_inline const char *rt_ofw_clk_get_parent_name(struct rt_ofw_node *np, int index)
  234. {
  235. return RT_NULL;
  236. }
  237. rt_inline rt_err_t rt_ofw_clk_set_defaults(struct rt_ofw_node *np)
  238. {
  239. return RT_EOK;
  240. }
  241. #endif /* RT_USING_OFW */
  242. /*! @}*/
  243. #endif /* __CLK_H__ */