clk-mtk.h 6.7 KB

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