clk-mtk.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2014 MediaTek Inc.
  4. * Author: James Liao <jamesjj.liao@mediatek.com>
  5. */
  6. #ifndef __DRV_CLK_MTK_H
  7. #define __DRV_CLK_MTK_H
  8. #include <linux/clk-provider.h>
  9. #include <linux/io.h>
  10. #include <linux/kernel.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/types.h>
  13. #include "reset.h"
  14. #define MAX_MUX_GATE_BIT 31
  15. #define INVALID_MUX_GATE_BIT (MAX_MUX_GATE_BIT + 1)
  16. #define MHZ (1000 * 1000)
  17. #define MTK_WAIT_HWV_DONE_US 30
  18. struct platform_device;
  19. /*
  20. * We need the clock IDs to start from zero but to maintain devicetree
  21. * backwards compatibility we can't change bindings to start from zero.
  22. * Only a few platforms are affected, so we solve issues given by the
  23. * commonized MTK clocks probe function(s) by adding a dummy clock at
  24. * the beginning where needed.
  25. */
  26. #define CLK_DUMMY 0
  27. extern const struct clk_ops mtk_clk_dummy_ops;
  28. extern const struct mtk_gate_regs cg_regs_dummy;
  29. #define GATE_DUMMY(_id, _name) { \
  30. .id = _id, \
  31. .name = _name, \
  32. .regs = &cg_regs_dummy, \
  33. .ops = &mtk_clk_dummy_ops, \
  34. }
  35. struct mtk_fixed_clk {
  36. int id;
  37. const char *name;
  38. const char *parent;
  39. unsigned long rate;
  40. };
  41. #define FIXED_CLK(_id, _name, _parent, _rate) { \
  42. .id = _id, \
  43. .name = _name, \
  44. .parent = _parent, \
  45. .rate = _rate, \
  46. }
  47. int mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks, int num,
  48. struct clk_hw_onecell_data *clk_data);
  49. void mtk_clk_unregister_fixed_clks(const struct mtk_fixed_clk *clks, int num,
  50. struct clk_hw_onecell_data *clk_data);
  51. struct mtk_fixed_factor {
  52. int id;
  53. const char *name;
  54. const char *parent_name;
  55. int mult;
  56. int div;
  57. unsigned long flags;
  58. };
  59. #define FACTOR_FLAGS(_id, _name, _parent, _mult, _div, _fl) { \
  60. .id = _id, \
  61. .name = _name, \
  62. .parent_name = _parent, \
  63. .mult = _mult, \
  64. .div = _div, \
  65. .flags = _fl, \
  66. }
  67. #define FACTOR(_id, _name, _parent, _mult, _div) \
  68. FACTOR_FLAGS(_id, _name, _parent, _mult, _div, CLK_SET_RATE_PARENT)
  69. int mtk_clk_register_factors(const struct mtk_fixed_factor *clks, int num,
  70. struct clk_hw_onecell_data *clk_data);
  71. void mtk_clk_unregister_factors(const struct mtk_fixed_factor *clks, int num,
  72. struct clk_hw_onecell_data *clk_data);
  73. struct mtk_composite {
  74. int id;
  75. const char *name;
  76. const char * const *parent_names;
  77. const char *parent;
  78. unsigned flags;
  79. uint32_t mux_reg;
  80. uint32_t divider_reg;
  81. uint32_t gate_reg;
  82. signed char mux_shift;
  83. signed char mux_width;
  84. signed char gate_shift;
  85. signed char divider_shift;
  86. signed char divider_width;
  87. u8 mux_flags;
  88. signed char num_parents;
  89. };
  90. #define MUX_GATE_FLAGS_2(_id, _name, _parents, _reg, _shift, \
  91. _width, _gate, _flags, _muxflags) { \
  92. .id = _id, \
  93. .name = _name, \
  94. .mux_reg = _reg, \
  95. .mux_shift = _shift, \
  96. .mux_width = _width, \
  97. .gate_reg = _reg, \
  98. .gate_shift = _gate, \
  99. .divider_shift = -1, \
  100. .parent_names = _parents, \
  101. .num_parents = ARRAY_SIZE(_parents), \
  102. .flags = _flags, \
  103. .mux_flags = _muxflags, \
  104. }
  105. /*
  106. * In case the rate change propagation to parent clocks is undesirable,
  107. * this macro allows to specify the clock flags manually.
  108. */
  109. #define MUX_GATE_FLAGS(_id, _name, _parents, _reg, _shift, _width, \
  110. _gate, _flags) \
  111. MUX_GATE_FLAGS_2(_id, _name, _parents, _reg, \
  112. _shift, _width, _gate, _flags, 0)
  113. /*
  114. * Unless necessary, all MUX_GATE clocks propagate rate changes to their
  115. * parent clock by default.
  116. */
  117. #define MUX_GATE(_id, _name, _parents, _reg, _shift, _width, _gate) \
  118. MUX_GATE_FLAGS(_id, _name, _parents, _reg, _shift, _width, \
  119. _gate, CLK_SET_RATE_PARENT)
  120. #define MUX(_id, _name, _parents, _reg, _shift, _width) \
  121. MUX_FLAGS(_id, _name, _parents, _reg, \
  122. _shift, _width, CLK_SET_RATE_PARENT)
  123. #define MUX_FLAGS(_id, _name, _parents, _reg, _shift, _width, _flags) { \
  124. .id = _id, \
  125. .name = _name, \
  126. .mux_reg = _reg, \
  127. .mux_shift = _shift, \
  128. .mux_width = _width, \
  129. .gate_shift = -1, \
  130. .divider_shift = -1, \
  131. .parent_names = _parents, \
  132. .num_parents = ARRAY_SIZE(_parents), \
  133. .flags = _flags, \
  134. }
  135. #define DIV_GATE(_id, _name, _parent, _gate_reg, _gate_shift, _div_reg, \
  136. _div_width, _div_shift) { \
  137. .id = _id, \
  138. .parent = _parent, \
  139. .name = _name, \
  140. .divider_reg = _div_reg, \
  141. .divider_shift = _div_shift, \
  142. .divider_width = _div_width, \
  143. .gate_reg = _gate_reg, \
  144. .gate_shift = _gate_shift, \
  145. .mux_shift = -1, \
  146. .flags = 0, \
  147. }
  148. #define MUX_DIV_GATE(_id, _name, _parents, \
  149. _mux_reg, _mux_shift, _mux_width, \
  150. _div_reg, _div_shift, _div_width, \
  151. _gate_reg, _gate_shift) { \
  152. .id = _id, \
  153. .name = _name, \
  154. .parent_names = _parents, \
  155. .num_parents = ARRAY_SIZE(_parents), \
  156. .mux_reg = _mux_reg, \
  157. .mux_shift = _mux_shift, \
  158. .mux_width = _mux_width, \
  159. .divider_reg = _div_reg, \
  160. .divider_shift = _div_shift, \
  161. .divider_width = _div_width, \
  162. .gate_reg = _gate_reg, \
  163. .gate_shift = _gate_shift, \
  164. .flags = CLK_SET_RATE_PARENT, \
  165. }
  166. int mtk_clk_register_composites(struct device *dev,
  167. const struct mtk_composite *mcs, int num,
  168. void __iomem *base, spinlock_t *lock,
  169. struct clk_hw_onecell_data *clk_data);
  170. void mtk_clk_unregister_composites(const struct mtk_composite *mcs, int num,
  171. struct clk_hw_onecell_data *clk_data);
  172. struct mtk_clk_divider {
  173. int id;
  174. const char *name;
  175. const char *parent_name;
  176. unsigned long flags;
  177. u32 div_reg;
  178. unsigned char div_shift;
  179. unsigned char div_width;
  180. unsigned char clk_divider_flags;
  181. const struct clk_div_table *clk_div_table;
  182. };
  183. #define DIV_ADJ(_id, _name, _parent, _reg, _shift, _width) { \
  184. .id = _id, \
  185. .name = _name, \
  186. .parent_name = _parent, \
  187. .div_reg = _reg, \
  188. .div_shift = _shift, \
  189. .div_width = _width, \
  190. }
  191. int mtk_clk_register_dividers(struct device *dev,
  192. const struct mtk_clk_divider *mcds, int num,
  193. void __iomem *base, spinlock_t *lock,
  194. struct clk_hw_onecell_data *clk_data);
  195. void mtk_clk_unregister_dividers(const struct mtk_clk_divider *mcds, int num,
  196. struct clk_hw_onecell_data *clk_data);
  197. struct clk_hw_onecell_data *mtk_alloc_clk_data(unsigned int clk_num);
  198. struct clk_hw_onecell_data *mtk_devm_alloc_clk_data(struct device *dev,
  199. unsigned int clk_num);
  200. void mtk_free_clk_data(struct clk_hw_onecell_data *clk_data);
  201. struct clk_hw *mtk_clk_register_ref2usb_tx(const char *name,
  202. const char *parent_name, void __iomem *reg);
  203. void mtk_clk_unregister_ref2usb_tx(struct clk_hw *hw);
  204. struct mtk_clk_desc {
  205. const struct mtk_gate *clks;
  206. size_t num_clks;
  207. const struct mtk_composite *composite_clks;
  208. size_t num_composite_clks;
  209. const struct mtk_clk_divider *divider_clks;
  210. size_t num_divider_clks;
  211. const struct mtk_fixed_clk *fixed_clks;
  212. size_t num_fixed_clks;
  213. const struct mtk_fixed_factor *factor_clks;
  214. size_t num_factor_clks;
  215. const struct mtk_mux *mux_clks;
  216. size_t num_mux_clks;
  217. const struct mtk_clk_rst_desc *rst_desc;
  218. spinlock_t *clk_lock;
  219. bool shared_io;
  220. int (*clk_notifier_func)(struct device *dev, struct clk *clk);
  221. unsigned int mfg_clk_idx;
  222. bool need_runtime_pm;
  223. };
  224. int mtk_clk_pdev_probe(struct platform_device *pdev);
  225. void mtk_clk_pdev_remove(struct platform_device *pdev);
  226. int mtk_clk_simple_probe(struct platform_device *pdev);
  227. void mtk_clk_simple_remove(struct platform_device *pdev);
  228. struct regmap *mtk_clk_get_hwv_regmap(struct device_node *node);
  229. #endif /* __DRV_CLK_MTK_H */