common.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. struct qcom_cc {
  24. struct qcom_reset_controller reset;
  25. struct clk_onecell_data data;
  26. struct clk *clks[];
  27. };
  28. const
  29. struct freq_tbl *qcom_find_freq(const struct freq_tbl *f, unsigned long rate)
  30. {
  31. if (!f)
  32. return NULL;
  33. for (; f->freq; f++)
  34. if (rate <= f->freq)
  35. return f;
  36. /* Default to our fastest rate */
  37. return f - 1;
  38. }
  39. EXPORT_SYMBOL_GPL(qcom_find_freq);
  40. int qcom_find_src_index(struct clk_hw *hw, const struct parent_map *map, u8 src)
  41. {
  42. int i, num_parents = clk_hw_get_num_parents(hw);
  43. for (i = 0; i < num_parents; i++)
  44. if (src == map[i].src)
  45. return i;
  46. return -ENOENT;
  47. }
  48. EXPORT_SYMBOL_GPL(qcom_find_src_index);
  49. struct regmap *
  50. qcom_cc_map(struct platform_device *pdev, const struct qcom_cc_desc *desc)
  51. {
  52. void __iomem *base;
  53. struct resource *res;
  54. struct device *dev = &pdev->dev;
  55. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  56. base = devm_ioremap_resource(dev, res);
  57. if (IS_ERR(base))
  58. return ERR_CAST(base);
  59. return devm_regmap_init_mmio(dev, base, desc->config);
  60. }
  61. EXPORT_SYMBOL_GPL(qcom_cc_map);
  62. int qcom_cc_really_probe(struct platform_device *pdev,
  63. const struct qcom_cc_desc *desc, struct regmap *regmap)
  64. {
  65. int i, ret;
  66. struct device *dev = &pdev->dev;
  67. struct clk *clk;
  68. struct clk_onecell_data *data;
  69. struct clk **clks;
  70. struct qcom_reset_controller *reset;
  71. struct qcom_cc *cc;
  72. size_t num_clks = desc->num_clks;
  73. struct clk_regmap **rclks = desc->clks;
  74. cc = devm_kzalloc(dev, sizeof(*cc) + sizeof(*clks) * num_clks,
  75. GFP_KERNEL);
  76. if (!cc)
  77. return -ENOMEM;
  78. clks = cc->clks;
  79. data = &cc->data;
  80. data->clks = clks;
  81. data->clk_num = num_clks;
  82. for (i = 0; i < num_clks; i++) {
  83. if (!rclks[i]) {
  84. clks[i] = ERR_PTR(-ENOENT);
  85. continue;
  86. }
  87. clk = devm_clk_register_regmap(dev, rclks[i]);
  88. if (IS_ERR(clk))
  89. return PTR_ERR(clk);
  90. clks[i] = clk;
  91. }
  92. ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get, data);
  93. if (ret)
  94. return ret;
  95. reset = &cc->reset;
  96. reset->rcdev.of_node = dev->of_node;
  97. reset->rcdev.ops = &qcom_reset_ops;
  98. reset->rcdev.owner = dev->driver->owner;
  99. reset->rcdev.nr_resets = desc->num_resets;
  100. reset->regmap = regmap;
  101. reset->reset_map = desc->resets;
  102. platform_set_drvdata(pdev, &reset->rcdev);
  103. ret = reset_controller_register(&reset->rcdev);
  104. if (ret)
  105. of_clk_del_provider(dev->of_node);
  106. return ret;
  107. }
  108. EXPORT_SYMBOL_GPL(qcom_cc_really_probe);
  109. int qcom_cc_probe(struct platform_device *pdev, const struct qcom_cc_desc *desc)
  110. {
  111. struct regmap *regmap;
  112. regmap = qcom_cc_map(pdev, desc);
  113. if (IS_ERR(regmap))
  114. return PTR_ERR(regmap);
  115. return qcom_cc_really_probe(pdev, desc, regmap);
  116. }
  117. EXPORT_SYMBOL_GPL(qcom_cc_probe);
  118. void qcom_cc_remove(struct platform_device *pdev)
  119. {
  120. of_clk_del_provider(pdev->dev.of_node);
  121. reset_controller_unregister(platform_get_drvdata(pdev));
  122. }
  123. EXPORT_SYMBOL_GPL(qcom_cc_remove);
  124. MODULE_LICENSE("GPL v2");