clk-periph.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/clk-provider.h>
  7. #include <linux/export.h>
  8. #include <linux/slab.h>
  9. #include <linux/err.h>
  10. #include "clk.h"
  11. static u8 clk_periph_get_parent(struct clk_hw *hw)
  12. {
  13. struct tegra_clk_periph *periph = to_clk_periph(hw);
  14. const struct clk_ops *mux_ops = periph->mux_ops;
  15. struct clk_hw *mux_hw = &periph->mux.hw;
  16. __clk_hw_set_clk(mux_hw, hw);
  17. return mux_ops->get_parent(mux_hw);
  18. }
  19. static int clk_periph_set_parent(struct clk_hw *hw, u8 index)
  20. {
  21. struct tegra_clk_periph *periph = to_clk_periph(hw);
  22. const struct clk_ops *mux_ops = periph->mux_ops;
  23. struct clk_hw *mux_hw = &periph->mux.hw;
  24. __clk_hw_set_clk(mux_hw, hw);
  25. return mux_ops->set_parent(mux_hw, index);
  26. }
  27. static unsigned long clk_periph_recalc_rate(struct clk_hw *hw,
  28. unsigned long parent_rate)
  29. {
  30. struct tegra_clk_periph *periph = to_clk_periph(hw);
  31. const struct clk_ops *div_ops = periph->div_ops;
  32. struct clk_hw *div_hw = &periph->divider.hw;
  33. __clk_hw_set_clk(div_hw, hw);
  34. return div_ops->recalc_rate(div_hw, parent_rate);
  35. }
  36. static long clk_periph_round_rate(struct clk_hw *hw, unsigned long rate,
  37. unsigned long *prate)
  38. {
  39. struct tegra_clk_periph *periph = to_clk_periph(hw);
  40. const struct clk_ops *div_ops = periph->div_ops;
  41. struct clk_hw *div_hw = &periph->divider.hw;
  42. __clk_hw_set_clk(div_hw, hw);
  43. return div_ops->round_rate(div_hw, rate, prate);
  44. }
  45. static int clk_periph_set_rate(struct clk_hw *hw, unsigned long rate,
  46. unsigned long parent_rate)
  47. {
  48. struct tegra_clk_periph *periph = to_clk_periph(hw);
  49. const struct clk_ops *div_ops = periph->div_ops;
  50. struct clk_hw *div_hw = &periph->divider.hw;
  51. __clk_hw_set_clk(div_hw, hw);
  52. return div_ops->set_rate(div_hw, rate, parent_rate);
  53. }
  54. static int clk_periph_is_enabled(struct clk_hw *hw)
  55. {
  56. struct tegra_clk_periph *periph = to_clk_periph(hw);
  57. const struct clk_ops *gate_ops = periph->gate_ops;
  58. struct clk_hw *gate_hw = &periph->gate.hw;
  59. __clk_hw_set_clk(gate_hw, hw);
  60. return gate_ops->is_enabled(gate_hw);
  61. }
  62. static int clk_periph_enable(struct clk_hw *hw)
  63. {
  64. struct tegra_clk_periph *periph = to_clk_periph(hw);
  65. const struct clk_ops *gate_ops = periph->gate_ops;
  66. struct clk_hw *gate_hw = &periph->gate.hw;
  67. __clk_hw_set_clk(gate_hw, hw);
  68. return gate_ops->enable(gate_hw);
  69. }
  70. static void clk_periph_disable(struct clk_hw *hw)
  71. {
  72. struct tegra_clk_periph *periph = to_clk_periph(hw);
  73. const struct clk_ops *gate_ops = periph->gate_ops;
  74. struct clk_hw *gate_hw = &periph->gate.hw;
  75. gate_ops->disable(gate_hw);
  76. }
  77. static void clk_periph_disable_unused(struct clk_hw *hw)
  78. {
  79. struct tegra_clk_periph *periph = to_clk_periph(hw);
  80. const struct clk_ops *gate_ops = periph->gate_ops;
  81. struct clk_hw *gate_hw = &periph->gate.hw;
  82. gate_ops->disable_unused(gate_hw);
  83. }
  84. static void clk_periph_restore_context(struct clk_hw *hw)
  85. {
  86. struct tegra_clk_periph *periph = to_clk_periph(hw);
  87. const struct clk_ops *div_ops = periph->div_ops;
  88. struct clk_hw *div_hw = &periph->divider.hw;
  89. int parent_id;
  90. parent_id = clk_hw_get_parent_index(hw);
  91. if (WARN_ON(parent_id < 0))
  92. return;
  93. if (!(periph->gate.flags & TEGRA_PERIPH_NO_DIV))
  94. div_ops->restore_context(div_hw);
  95. clk_periph_set_parent(hw, parent_id);
  96. }
  97. const struct clk_ops tegra_clk_periph_ops = {
  98. .get_parent = clk_periph_get_parent,
  99. .set_parent = clk_periph_set_parent,
  100. .recalc_rate = clk_periph_recalc_rate,
  101. .round_rate = clk_periph_round_rate,
  102. .set_rate = clk_periph_set_rate,
  103. .is_enabled = clk_periph_is_enabled,
  104. .enable = clk_periph_enable,
  105. .disable = clk_periph_disable,
  106. .disable_unused = clk_periph_disable_unused,
  107. .restore_context = clk_periph_restore_context,
  108. };
  109. static const struct clk_ops tegra_clk_periph_nodiv_ops = {
  110. .get_parent = clk_periph_get_parent,
  111. .set_parent = clk_periph_set_parent,
  112. .is_enabled = clk_periph_is_enabled,
  113. .enable = clk_periph_enable,
  114. .disable = clk_periph_disable,
  115. .disable_unused = clk_periph_disable_unused,
  116. .restore_context = clk_periph_restore_context,
  117. };
  118. static const struct clk_ops tegra_clk_periph_no_gate_ops = {
  119. .get_parent = clk_periph_get_parent,
  120. .set_parent = clk_periph_set_parent,
  121. .recalc_rate = clk_periph_recalc_rate,
  122. .round_rate = clk_periph_round_rate,
  123. .set_rate = clk_periph_set_rate,
  124. .restore_context = clk_periph_restore_context,
  125. };
  126. static struct clk *_tegra_clk_register_periph(const char *name,
  127. const char * const *parent_names, int num_parents,
  128. struct tegra_clk_periph *periph,
  129. void __iomem *clk_base, u32 offset,
  130. unsigned long flags)
  131. {
  132. struct clk *clk;
  133. struct clk_init_data init;
  134. const struct tegra_clk_periph_regs *bank;
  135. bool div = !(periph->gate.flags & TEGRA_PERIPH_NO_DIV);
  136. if (periph->gate.flags & TEGRA_PERIPH_NO_DIV) {
  137. flags |= CLK_SET_RATE_PARENT;
  138. init.ops = &tegra_clk_periph_nodiv_ops;
  139. } else if (periph->gate.flags & TEGRA_PERIPH_NO_GATE)
  140. init.ops = &tegra_clk_periph_no_gate_ops;
  141. else
  142. init.ops = &tegra_clk_periph_ops;
  143. init.name = name;
  144. init.flags = flags;
  145. init.parent_names = parent_names;
  146. init.num_parents = num_parents;
  147. bank = get_reg_bank(periph->gate.clk_num);
  148. if (!bank)
  149. return ERR_PTR(-EINVAL);
  150. /* Data in .init is copied by clk_register(), so stack variable OK */
  151. periph->hw.init = &init;
  152. periph->magic = TEGRA_CLK_PERIPH_MAGIC;
  153. periph->mux.reg = clk_base + offset;
  154. periph->divider.reg = div ? (clk_base + offset) : NULL;
  155. periph->gate.clk_base = clk_base;
  156. periph->gate.regs = bank;
  157. periph->gate.enable_refcnt = periph_clk_enb_refcnt;
  158. clk = clk_register(NULL, &periph->hw);
  159. if (IS_ERR(clk))
  160. return clk;
  161. periph->mux.hw.clk = clk;
  162. periph->divider.hw.clk = div ? clk : NULL;
  163. periph->gate.hw.clk = clk;
  164. return clk;
  165. }
  166. struct clk *tegra_clk_register_periph(const char *name,
  167. const char * const *parent_names, int num_parents,
  168. struct tegra_clk_periph *periph, void __iomem *clk_base,
  169. u32 offset, unsigned long flags)
  170. {
  171. return _tegra_clk_register_periph(name, parent_names, num_parents,
  172. periph, clk_base, offset, flags);
  173. }
  174. struct clk *tegra_clk_register_periph_nodiv(const char *name,
  175. const char * const *parent_names, int num_parents,
  176. struct tegra_clk_periph *periph, void __iomem *clk_base,
  177. u32 offset)
  178. {
  179. periph->gate.flags |= TEGRA_PERIPH_NO_DIV;
  180. return _tegra_clk_register_periph(name, parent_names, num_parents,
  181. periph, clk_base, offset, CLK_SET_RATE_PARENT);
  182. }
  183. struct clk *tegra_clk_register_periph_data(void __iomem *clk_base,
  184. struct tegra_periph_init_data *init)
  185. {
  186. return _tegra_clk_register_periph(init->name, init->p.parent_names,
  187. init->num_parents, &init->periph,
  188. clk_base, init->offset, init->flags);
  189. }