clk-periph.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 int clk_periph_determine_rate(struct clk_hw *hw,
  37. struct clk_rate_request *req)
  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->determine_rate(div_hw, req);
  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. static 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. .determine_rate = clk_periph_determine_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. .determine_rate = clk_hw_determine_rate_no_reparent,
  111. .get_parent = clk_periph_get_parent,
  112. .set_parent = clk_periph_set_parent,
  113. .is_enabled = clk_periph_is_enabled,
  114. .enable = clk_periph_enable,
  115. .disable = clk_periph_disable,
  116. .disable_unused = clk_periph_disable_unused,
  117. .restore_context = clk_periph_restore_context,
  118. };
  119. static const struct clk_ops tegra_clk_periph_no_gate_ops = {
  120. .get_parent = clk_periph_get_parent,
  121. .set_parent = clk_periph_set_parent,
  122. .recalc_rate = clk_periph_recalc_rate,
  123. .determine_rate = clk_periph_determine_rate,
  124. .set_rate = clk_periph_set_rate,
  125. .restore_context = clk_periph_restore_context,
  126. };
  127. static struct clk *_tegra_clk_register_periph(const char *name,
  128. const char * const *parent_names, int num_parents,
  129. struct tegra_clk_periph *periph,
  130. void __iomem *clk_base, u32 offset,
  131. unsigned long flags)
  132. {
  133. struct clk *clk;
  134. struct clk_init_data init;
  135. const struct tegra_clk_periph_regs *bank;
  136. bool div = !(periph->gate.flags & TEGRA_PERIPH_NO_DIV);
  137. if (periph->gate.flags & TEGRA_PERIPH_NO_DIV) {
  138. flags |= CLK_SET_RATE_PARENT;
  139. init.ops = &tegra_clk_periph_nodiv_ops;
  140. } else if (periph->gate.flags & TEGRA_PERIPH_NO_GATE)
  141. init.ops = &tegra_clk_periph_no_gate_ops;
  142. else
  143. init.ops = &tegra_clk_periph_ops;
  144. init.name = name;
  145. init.flags = flags;
  146. init.parent_names = parent_names;
  147. init.num_parents = num_parents;
  148. bank = get_reg_bank(periph->gate.clk_num);
  149. if (!bank)
  150. return ERR_PTR(-EINVAL);
  151. /* Data in .init is copied by clk_register(), so stack variable OK */
  152. periph->hw.init = &init;
  153. periph->magic = TEGRA_CLK_PERIPH_MAGIC;
  154. periph->mux.reg = clk_base + offset;
  155. periph->divider.reg = div ? (clk_base + offset) : NULL;
  156. periph->gate.clk_base = clk_base;
  157. periph->gate.regs = bank;
  158. periph->gate.enable_refcnt = periph_clk_enb_refcnt;
  159. clk = clk_register(NULL, &periph->hw);
  160. if (IS_ERR(clk))
  161. return clk;
  162. periph->mux.hw.clk = clk;
  163. periph->divider.hw.clk = div ? clk : NULL;
  164. periph->gate.hw.clk = clk;
  165. return clk;
  166. }
  167. struct clk *tegra_clk_register_periph(const char *name,
  168. const char * const *parent_names, int num_parents,
  169. struct tegra_clk_periph *periph, void __iomem *clk_base,
  170. u32 offset, unsigned long flags)
  171. {
  172. return _tegra_clk_register_periph(name, parent_names, num_parents,
  173. periph, clk_base, offset, flags);
  174. }
  175. struct clk *tegra_clk_register_periph_nodiv(const char *name,
  176. const char * const *parent_names, int num_parents,
  177. struct tegra_clk_periph *periph, void __iomem *clk_base,
  178. u32 offset)
  179. {
  180. periph->gate.flags |= TEGRA_PERIPH_NO_DIV;
  181. return _tegra_clk_register_periph(name, parent_names, num_parents,
  182. periph, clk_base, offset, CLK_SET_RATE_PARENT);
  183. }
  184. struct clk *tegra_clk_register_periph_data(void __iomem *clk_base,
  185. struct tegra_periph_init_data *init)
  186. {
  187. return _tegra_clk_register_periph(init->name, init->p.parent_names,
  188. init->num_parents, &init->periph,
  189. clk_base, init->offset, init->flags);
  190. }