armada-8k-cpufreq.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * CPUFreq support for Armada 8K
  4. *
  5. * Copyright (C) 2018 Marvell
  6. *
  7. * Omri Itach <omrii@marvell.com>
  8. * Gregory Clement <gregory.clement@bootlin.com>
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/clk.h>
  12. #include <linux/cpu.h>
  13. #include <linux/err.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pm_opp.h>
  20. #include <linux/slab.h>
  21. static const struct of_device_id __maybe_unused armada_8k_cpufreq_of_match[] = {
  22. { .compatible = "marvell,ap806-cpu-clock" },
  23. { .compatible = "marvell,ap807-cpu-clock" },
  24. { },
  25. };
  26. MODULE_DEVICE_TABLE(of, armada_8k_cpufreq_of_match);
  27. /*
  28. * Setup the opps list with the divider for the max frequency, that
  29. * will be filled at runtime.
  30. */
  31. static const int opps_div[] __initconst = {1, 2, 3, 4};
  32. static struct platform_device *armada_8k_pdev;
  33. struct freq_table {
  34. struct device *cpu_dev;
  35. unsigned int freq[ARRAY_SIZE(opps_div)];
  36. };
  37. /* If the CPUs share the same clock, then they are in the same cluster. */
  38. static void __init armada_8k_get_sharing_cpus(struct clk *cur_clk,
  39. struct cpumask *cpumask)
  40. {
  41. int cpu;
  42. for_each_possible_cpu(cpu) {
  43. struct device *cpu_dev;
  44. struct clk *clk;
  45. cpu_dev = get_cpu_device(cpu);
  46. if (!cpu_dev) {
  47. pr_warn("Failed to get cpu%d device\n", cpu);
  48. continue;
  49. }
  50. clk = clk_get(cpu_dev, NULL);
  51. if (IS_ERR(clk)) {
  52. pr_warn("Cannot get clock for CPU %d\n", cpu);
  53. } else {
  54. if (clk_is_match(clk, cur_clk))
  55. cpumask_set_cpu(cpu, cpumask);
  56. clk_put(clk);
  57. }
  58. }
  59. }
  60. static int __init armada_8k_add_opp(struct clk *clk, struct device *cpu_dev,
  61. struct freq_table *freq_tables,
  62. int opps_index)
  63. {
  64. unsigned int cur_frequency;
  65. unsigned int freq;
  66. int i, ret;
  67. /* Get nominal (current) CPU frequency. */
  68. cur_frequency = clk_get_rate(clk);
  69. if (!cur_frequency) {
  70. dev_err(cpu_dev, "Failed to get clock rate for this CPU\n");
  71. return -EINVAL;
  72. }
  73. freq_tables[opps_index].cpu_dev = cpu_dev;
  74. for (i = 0; i < ARRAY_SIZE(opps_div); i++) {
  75. freq = cur_frequency / opps_div[i];
  76. ret = dev_pm_opp_add(cpu_dev, freq, 0);
  77. if (ret)
  78. return ret;
  79. freq_tables[opps_index].freq[i] = freq;
  80. }
  81. return 0;
  82. }
  83. static void armada_8k_cpufreq_free_table(struct freq_table *freq_tables)
  84. {
  85. int opps_index, nb_cpus = num_possible_cpus();
  86. for (opps_index = 0 ; opps_index <= nb_cpus; opps_index++) {
  87. int i;
  88. /* If cpu_dev is NULL then we reached the end of the array */
  89. if (!freq_tables[opps_index].cpu_dev)
  90. break;
  91. for (i = 0; i < ARRAY_SIZE(opps_div); i++) {
  92. /*
  93. * A 0Hz frequency is not valid, this meant
  94. * that it was not yet initialized so there is
  95. * no more opp to free
  96. */
  97. if (freq_tables[opps_index].freq[i] == 0)
  98. break;
  99. dev_pm_opp_remove(freq_tables[opps_index].cpu_dev,
  100. freq_tables[opps_index].freq[i]);
  101. }
  102. }
  103. kfree(freq_tables);
  104. }
  105. static int __init armada_8k_cpufreq_init(void)
  106. {
  107. int ret = 0, opps_index = 0, cpu, nb_cpus;
  108. struct freq_table *freq_tables;
  109. struct device_node *node;
  110. static struct cpumask cpus;
  111. node = of_find_matching_node_and_match(NULL, armada_8k_cpufreq_of_match,
  112. NULL);
  113. if (!node || !of_device_is_available(node)) {
  114. of_node_put(node);
  115. return -ENODEV;
  116. }
  117. of_node_put(node);
  118. nb_cpus = num_possible_cpus();
  119. freq_tables = kcalloc(nb_cpus, sizeof(*freq_tables), GFP_KERNEL);
  120. if (!freq_tables)
  121. return -ENOMEM;
  122. cpumask_copy(&cpus, cpu_possible_mask);
  123. /*
  124. * For each CPU, this loop registers the operating points
  125. * supported (which are the nominal CPU frequency and full integer
  126. * divisions of it).
  127. */
  128. for_each_cpu(cpu, &cpus) {
  129. struct cpumask shared_cpus;
  130. struct device *cpu_dev;
  131. struct clk *clk;
  132. cpu_dev = get_cpu_device(cpu);
  133. if (!cpu_dev) {
  134. pr_err("Cannot get CPU %d\n", cpu);
  135. continue;
  136. }
  137. clk = clk_get(cpu_dev, NULL);
  138. if (IS_ERR(clk)) {
  139. pr_err("Cannot get clock for CPU %d\n", cpu);
  140. ret = PTR_ERR(clk);
  141. goto remove_opp;
  142. }
  143. ret = armada_8k_add_opp(clk, cpu_dev, freq_tables, opps_index);
  144. if (ret) {
  145. clk_put(clk);
  146. goto remove_opp;
  147. }
  148. opps_index++;
  149. cpumask_clear(&shared_cpus);
  150. armada_8k_get_sharing_cpus(clk, &shared_cpus);
  151. dev_pm_opp_set_sharing_cpus(cpu_dev, &shared_cpus);
  152. cpumask_andnot(&cpus, &cpus, &shared_cpus);
  153. clk_put(clk);
  154. }
  155. armada_8k_pdev = platform_device_register_simple("cpufreq-dt", -1,
  156. NULL, 0);
  157. ret = PTR_ERR_OR_ZERO(armada_8k_pdev);
  158. if (ret)
  159. goto remove_opp;
  160. platform_set_drvdata(armada_8k_pdev, freq_tables);
  161. return 0;
  162. remove_opp:
  163. armada_8k_cpufreq_free_table(freq_tables);
  164. return ret;
  165. }
  166. module_init(armada_8k_cpufreq_init);
  167. static void __exit armada_8k_cpufreq_exit(void)
  168. {
  169. struct freq_table *freq_tables = platform_get_drvdata(armada_8k_pdev);
  170. platform_device_unregister(armada_8k_pdev);
  171. armada_8k_cpufreq_free_table(freq_tables);
  172. }
  173. module_exit(armada_8k_cpufreq_exit);
  174. MODULE_AUTHOR("Gregory Clement <gregory.clement@bootlin.com>");
  175. MODULE_DESCRIPTION("Armada 8K cpufreq driver");
  176. MODULE_LICENSE("GPL");