common.c 3.0 KB

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