clk-mtk.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. struct clk_onecell_data * __init mtk_alloc_clk_data(unsigned int clk_num)
  25. {
  26. int i;
  27. struct clk_onecell_data *clk_data;
  28. clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
  29. if (!clk_data)
  30. return NULL;
  31. clk_data->clks = kcalloc(clk_num, sizeof(*clk_data->clks), GFP_KERNEL);
  32. if (!clk_data->clks)
  33. goto err_out;
  34. clk_data->clk_num = clk_num;
  35. for (i = 0; i < clk_num; i++)
  36. clk_data->clks[i] = ERR_PTR(-ENOENT);
  37. return clk_data;
  38. err_out:
  39. kfree(clk_data);
  40. return NULL;
  41. }
  42. void __init mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks,
  43. int num, struct clk_onecell_data *clk_data)
  44. {
  45. int i;
  46. struct clk *clk;
  47. for (i = 0; i < num; i++) {
  48. const struct mtk_fixed_clk *rc = &clks[i];
  49. clk = clk_register_fixed_rate(NULL, rc->name, rc->parent,
  50. rc->parent ? 0 : CLK_IS_ROOT, rc->rate);
  51. if (IS_ERR(clk)) {
  52. pr_err("Failed to register clk %s: %ld\n",
  53. rc->name, PTR_ERR(clk));
  54. continue;
  55. }
  56. if (clk_data)
  57. clk_data->clks[rc->id] = clk;
  58. }
  59. }
  60. void __init mtk_clk_register_factors(const struct mtk_fixed_factor *clks,
  61. int num, struct clk_onecell_data *clk_data)
  62. {
  63. int i;
  64. struct clk *clk;
  65. for (i = 0; i < num; i++) {
  66. const struct mtk_fixed_factor *ff = &clks[i];
  67. clk = clk_register_fixed_factor(NULL, ff->name, ff->parent_name,
  68. CLK_SET_RATE_PARENT, ff->mult, ff->div);
  69. if (IS_ERR(clk)) {
  70. pr_err("Failed to register clk %s: %ld\n",
  71. ff->name, PTR_ERR(clk));
  72. continue;
  73. }
  74. if (clk_data)
  75. clk_data->clks[ff->id] = clk;
  76. }
  77. }
  78. int __init mtk_clk_register_gates(struct device_node *node,
  79. const struct mtk_gate *clks,
  80. int num, struct clk_onecell_data *clk_data)
  81. {
  82. int i;
  83. struct clk *clk;
  84. struct regmap *regmap;
  85. if (!clk_data)
  86. return -ENOMEM;
  87. regmap = syscon_node_to_regmap(node);
  88. if (IS_ERR(regmap)) {
  89. pr_err("Cannot find regmap for %s: %ld\n", node->full_name,
  90. PTR_ERR(regmap));
  91. return PTR_ERR(regmap);
  92. }
  93. for (i = 0; i < num; i++) {
  94. const struct mtk_gate *gate = &clks[i];
  95. clk = mtk_clk_register_gate(gate->name, gate->parent_name,
  96. regmap,
  97. gate->regs->set_ofs,
  98. gate->regs->clr_ofs,
  99. gate->regs->sta_ofs,
  100. gate->shift, gate->ops);
  101. if (IS_ERR(clk)) {
  102. pr_err("Failed to register clk %s: %ld\n",
  103. gate->name, PTR_ERR(clk));
  104. continue;
  105. }
  106. clk_data->clks[gate->id] = clk;
  107. }
  108. return 0;
  109. }
  110. struct clk * __init mtk_clk_register_composite(const struct mtk_composite *mc,
  111. void __iomem *base, spinlock_t *lock)
  112. {
  113. struct clk *clk;
  114. struct clk_mux *mux = NULL;
  115. struct clk_gate *gate = NULL;
  116. struct clk_divider *div = NULL;
  117. struct clk_hw *mux_hw = NULL, *gate_hw = NULL, *div_hw = NULL;
  118. const struct clk_ops *mux_ops = NULL, *gate_ops = NULL, *div_ops = NULL;
  119. const char * const *parent_names;
  120. const char *parent;
  121. int num_parents;
  122. int ret;
  123. if (mc->mux_shift >= 0) {
  124. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  125. if (!mux)
  126. return ERR_PTR(-ENOMEM);
  127. mux->reg = base + mc->mux_reg;
  128. mux->mask = BIT(mc->mux_width) - 1;
  129. mux->shift = mc->mux_shift;
  130. mux->lock = lock;
  131. mux_hw = &mux->hw;
  132. mux_ops = &clk_mux_ops;
  133. parent_names = mc->parent_names;
  134. num_parents = mc->num_parents;
  135. } else {
  136. parent = mc->parent;
  137. parent_names = &parent;
  138. num_parents = 1;
  139. }
  140. if (mc->gate_shift >= 0) {
  141. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  142. if (!gate) {
  143. ret = -ENOMEM;
  144. goto err_out;
  145. }
  146. gate->reg = base + mc->gate_reg;
  147. gate->bit_idx = mc->gate_shift;
  148. gate->flags = CLK_GATE_SET_TO_DISABLE;
  149. gate->lock = lock;
  150. gate_hw = &gate->hw;
  151. gate_ops = &clk_gate_ops;
  152. }
  153. if (mc->divider_shift >= 0) {
  154. div = kzalloc(sizeof(*div), GFP_KERNEL);
  155. if (!div) {
  156. ret = -ENOMEM;
  157. goto err_out;
  158. }
  159. div->reg = base + mc->divider_reg;
  160. div->shift = mc->divider_shift;
  161. div->width = mc->divider_width;
  162. div->lock = lock;
  163. div_hw = &div->hw;
  164. div_ops = &clk_divider_ops;
  165. }
  166. clk = clk_register_composite(NULL, mc->name, parent_names, num_parents,
  167. mux_hw, mux_ops,
  168. div_hw, div_ops,
  169. gate_hw, gate_ops,
  170. mc->flags);
  171. if (IS_ERR(clk)) {
  172. kfree(gate);
  173. kfree(mux);
  174. }
  175. return clk;
  176. err_out:
  177. kfree(mux);
  178. return ERR_PTR(ret);
  179. }
  180. void __init mtk_clk_register_composites(const struct mtk_composite *mcs,
  181. int num, void __iomem *base, spinlock_t *lock,
  182. struct clk_onecell_data *clk_data)
  183. {
  184. struct clk *clk;
  185. int i;
  186. for (i = 0; i < num; i++) {
  187. const struct mtk_composite *mc = &mcs[i];
  188. clk = mtk_clk_register_composite(mc, base, lock);
  189. if (IS_ERR(clk)) {
  190. pr_err("Failed to register clk %s: %ld\n",
  191. mc->name, PTR_ERR(clk));
  192. continue;
  193. }
  194. if (clk_data)
  195. clk_data->clks[mc->id] = clk;
  196. }
  197. }