qcom-cpufreq-kryo.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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;
  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. opp_tables = kcalloc(num_possible_cpus(), sizeof(*opp_tables), GFP_KERNEL);
  117. if (!opp_tables)
  118. return -ENOMEM;
  119. for_each_possible_cpu(cpu) {
  120. cpu_dev = get_cpu_device(cpu);
  121. if (NULL == cpu_dev) {
  122. ret = -ENODEV;
  123. goto free_opp;
  124. }
  125. opp_tables[cpu] = dev_pm_opp_set_supported_hw(cpu_dev,
  126. &versions, 1);
  127. if (IS_ERR(opp_tables[cpu])) {
  128. ret = PTR_ERR(opp_tables[cpu]);
  129. dev_err(cpu_dev, "Failed to set supported hardware\n");
  130. goto free_opp;
  131. }
  132. }
  133. cpufreq_dt_pdev = platform_device_register_simple("cpufreq-dt", -1,
  134. NULL, 0);
  135. if (!IS_ERR(cpufreq_dt_pdev)) {
  136. platform_set_drvdata(pdev, opp_tables);
  137. return 0;
  138. }
  139. ret = PTR_ERR(cpufreq_dt_pdev);
  140. dev_err(cpu_dev, "Failed to register platform device\n");
  141. free_opp:
  142. for_each_possible_cpu(cpu) {
  143. if (IS_ERR_OR_NULL(opp_tables[cpu]))
  144. break;
  145. dev_pm_opp_put_supported_hw(opp_tables[cpu]);
  146. }
  147. kfree(opp_tables);
  148. return ret;
  149. }
  150. static int qcom_cpufreq_kryo_remove(struct platform_device *pdev)
  151. {
  152. struct opp_table **opp_tables = platform_get_drvdata(pdev);
  153. unsigned int cpu;
  154. platform_device_unregister(cpufreq_dt_pdev);
  155. for_each_possible_cpu(cpu)
  156. dev_pm_opp_put_supported_hw(opp_tables[cpu]);
  157. kfree(opp_tables);
  158. return 0;
  159. }
  160. static struct platform_driver qcom_cpufreq_kryo_driver = {
  161. .probe = qcom_cpufreq_kryo_probe,
  162. .remove = qcom_cpufreq_kryo_remove,
  163. .driver = {
  164. .name = "qcom-cpufreq-kryo",
  165. },
  166. };
  167. static const struct of_device_id qcom_cpufreq_kryo_match_list[] __initconst = {
  168. { .compatible = "qcom,apq8096", },
  169. { .compatible = "qcom,msm8996", },
  170. {}
  171. };
  172. /*
  173. * Since the driver depends on smem and nvmem drivers, which may
  174. * return EPROBE_DEFER, all the real activity is done in the probe,
  175. * which may be defered as well. The init here is only registering
  176. * the driver and the platform device.
  177. */
  178. static int __init qcom_cpufreq_kryo_init(void)
  179. {
  180. struct device_node *np = of_find_node_by_path("/");
  181. const struct of_device_id *match;
  182. int ret;
  183. if (!np)
  184. return -ENODEV;
  185. match = of_match_node(qcom_cpufreq_kryo_match_list, np);
  186. of_node_put(np);
  187. if (!match)
  188. return -ENODEV;
  189. ret = platform_driver_register(&qcom_cpufreq_kryo_driver);
  190. if (unlikely(ret < 0))
  191. return ret;
  192. kryo_cpufreq_pdev = platform_device_register_simple(
  193. "qcom-cpufreq-kryo", -1, NULL, 0);
  194. ret = PTR_ERR_OR_ZERO(kryo_cpufreq_pdev);
  195. if (0 == ret)
  196. return 0;
  197. platform_driver_unregister(&qcom_cpufreq_kryo_driver);
  198. return ret;
  199. }
  200. module_init(qcom_cpufreq_kryo_init);
  201. static void __exit qcom_cpufreq_kryo_exit(void)
  202. {
  203. platform_device_unregister(kryo_cpufreq_pdev);
  204. platform_driver_unregister(&qcom_cpufreq_kryo_driver);
  205. }
  206. module_exit(qcom_cpufreq_kryo_exit);
  207. MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Kryo CPUfreq driver");
  208. MODULE_LICENSE("GPL v2");