common.c 3.3 KB

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