qcom-cpufreq-kryo.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2018, The Linux Foundation. All rights reserved.
  4. */
  5. /*
  6. * In Certain QCOM SoCs like apq8096 and msm8996 that have KRYO processors,
  7. * the CPU frequency subset and voltage value of each OPP varies
  8. * based on the silicon variant in use. Qualcomm Process Voltage Scaling Tables
  9. * defines the voltage and frequency value based on the msm-id in SMEM
  10. * and speedbin blown in the efuse combination.
  11. * The qcom-cpufreq-kryo driver reads the msm-id and efuse value from the SoC
  12. * to provide the OPP framework with required information.
  13. * This is used to determine the voltage and frequency value for each OPP of
  14. * operating-points-v2 table when it is parsed by the OPP framework.
  15. */
  16. #include <linux/cpu.h>
  17. #include <linux/err.h>
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/nvmem-consumer.h>
  22. #include <linux/of.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pm_opp.h>
  25. #include <linux/slab.h>
  26. #include <linux/soc/qcom/smem.h>
  27. #define MSM_ID_SMEM 137
  28. enum _msm_id {
  29. MSM8996V3 = 0xF6ul,
  30. APQ8096V3 = 0x123ul,
  31. MSM8996SG = 0x131ul,
  32. APQ8096SG = 0x138ul,
  33. };
  34. enum _msm8996_version {
  35. MSM8996_V3,
  36. MSM8996_SG,
  37. NUM_OF_MSM8996_VERSIONS,
  38. };
  39. static enum _msm8996_version __init qcom_cpufreq_kryo_get_msm_id(void)
  40. {
  41. size_t len;
  42. u32 *msm_id;
  43. enum _msm8996_version version;
  44. msm_id = qcom_smem_get(QCOM_SMEM_HOST_ANY, MSM_ID_SMEM, &len);
  45. if (IS_ERR(msm_id))
  46. return NUM_OF_MSM8996_VERSIONS;
  47. /* The first 4 bytes are format, next to them is the actual msm-id */
  48. msm_id++;
  49. switch ((enum _msm_id)*msm_id) {
  50. case MSM8996V3:
  51. case APQ8096V3:
  52. version = MSM8996_V3;
  53. break;
  54. case MSM8996SG:
  55. case APQ8096SG:
  56. version = MSM8996_SG;
  57. break;
  58. default:
  59. version = NUM_OF_MSM8996_VERSIONS;
  60. }
  61. return version;
  62. }
  63. static int qcom_cpufreq_kryo_probe(struct platform_device *pdev)
  64. {
  65. struct opp_table *opp_tables[NR_CPUS] = {0};
  66. struct platform_device *cpufreq_dt_pdev;
  67. enum _msm8996_version msm8996_version;
  68. struct nvmem_cell *speedbin_nvmem;
  69. struct device_node *np;
  70. struct device *cpu_dev;
  71. unsigned cpu;
  72. u8 *speedbin;
  73. u32 versions;
  74. size_t len;
  75. int ret;
  76. cpu_dev = get_cpu_device(0);
  77. if (NULL == cpu_dev)
  78. ret = -ENODEV;
  79. msm8996_version = qcom_cpufreq_kryo_get_msm_id();
  80. if (NUM_OF_MSM8996_VERSIONS == msm8996_version) {
  81. dev_err(cpu_dev, "Not Snapdragon 820/821!");
  82. return -ENODEV;
  83. }
  84. np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
  85. if (IS_ERR(np))
  86. return PTR_ERR(np);
  87. ret = of_device_is_compatible(np, "operating-points-v2-kryo-cpu");
  88. if (!ret) {
  89. of_node_put(np);
  90. return -ENOENT;
  91. }
  92. speedbin_nvmem = of_nvmem_cell_get(np, NULL);
  93. of_node_put(np);
  94. if (IS_ERR(speedbin_nvmem)) {
  95. dev_err(cpu_dev, "Could not get nvmem cell: %ld\n",
  96. PTR_ERR(speedbin_nvmem));
  97. return PTR_ERR(speedbin_nvmem);
  98. }
  99. speedbin = nvmem_cell_read(speedbin_nvmem, &len);
  100. nvmem_cell_put(speedbin_nvmem);
  101. switch (msm8996_version) {
  102. case MSM8996_V3:
  103. versions = 1 << (unsigned int)(*speedbin);
  104. break;
  105. case MSM8996_SG:
  106. versions = 1 << ((unsigned int)(*speedbin) + 4);
  107. break;
  108. default:
  109. BUG();
  110. break;
  111. }
  112. for_each_possible_cpu(cpu) {
  113. cpu_dev = get_cpu_device(cpu);
  114. if (NULL == cpu_dev) {
  115. ret = -ENODEV;
  116. goto free_opp;
  117. }
  118. opp_tables[cpu] = dev_pm_opp_set_supported_hw(cpu_dev,
  119. &versions, 1);
  120. if (IS_ERR(opp_tables[cpu])) {
  121. ret = PTR_ERR(opp_tables[cpu]);
  122. dev_err(cpu_dev, "Failed to set supported hardware\n");
  123. goto free_opp;
  124. }
  125. }
  126. cpufreq_dt_pdev = platform_device_register_simple("cpufreq-dt", -1,
  127. NULL, 0);
  128. if (!IS_ERR(cpufreq_dt_pdev))
  129. return 0;
  130. ret = PTR_ERR(cpufreq_dt_pdev);
  131. dev_err(cpu_dev, "Failed to register platform device\n");
  132. free_opp:
  133. for_each_possible_cpu(cpu) {
  134. if (IS_ERR_OR_NULL(opp_tables[cpu]))
  135. break;
  136. dev_pm_opp_put_supported_hw(opp_tables[cpu]);
  137. }
  138. return ret;
  139. }
  140. static struct platform_driver qcom_cpufreq_kryo_driver = {
  141. .probe = qcom_cpufreq_kryo_probe,
  142. .driver = {
  143. .name = "qcom-cpufreq-kryo",
  144. },
  145. };
  146. static const struct of_device_id qcom_cpufreq_kryo_match_list[] __initconst = {
  147. { .compatible = "qcom,apq8096", },
  148. { .compatible = "qcom,msm8996", },
  149. };
  150. /*
  151. * Since the driver depends on smem and nvmem drivers, which may
  152. * return EPROBE_DEFER, all the real activity is done in the probe,
  153. * which may be defered as well. The init here is only registering
  154. * the driver and the platform device.
  155. */
  156. static int __init qcom_cpufreq_kryo_init(void)
  157. {
  158. struct device_node *np = of_find_node_by_path("/");
  159. const struct of_device_id *match;
  160. int ret;
  161. if (!np)
  162. return -ENODEV;
  163. match = of_match_node(qcom_cpufreq_kryo_match_list, np);
  164. of_node_put(np);
  165. if (!match)
  166. return -ENODEV;
  167. ret = platform_driver_register(&qcom_cpufreq_kryo_driver);
  168. if (unlikely(ret < 0))
  169. return ret;
  170. ret = PTR_ERR_OR_ZERO(platform_device_register_simple(
  171. "qcom-cpufreq-kryo", -1, NULL, 0));
  172. if (0 == ret)
  173. return 0;
  174. platform_driver_unregister(&qcom_cpufreq_kryo_driver);
  175. return ret;
  176. }
  177. module_init(qcom_cpufreq_kryo_init);
  178. MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Kryo CPUfreq driver");
  179. MODULE_LICENSE("GPL v2");