qcom-cpufreq-kryo.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. struct platform_device *cpufreq_dt_pdev, *kryo_cpufreq_pdev;
  40. static enum _msm8996_version qcom_cpufreq_kryo_get_msm_id(void)
  41. {
  42. size_t len;
  43. u32 *msm_id;
  44. enum _msm8996_version version;
  45. msm_id = qcom_smem_get(QCOM_SMEM_HOST_ANY, MSM_ID_SMEM, &len);
  46. if (IS_ERR(msm_id))
  47. return NUM_OF_MSM8996_VERSIONS;
  48. /* The first 4 bytes are format, next to them is the actual msm-id */
  49. msm_id++;
  50. switch ((enum _msm_id)*msm_id) {
  51. case MSM8996V3:
  52. case APQ8096V3:
  53. version = MSM8996_V3;
  54. break;
  55. case MSM8996SG:
  56. case APQ8096SG:
  57. version = MSM8996_SG;
  58. break;
  59. default:
  60. version = NUM_OF_MSM8996_VERSIONS;
  61. }
  62. return version;
  63. }
  64. static int qcom_cpufreq_kryo_probe(struct platform_device *pdev)
  65. {
  66. struct opp_table *opp_tables[NR_CPUS] = {0};
  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 (!cpu_dev)
  78. return -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 (!np)
  86. return -ENOENT;
  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. if (PTR_ERR(speedbin_nvmem) != -EPROBE_DEFER)
  96. dev_err(cpu_dev, "Could not get nvmem cell: %ld\n",
  97. PTR_ERR(speedbin_nvmem));
  98. return PTR_ERR(speedbin_nvmem);
  99. }
  100. speedbin = nvmem_cell_read(speedbin_nvmem, &len);
  101. nvmem_cell_put(speedbin_nvmem);
  102. if (IS_ERR(speedbin))
  103. return PTR_ERR(speedbin);
  104. switch (msm8996_version) {
  105. case MSM8996_V3:
  106. versions = 1 << (unsigned int)(*speedbin);
  107. break;
  108. case MSM8996_SG:
  109. versions = 1 << ((unsigned int)(*speedbin) + 4);
  110. break;
  111. default:
  112. BUG();
  113. break;
  114. }
  115. kfree(speedbin);
  116. for_each_possible_cpu(cpu) {
  117. cpu_dev = get_cpu_device(cpu);
  118. if (NULL == cpu_dev) {
  119. ret = -ENODEV;
  120. goto free_opp;
  121. }
  122. opp_tables[cpu] = dev_pm_opp_set_supported_hw(cpu_dev,
  123. &versions, 1);
  124. if (IS_ERR(opp_tables[cpu])) {
  125. ret = PTR_ERR(opp_tables[cpu]);
  126. dev_err(cpu_dev, "Failed to set supported hardware\n");
  127. goto free_opp;
  128. }
  129. }
  130. cpufreq_dt_pdev = platform_device_register_simple("cpufreq-dt", -1,
  131. NULL, 0);
  132. if (!IS_ERR(cpufreq_dt_pdev))
  133. return 0;
  134. ret = PTR_ERR(cpufreq_dt_pdev);
  135. dev_err(cpu_dev, "Failed to register platform device\n");
  136. free_opp:
  137. for_each_possible_cpu(cpu) {
  138. if (IS_ERR_OR_NULL(opp_tables[cpu]))
  139. break;
  140. dev_pm_opp_put_supported_hw(opp_tables[cpu]);
  141. }
  142. return ret;
  143. }
  144. static int qcom_cpufreq_kryo_remove(struct platform_device *pdev)
  145. {
  146. platform_device_unregister(cpufreq_dt_pdev);
  147. return 0;
  148. }
  149. static struct platform_driver qcom_cpufreq_kryo_driver = {
  150. .probe = qcom_cpufreq_kryo_probe,
  151. .remove = qcom_cpufreq_kryo_remove,
  152. .driver = {
  153. .name = "qcom-cpufreq-kryo",
  154. },
  155. };
  156. static const struct of_device_id qcom_cpufreq_kryo_match_list[] __initconst = {
  157. { .compatible = "qcom,apq8096", },
  158. { .compatible = "qcom,msm8996", },
  159. {}
  160. };
  161. /*
  162. * Since the driver depends on smem and nvmem drivers, which may
  163. * return EPROBE_DEFER, all the real activity is done in the probe,
  164. * which may be defered as well. The init here is only registering
  165. * the driver and the platform device.
  166. */
  167. static int __init qcom_cpufreq_kryo_init(void)
  168. {
  169. struct device_node *np = of_find_node_by_path("/");
  170. const struct of_device_id *match;
  171. int ret;
  172. if (!np)
  173. return -ENODEV;
  174. match = of_match_node(qcom_cpufreq_kryo_match_list, np);
  175. of_node_put(np);
  176. if (!match)
  177. return -ENODEV;
  178. ret = platform_driver_register(&qcom_cpufreq_kryo_driver);
  179. if (unlikely(ret < 0))
  180. return ret;
  181. kryo_cpufreq_pdev = platform_device_register_simple(
  182. "qcom-cpufreq-kryo", -1, NULL, 0);
  183. ret = PTR_ERR_OR_ZERO(kryo_cpufreq_pdev);
  184. if (0 == ret)
  185. return 0;
  186. platform_driver_unregister(&qcom_cpufreq_kryo_driver);
  187. return ret;
  188. }
  189. module_init(qcom_cpufreq_kryo_init);
  190. static void __exit qcom_cpufreq_kryo_exit(void)
  191. {
  192. platform_device_unregister(kryo_cpufreq_pdev);
  193. platform_driver_unregister(&qcom_cpufreq_kryo_driver);
  194. }
  195. module_exit(qcom_cpufreq_kryo_exit);
  196. MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Kryo CPUfreq driver");
  197. MODULE_LICENSE("GPL v2");