exynos-regulator-coupler.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2020 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com/
  5. * Author: Marek Szyprowski <m.szyprowski@samsung.com>
  6. *
  7. * Simplified generic voltage coupler from regulator core.c
  8. * The main difference is that it keeps current regulator voltage
  9. * if consumers didn't apply their constraints yet.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/of.h>
  14. #include <linux/regulator/coupler.h>
  15. #include <linux/regulator/driver.h>
  16. #include <linux/regulator/machine.h>
  17. static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
  18. int *current_uV,
  19. int *min_uV, int *max_uV,
  20. suspend_state_t state)
  21. {
  22. struct coupling_desc *c_desc = &rdev->coupling_desc;
  23. struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
  24. struct regulation_constraints *constraints = rdev->constraints;
  25. int desired_min_uV = 0, desired_max_uV = INT_MAX;
  26. int max_current_uV = 0, min_current_uV = INT_MAX;
  27. int highest_min_uV = 0, target_uV, possible_uV;
  28. int i, ret, max_spread, n_coupled = c_desc->n_coupled;
  29. bool done;
  30. *current_uV = -1;
  31. /* Find highest min desired voltage */
  32. for (i = 0; i < n_coupled; i++) {
  33. int tmp_min = 0;
  34. int tmp_max = INT_MAX;
  35. lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
  36. ret = regulator_check_consumers(c_rdevs[i],
  37. &tmp_min,
  38. &tmp_max, state);
  39. if (ret < 0)
  40. return ret;
  41. if (tmp_min == 0) {
  42. ret = regulator_get_voltage_rdev(c_rdevs[i]);
  43. if (ret < 0)
  44. return ret;
  45. tmp_min = ret;
  46. }
  47. /* apply constraints */
  48. ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
  49. if (ret < 0)
  50. return ret;
  51. highest_min_uV = max(highest_min_uV, tmp_min);
  52. if (i == 0) {
  53. desired_min_uV = tmp_min;
  54. desired_max_uV = tmp_max;
  55. }
  56. }
  57. max_spread = constraints->max_spread[0];
  58. /*
  59. * Let target_uV be equal to the desired one if possible.
  60. * If not, set it to minimum voltage, allowed by other coupled
  61. * regulators.
  62. */
  63. target_uV = max(desired_min_uV, highest_min_uV - max_spread);
  64. /*
  65. * Find min and max voltages, which currently aren't violating
  66. * max_spread.
  67. */
  68. for (i = 1; i < n_coupled; i++) {
  69. int tmp_act;
  70. tmp_act = regulator_get_voltage_rdev(c_rdevs[i]);
  71. if (tmp_act < 0)
  72. return tmp_act;
  73. min_current_uV = min(tmp_act, min_current_uV);
  74. max_current_uV = max(tmp_act, max_current_uV);
  75. }
  76. /*
  77. * Correct target voltage, so as it currently isn't
  78. * violating max_spread
  79. */
  80. possible_uV = max(target_uV, max_current_uV - max_spread);
  81. possible_uV = min(possible_uV, min_current_uV + max_spread);
  82. if (possible_uV > desired_max_uV)
  83. return -EINVAL;
  84. done = (possible_uV == target_uV);
  85. desired_min_uV = possible_uV;
  86. /* Set current_uV if wasn't done earlier in the code and if necessary */
  87. if (*current_uV == -1) {
  88. ret = regulator_get_voltage_rdev(rdev);
  89. if (ret < 0)
  90. return ret;
  91. *current_uV = ret;
  92. }
  93. *min_uV = desired_min_uV;
  94. *max_uV = desired_max_uV;
  95. return done;
  96. }
  97. static int exynos_coupler_balance_voltage(struct regulator_coupler *coupler,
  98. struct regulator_dev *rdev,
  99. suspend_state_t state)
  100. {
  101. struct regulator_dev **c_rdevs;
  102. struct regulator_dev *best_rdev;
  103. struct coupling_desc *c_desc = &rdev->coupling_desc;
  104. int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
  105. unsigned int delta, best_delta;
  106. unsigned long c_rdev_done = 0;
  107. bool best_c_rdev_done;
  108. c_rdevs = c_desc->coupled_rdevs;
  109. n_coupled = c_desc->n_coupled;
  110. /*
  111. * Find the best possible voltage change on each loop. Leave the loop
  112. * if there isn't any possible change.
  113. */
  114. do {
  115. best_c_rdev_done = false;
  116. best_delta = 0;
  117. best_min_uV = 0;
  118. best_max_uV = 0;
  119. best_c_rdev = 0;
  120. best_rdev = NULL;
  121. /*
  122. * Find highest difference between optimal voltage
  123. * and current voltage.
  124. */
  125. for (i = 0; i < n_coupled; i++) {
  126. /*
  127. * optimal_uV is the best voltage that can be set for
  128. * i-th regulator at the moment without violating
  129. * max_spread constraint in order to balance
  130. * the coupled voltages.
  131. */
  132. int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
  133. if (test_bit(i, &c_rdev_done))
  134. continue;
  135. ret = regulator_get_optimal_voltage(c_rdevs[i],
  136. &current_uV,
  137. &optimal_uV,
  138. &optimal_max_uV,
  139. state);
  140. if (ret < 0)
  141. goto out;
  142. delta = abs(optimal_uV - current_uV);
  143. if (delta && best_delta <= delta) {
  144. best_c_rdev_done = ret;
  145. best_delta = delta;
  146. best_rdev = c_rdevs[i];
  147. best_min_uV = optimal_uV;
  148. best_max_uV = optimal_max_uV;
  149. best_c_rdev = i;
  150. }
  151. }
  152. /* Nothing to change, return successfully */
  153. if (!best_rdev) {
  154. ret = 0;
  155. goto out;
  156. }
  157. ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
  158. best_max_uV, state);
  159. if (ret < 0)
  160. goto out;
  161. if (best_c_rdev_done)
  162. set_bit(best_c_rdev, &c_rdev_done);
  163. } while (n_coupled > 1);
  164. out:
  165. return ret;
  166. }
  167. static int exynos_coupler_attach(struct regulator_coupler *coupler,
  168. struct regulator_dev *rdev)
  169. {
  170. return 0;
  171. }
  172. static struct regulator_coupler exynos_coupler = {
  173. .attach_regulator = exynos_coupler_attach,
  174. .balance_voltage = exynos_coupler_balance_voltage,
  175. };
  176. static int __init exynos_coupler_init(void)
  177. {
  178. if (!of_machine_is_compatible("samsung,exynos5800"))
  179. return 0;
  180. return regulator_coupler_register(&exynos_coupler);
  181. }
  182. arch_initcall(exynos_coupler_init);