a53-pll.c 3.7 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. .fast_io = true,
  33. };
  34. static struct pll_freq_tbl *qcom_a53pll_get_freq_tbl(struct device *dev)
  35. {
  36. struct pll_freq_tbl *freq_tbl;
  37. unsigned long xo_freq;
  38. unsigned long freq;
  39. struct clk *xo_clk;
  40. int count;
  41. int ret;
  42. int i;
  43. xo_clk = devm_clk_get(dev, "xo");
  44. if (IS_ERR(xo_clk))
  45. return NULL;
  46. xo_freq = clk_get_rate(xo_clk);
  47. ret = devm_pm_opp_of_add_table(dev);
  48. if (ret)
  49. return NULL;
  50. count = dev_pm_opp_get_opp_count(dev);
  51. if (count <= 0)
  52. return NULL;
  53. freq_tbl = devm_kcalloc(dev, count + 1, sizeof(*freq_tbl), GFP_KERNEL);
  54. if (!freq_tbl)
  55. return NULL;
  56. for (i = 0, freq = 0; i < count; i++, freq++) {
  57. struct dev_pm_opp *opp;
  58. opp = dev_pm_opp_find_freq_ceil(dev, &freq);
  59. if (IS_ERR(opp))
  60. return NULL;
  61. /* Skip the freq that is not divisible */
  62. if (freq % xo_freq)
  63. continue;
  64. freq_tbl[i].freq = freq;
  65. freq_tbl[i].l = freq / xo_freq;
  66. freq_tbl[i].n = 1;
  67. dev_pm_opp_put(opp);
  68. }
  69. return freq_tbl;
  70. }
  71. static int qcom_a53pll_probe(struct platform_device *pdev)
  72. {
  73. struct device *dev = &pdev->dev;
  74. struct device_node *np = dev->of_node;
  75. struct regmap *regmap;
  76. struct clk_pll *pll;
  77. void __iomem *base;
  78. struct clk_init_data init = { };
  79. int ret;
  80. pll = devm_kzalloc(dev, sizeof(*pll), GFP_KERNEL);
  81. if (!pll)
  82. return -ENOMEM;
  83. base = devm_platform_ioremap_resource(pdev, 0);
  84. if (IS_ERR(base))
  85. return PTR_ERR(base);
  86. regmap = devm_regmap_init_mmio(dev, base, &a53pll_regmap_config);
  87. if (IS_ERR(regmap))
  88. return PTR_ERR(regmap);
  89. pll->l_reg = 0x04;
  90. pll->m_reg = 0x08;
  91. pll->n_reg = 0x0c;
  92. pll->config_reg = 0x14;
  93. pll->mode_reg = 0x00;
  94. pll->status_reg = 0x1c;
  95. pll->status_bit = 16;
  96. pll->freq_tbl = qcom_a53pll_get_freq_tbl(dev);
  97. if (!pll->freq_tbl) {
  98. /* Fall on a53pll_freq if no freq_tbl is found from OPP */
  99. pll->freq_tbl = a53pll_freq;
  100. }
  101. /* Use an unique name by appending @unit-address */
  102. init.name = devm_kasprintf(dev, GFP_KERNEL, "a53pll%s",
  103. strchrnul(np->full_name, '@'));
  104. if (!init.name)
  105. return -ENOMEM;
  106. init.parent_data = &(const struct clk_parent_data){
  107. .fw_name = "xo", .name = "xo_board",
  108. };
  109. init.num_parents = 1;
  110. init.ops = &clk_pll_sr2_ops;
  111. pll->clkr.hw.init = &init;
  112. ret = devm_clk_register_regmap(dev, &pll->clkr);
  113. if (ret) {
  114. dev_err(dev, "failed to register regmap clock: %d\n", ret);
  115. return ret;
  116. }
  117. ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get,
  118. &pll->clkr.hw);
  119. if (ret) {
  120. dev_err(dev, "failed to add clock provider: %d\n", ret);
  121. return ret;
  122. }
  123. return 0;
  124. }
  125. static const struct of_device_id qcom_a53pll_match_table[] = {
  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");