exynos-asv.c 3.7 KB

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