clk-mtk.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 2014 MediaTek Inc.
  3. * Author: James Liao <jamesjj.liao@mediatek.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/err.h>
  17. #include <linux/io.h>
  18. #include <linux/slab.h>
  19. #include <linux/delay.h>
  20. #include <linux/clkdev.h>
  21. #include <linux/mfd/syscon.h>
  22. #include "clk-mtk.h"
  23. #include "clk-gate.h"
  24. #define WORKAROUND_318_WARNING 1
  25. struct clk_onecell_data * __init mtk_alloc_clk_data(unsigned int clk_num)
  26. {
  27. int i;
  28. struct clk_onecell_data *clk_data;
  29. clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
  30. if (!clk_data)
  31. return NULL;
  32. clk_data->clks = kcalloc(clk_num, sizeof(*clk_data->clks), GFP_KERNEL);
  33. if (!clk_data->clks)
  34. goto err_out;
  35. clk_data->clk_num = clk_num;
  36. for (i = 0; i < clk_num; i++)
  37. clk_data->clks[i] = ERR_PTR(-ENOENT);
  38. return clk_data;
  39. err_out:
  40. kfree(clk_data);
  41. return NULL;
  42. }
  43. void __init mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks,
  44. int num, struct clk_onecell_data *clk_data)
  45. {
  46. int i;
  47. struct clk *clk;
  48. for (i = 0; i < num; i++) {
  49. const struct mtk_fixed_clk *rc = &clks[i];
  50. clk = clk_register_fixed_rate(NULL, rc->name, rc->parent,
  51. rc->parent ? 0 : CLK_IS_ROOT, rc->rate);
  52. if (IS_ERR(clk)) {
  53. pr_err("Failed to register clk %s: %ld\n",
  54. rc->name, PTR_ERR(clk));
  55. continue;
  56. }
  57. if (clk_data)
  58. clk_data->clks[rc->id] = clk;
  59. }
  60. }
  61. void __init mtk_clk_register_factors(const struct mtk_fixed_factor *clks,
  62. int num, struct clk_onecell_data *clk_data)
  63. {
  64. int i;
  65. struct clk *clk;
  66. for (i = 0; i < num; i++) {
  67. const struct mtk_fixed_factor *ff = &clks[i];
  68. clk = clk_register_fixed_factor(NULL, ff->name, ff->parent_name,
  69. CLK_SET_RATE_PARENT, ff->mult, ff->div);
  70. if (IS_ERR(clk)) {
  71. pr_err("Failed to register clk %s: %ld\n",
  72. ff->name, PTR_ERR(clk));
  73. continue;
  74. }
  75. if (clk_data)
  76. clk_data->clks[ff->id] = clk;
  77. }
  78. }
  79. int __init mtk_clk_register_gates(struct device_node *node,
  80. const struct mtk_gate *clks,
  81. int num, struct clk_onecell_data *clk_data)
  82. {
  83. int i;
  84. struct clk *clk;
  85. struct regmap *regmap;
  86. if (!clk_data)
  87. return -ENOMEM;
  88. regmap = syscon_node_to_regmap(node);
  89. if (IS_ERR(regmap)) {
  90. pr_err("Cannot find regmap for %s: %ld\n", node->full_name,
  91. PTR_ERR(regmap));
  92. return PTR_ERR(regmap);
  93. }
  94. for (i = 0; i < num; i++) {
  95. const struct mtk_gate *gate = &clks[i];
  96. clk = mtk_clk_register_gate(gate->name, gate->parent_name,
  97. regmap,
  98. gate->regs->set_ofs,
  99. gate->regs->clr_ofs,
  100. gate->regs->sta_ofs,
  101. gate->shift, gate->ops);
  102. if (IS_ERR(clk)) {
  103. pr_err("Failed to register clk %s: %ld\n",
  104. gate->name, PTR_ERR(clk));
  105. continue;
  106. }
  107. clk_data->clks[gate->id] = clk;
  108. }
  109. return 0;
  110. }
  111. struct clk * __init mtk_clk_register_composite(const struct mtk_composite *mc,
  112. void __iomem *base, spinlock_t *lock)
  113. {
  114. struct clk *clk;
  115. struct clk_mux *mux = NULL;
  116. struct clk_gate *gate = NULL;
  117. struct clk_divider *div = NULL;
  118. struct clk_hw *mux_hw = NULL, *gate_hw = NULL, *div_hw = NULL;
  119. const struct clk_ops *mux_ops = NULL, *gate_ops = NULL, *div_ops = NULL;
  120. const char * const *parent_names;
  121. const char *parent;
  122. int num_parents;
  123. int ret;
  124. if (mc->mux_shift >= 0) {
  125. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  126. if (!mux)
  127. return ERR_PTR(-ENOMEM);
  128. mux->reg = base + mc->mux_reg;
  129. mux->mask = BIT(mc->mux_width) - 1;
  130. mux->shift = mc->mux_shift;
  131. mux->lock = lock;
  132. mux_hw = &mux->hw;
  133. mux_ops = &clk_mux_ops;
  134. parent_names = mc->parent_names;
  135. num_parents = mc->num_parents;
  136. } else {
  137. parent = mc->parent;
  138. parent_names = &parent;
  139. num_parents = 1;
  140. }
  141. if (mc->gate_shift >= 0) {
  142. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  143. if (!gate) {
  144. ret = -ENOMEM;
  145. goto err_out;
  146. }
  147. gate->reg = base + mc->gate_reg;
  148. gate->bit_idx = mc->gate_shift;
  149. gate->flags = CLK_GATE_SET_TO_DISABLE;
  150. gate->lock = lock;
  151. gate_hw = &gate->hw;
  152. gate_ops = &clk_gate_ops;
  153. }
  154. if (mc->divider_shift >= 0) {
  155. div = kzalloc(sizeof(*div), GFP_KERNEL);
  156. if (!div) {
  157. ret = -ENOMEM;
  158. goto err_out;
  159. }
  160. div->reg = base + mc->divider_reg;
  161. div->shift = mc->divider_shift;
  162. div->width = mc->divider_width;
  163. div->lock = lock;
  164. div_hw = &div->hw;
  165. div_ops = &clk_divider_ops;
  166. }
  167. #if WORKAROUND_318_WARNING
  168. clk = clk_register_composite(NULL, mc->name,
  169. (const char **)parent_names, num_parents,
  170. #else
  171. clk = clk_register_composite(NULL, mc->name, parent_names, num_parents,
  172. #endif
  173. mux_hw, mux_ops,
  174. div_hw, div_ops,
  175. gate_hw, gate_ops,
  176. mc->flags);
  177. if (IS_ERR(clk)) {
  178. kfree(gate);
  179. kfree(mux);
  180. }
  181. return clk;
  182. err_out:
  183. kfree(mux);
  184. return ERR_PTR(ret);
  185. }
  186. void __init mtk_clk_register_composites(const struct mtk_composite *mcs,
  187. int num, void __iomem *base, spinlock_t *lock,
  188. struct clk_onecell_data *clk_data)
  189. {
  190. struct clk *clk;
  191. int i;
  192. for (i = 0; i < num; i++) {
  193. const struct mtk_composite *mc = &mcs[i];
  194. clk = mtk_clk_register_composite(mc, base, lock);
  195. if (IS_ERR(clk)) {
  196. pr_err("Failed to register clk %s: %ld\n",
  197. mc->name, PTR_ERR(clk));
  198. continue;
  199. }
  200. if (clk_data)
  201. clk_data->clks[mc->id] = clk;
  202. }
  203. }