common.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/export.h>
  14. #include <linux/module.h>
  15. #include <linux/regmap.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/clk-provider.h>
  18. #include <linux/reset-controller.h>
  19. #include "common.h"
  20. #include "clk-rcg.h"
  21. #include "clk-regmap.h"
  22. #include "reset.h"
  23. #include "gdsc.h"
  24. struct qcom_cc {
  25. struct qcom_reset_controller reset;
  26. struct clk_onecell_data data;
  27. struct clk *clks[];
  28. };
  29. const
  30. struct freq_tbl *qcom_find_freq(const struct freq_tbl *f, unsigned long rate)
  31. {
  32. if (!f)
  33. return NULL;
  34. for (; f->freq; f++)
  35. if (rate <= f->freq)
  36. return f;
  37. /* Default to our fastest rate */
  38. return f - 1;
  39. }
  40. EXPORT_SYMBOL_GPL(qcom_find_freq);
  41. int qcom_find_src_index(struct clk_hw *hw, const struct parent_map *map, u8 src)
  42. {
  43. int i, num_parents = clk_hw_get_num_parents(hw);
  44. for (i = 0; i < num_parents; i++)
  45. if (src == map[i].src)
  46. return i;
  47. return -ENOENT;
  48. }
  49. EXPORT_SYMBOL_GPL(qcom_find_src_index);
  50. struct regmap *
  51. qcom_cc_map(struct platform_device *pdev, const struct qcom_cc_desc *desc)
  52. {
  53. void __iomem *base;
  54. struct resource *res;
  55. struct device *dev = &pdev->dev;
  56. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  57. base = devm_ioremap_resource(dev, res);
  58. if (IS_ERR(base))
  59. return ERR_CAST(base);
  60. return devm_regmap_init_mmio(dev, base, desc->config);
  61. }
  62. EXPORT_SYMBOL_GPL(qcom_cc_map);
  63. static void qcom_cc_del_clk_provider(void *data)
  64. {
  65. of_clk_del_provider(data);
  66. }
  67. static void qcom_cc_reset_unregister(void *data)
  68. {
  69. reset_controller_unregister(data);
  70. }
  71. static void qcom_cc_gdsc_unregister(void *data)
  72. {
  73. gdsc_unregister(data);
  74. }
  75. int qcom_cc_really_probe(struct platform_device *pdev,
  76. const struct qcom_cc_desc *desc, struct regmap *regmap)
  77. {
  78. int i, ret;
  79. struct device *dev = &pdev->dev;
  80. struct clk *clk;
  81. struct clk_onecell_data *data;
  82. struct clk **clks;
  83. struct qcom_reset_controller *reset;
  84. struct qcom_cc *cc;
  85. size_t num_clks = desc->num_clks;
  86. struct clk_regmap **rclks = desc->clks;
  87. cc = devm_kzalloc(dev, sizeof(*cc) + sizeof(*clks) * num_clks,
  88. GFP_KERNEL);
  89. if (!cc)
  90. return -ENOMEM;
  91. clks = cc->clks;
  92. data = &cc->data;
  93. data->clks = clks;
  94. data->clk_num = num_clks;
  95. for (i = 0; i < num_clks; i++) {
  96. if (!rclks[i]) {
  97. clks[i] = ERR_PTR(-ENOENT);
  98. continue;
  99. }
  100. clk = devm_clk_register_regmap(dev, rclks[i]);
  101. if (IS_ERR(clk))
  102. return PTR_ERR(clk);
  103. clks[i] = clk;
  104. }
  105. ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get, data);
  106. if (ret)
  107. return ret;
  108. devm_add_action(dev, qcom_cc_del_clk_provider, pdev->dev.of_node);
  109. reset = &cc->reset;
  110. reset->rcdev.of_node = dev->of_node;
  111. reset->rcdev.ops = &qcom_reset_ops;
  112. reset->rcdev.owner = dev->driver->owner;
  113. reset->rcdev.nr_resets = desc->num_resets;
  114. reset->regmap = regmap;
  115. reset->reset_map = desc->resets;
  116. ret = reset_controller_register(&reset->rcdev);
  117. if (ret)
  118. return ret;
  119. devm_add_action(dev, qcom_cc_reset_unregister, &reset->rcdev);
  120. if (desc->gdscs && desc->num_gdscs) {
  121. ret = gdsc_register(dev, desc->gdscs, desc->num_gdscs,
  122. &reset->rcdev, regmap);
  123. if (ret)
  124. return ret;
  125. }
  126. devm_add_action(dev, qcom_cc_gdsc_unregister, dev);
  127. return 0;
  128. }
  129. EXPORT_SYMBOL_GPL(qcom_cc_really_probe);
  130. int qcom_cc_probe(struct platform_device *pdev, const struct qcom_cc_desc *desc)
  131. {
  132. struct regmap *regmap;
  133. regmap = qcom_cc_map(pdev, desc);
  134. if (IS_ERR(regmap))
  135. return PTR_ERR(regmap);
  136. return qcom_cc_really_probe(pdev, desc, regmap);
  137. }
  138. EXPORT_SYMBOL_GPL(qcom_cc_probe);
  139. MODULE_LICENSE("GPL v2");