a53-pll.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Qualcomm A53 PLL driver
  4. *
  5. * Copyright (c) 2017, Linaro Limited
  6. * Author: Georgi Djakov <georgi.djakov@linaro.org>
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/clk-provider.h>
  10. #include <linux/kernel.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/pm_opp.h>
  13. #include <linux/regmap.h>
  14. #include <linux/module.h>
  15. #include "clk-pll.h"
  16. #include "clk-regmap.h"
  17. static const struct pll_freq_tbl a53pll_freq[] = {
  18. { 998400000, 52, 0x0, 0x1, 0 },
  19. { 1094400000, 57, 0x0, 0x1, 0 },
  20. { 1152000000, 62, 0x0, 0x1, 0 },
  21. { 1209600000, 63, 0x0, 0x1, 0 },
  22. { 1248000000, 65, 0x0, 0x1, 0 },
  23. { 1363200000, 71, 0x0, 0x1, 0 },
  24. { 1401600000, 73, 0x0, 0x1, 0 },
  25. { }
  26. };
  27. static const struct regmap_config a53pll_regmap_config = {
  28. .reg_bits = 32,
  29. .reg_stride = 4,
  30. .val_bits = 32,
  31. .max_register = 0x40,
  32. };
  33. static struct pll_freq_tbl *qcom_a53pll_get_freq_tbl(struct device *dev)
  34. {
  35. struct pll_freq_tbl *freq_tbl;
  36. unsigned long xo_freq;
  37. unsigned long freq;
  38. struct clk *xo_clk;
  39. int count;
  40. int ret;
  41. int i;
  42. xo_clk = devm_clk_get(dev, "xo");
  43. if (IS_ERR(xo_clk))
  44. return NULL;
  45. xo_freq = clk_get_rate(xo_clk);
  46. ret = devm_pm_opp_of_add_table(dev);
  47. if (ret)
  48. return NULL;
  49. count = dev_pm_opp_get_opp_count(dev);
  50. if (count <= 0)
  51. return NULL;
  52. freq_tbl = devm_kcalloc(dev, count + 1, sizeof(*freq_tbl), GFP_KERNEL);
  53. if (!freq_tbl)
  54. return NULL;
  55. for (i = 0, freq = 0; i < count; i++, freq++) {
  56. struct dev_pm_opp *opp;
  57. opp = dev_pm_opp_find_freq_ceil(dev, &freq);
  58. if (IS_ERR(opp))
  59. return NULL;
  60. /* Skip the freq that is not divisible */
  61. if (freq % xo_freq)
  62. continue;
  63. freq_tbl[i].freq = freq;
  64. freq_tbl[i].l = freq / xo_freq;
  65. freq_tbl[i].n = 1;
  66. dev_pm_opp_put(opp);
  67. }
  68. return freq_tbl;
  69. }
  70. static int qcom_a53pll_probe(struct platform_device *pdev)
  71. {
  72. struct device *dev = &pdev->dev;
  73. struct device_node *np = dev->of_node;
  74. struct regmap *regmap;
  75. struct clk_pll *pll;
  76. void __iomem *base;
  77. struct clk_init_data init = { };
  78. int ret;
  79. pll = devm_kzalloc(dev, sizeof(*pll), GFP_KERNEL);
  80. if (!pll)
  81. return -ENOMEM;
  82. base = devm_platform_ioremap_resource(pdev, 0);
  83. if (IS_ERR(base))
  84. return PTR_ERR(base);
  85. regmap = devm_regmap_init_mmio(dev, base, &a53pll_regmap_config);
  86. if (IS_ERR(regmap))
  87. return PTR_ERR(regmap);
  88. pll->l_reg = 0x04;
  89. pll->m_reg = 0x08;
  90. pll->n_reg = 0x0c;
  91. pll->config_reg = 0x14;
  92. pll->mode_reg = 0x00;
  93. pll->status_reg = 0x1c;
  94. pll->status_bit = 16;
  95. pll->freq_tbl = qcom_a53pll_get_freq_tbl(dev);
  96. if (!pll->freq_tbl) {
  97. /* Fall on a53pll_freq if no freq_tbl is found from OPP */
  98. pll->freq_tbl = a53pll_freq;
  99. }
  100. /* Use an unique name by appending @unit-address */
  101. init.name = devm_kasprintf(dev, GFP_KERNEL, "a53pll%s",
  102. strchrnul(np->full_name, '@'));
  103. if (!init.name)
  104. return -ENOMEM;
  105. init.parent_data = &(const struct clk_parent_data){
  106. .fw_name = "xo", .name = "xo_board",
  107. };
  108. init.num_parents = 1;
  109. init.ops = &clk_pll_sr2_ops;
  110. pll->clkr.hw.init = &init;
  111. ret = devm_clk_register_regmap(dev, &pll->clkr);
  112. if (ret) {
  113. dev_err(dev, "failed to register regmap clock: %d\n", ret);
  114. return ret;
  115. }
  116. ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get,
  117. &pll->clkr.hw);
  118. if (ret) {
  119. dev_err(dev, "failed to add clock provider: %d\n", ret);
  120. return ret;
  121. }
  122. return 0;
  123. }
  124. static const struct of_device_id qcom_a53pll_match_table[] = {
  125. { .compatible = "qcom,msm8226-a7pll" },
  126. { .compatible = "qcom,msm8916-a53pll" },
  127. { .compatible = "qcom,msm8939-a53pll" },
  128. { }
  129. };
  130. MODULE_DEVICE_TABLE(of, qcom_a53pll_match_table);
  131. static struct platform_driver qcom_a53pll_driver = {
  132. .probe = qcom_a53pll_probe,
  133. .driver = {
  134. .name = "qcom-a53pll",
  135. .of_match_table = qcom_a53pll_match_table,
  136. },
  137. };
  138. module_platform_driver(qcom_a53pll_driver);
  139. MODULE_DESCRIPTION("Qualcomm A53 PLL Driver");
  140. MODULE_LICENSE("GPL v2");