exynos-asv.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2019 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com/
  5. * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
  6. *
  7. * Samsung Exynos SoC Adaptive Supply Voltage support
  8. */
  9. #include <linux/cpu.h>
  10. #include <linux/device.h>
  11. #include <linux/errno.h>
  12. #include <linux/init.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pm_opp.h>
  19. #include <linux/regmap.h>
  20. #include <linux/soc/samsung/exynos-chipid.h>
  21. #include "exynos-asv.h"
  22. #include "exynos5422-asv.h"
  23. #define MHZ 1000000U
  24. static int exynos_asv_update_cpu_opps(struct exynos_asv *asv,
  25. struct device *cpu)
  26. {
  27. struct exynos_asv_subsys *subsys = NULL;
  28. struct dev_pm_opp *opp;
  29. unsigned int opp_freq;
  30. int i;
  31. for (i = 0; i < ARRAY_SIZE(asv->subsys); i++) {
  32. if (of_device_is_compatible(cpu->of_node,
  33. asv->subsys[i].cpu_dt_compat)) {
  34. subsys = &asv->subsys[i];
  35. break;
  36. }
  37. }
  38. if (!subsys)
  39. return -EINVAL;
  40. for (i = 0; i < subsys->table.num_rows; i++) {
  41. unsigned int new_volt, volt;
  42. int ret;
  43. opp_freq = exynos_asv_opp_get_frequency(subsys, i);
  44. opp = dev_pm_opp_find_freq_exact(cpu, opp_freq * MHZ, true);
  45. if (IS_ERR(opp)) {
  46. dev_info(asv->dev, "cpu%d opp%d, freq: %u missing\n",
  47. cpu->id, i, opp_freq);
  48. continue;
  49. }
  50. volt = dev_pm_opp_get_voltage(opp);
  51. new_volt = asv->opp_get_voltage(subsys, i, volt);
  52. dev_pm_opp_put(opp);
  53. if (new_volt == volt)
  54. continue;
  55. ret = dev_pm_opp_adjust_voltage(cpu, opp_freq * MHZ,
  56. new_volt, new_volt, new_volt);
  57. if (ret < 0)
  58. dev_err(asv->dev,
  59. "Failed to adjust OPP %u Hz/%u uV for cpu%d\n",
  60. opp_freq, new_volt, cpu->id);
  61. else
  62. dev_dbg(asv->dev,
  63. "Adjusted OPP %u Hz/%u -> %u uV, cpu%d\n",
  64. opp_freq, volt, new_volt, cpu->id);
  65. }
  66. return 0;
  67. }
  68. static int exynos_asv_update_opps(struct exynos_asv *asv)
  69. {
  70. struct opp_table *last_opp_table = NULL;
  71. struct device *cpu;
  72. int ret, cpuid;
  73. for_each_possible_cpu(cpuid) {
  74. struct opp_table *opp_table;
  75. cpu = get_cpu_device(cpuid);
  76. if (!cpu)
  77. continue;
  78. opp_table = dev_pm_opp_get_opp_table(cpu);
  79. if (IS_ERR(opp_table))
  80. continue;
  81. if (!last_opp_table || opp_table != last_opp_table) {
  82. last_opp_table = opp_table;
  83. ret = exynos_asv_update_cpu_opps(asv, cpu);
  84. if (ret < 0)
  85. dev_err(asv->dev, "Couldn't udate OPPs for cpu%d\n",
  86. cpuid);
  87. }
  88. dev_pm_opp_put_opp_table(opp_table);
  89. }
  90. return 0;
  91. }
  92. static int exynos_asv_probe(struct platform_device *pdev)
  93. {
  94. int (*probe_func)(struct exynos_asv *asv);
  95. struct exynos_asv *asv;
  96. struct device *cpu_dev;
  97. u32 product_id = 0;
  98. int ret, i;
  99. asv = devm_kzalloc(&pdev->dev, sizeof(*asv), GFP_KERNEL);
  100. if (!asv)
  101. return -ENOMEM;
  102. asv->chipid_regmap = device_node_to_regmap(pdev->dev.of_node);
  103. if (IS_ERR(asv->chipid_regmap)) {
  104. dev_err(&pdev->dev, "Could not find syscon regmap\n");
  105. return PTR_ERR(asv->chipid_regmap);
  106. }
  107. ret = regmap_read(asv->chipid_regmap, EXYNOS_CHIPID_REG_PRO_ID,
  108. &product_id);
  109. if (ret < 0) {
  110. dev_err(&pdev->dev, "Cannot read revision from ChipID: %d\n",
  111. ret);
  112. return -ENODEV;
  113. }
  114. switch (product_id & EXYNOS_MASK) {
  115. case 0xE5422000:
  116. probe_func = exynos5422_asv_init;
  117. break;
  118. default:
  119. return -ENODEV;
  120. }
  121. cpu_dev = get_cpu_device(0);
  122. ret = dev_pm_opp_get_opp_count(cpu_dev);
  123. if (ret < 0)
  124. return -EPROBE_DEFER;
  125. ret = of_property_read_u32(pdev->dev.of_node, "samsung,asv-bin",
  126. &asv->of_bin);
  127. if (ret < 0)
  128. asv->of_bin = -EINVAL;
  129. asv->dev = &pdev->dev;
  130. dev_set_drvdata(&pdev->dev, asv);
  131. for (i = 0; i < ARRAY_SIZE(asv->subsys); i++)
  132. asv->subsys[i].asv = asv;
  133. ret = probe_func(asv);
  134. if (ret < 0)
  135. return ret;
  136. return exynos_asv_update_opps(asv);
  137. }
  138. static const struct of_device_id exynos_asv_of_device_ids[] = {
  139. { .compatible = "samsung,exynos4210-chipid" },
  140. {}
  141. };
  142. static struct platform_driver exynos_asv_driver = {
  143. .driver = {
  144. .name = "exynos-asv",
  145. .of_match_table = exynos_asv_of_device_ids,
  146. },
  147. .probe = exynos_asv_probe,
  148. };
  149. module_platform_driver(exynos_asv_driver);