gk20a.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (c) 2014-2016, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. #define gk20a_volt(p) container_of((p), struct gk20a_volt, base)
  23. #include "priv.h"
  24. #include <core/tegra.h>
  25. #include "gk20a.h"
  26. static const struct cvb_coef gk20a_cvb_coef[] = {
  27. /* MHz, c0, c1, c2, c3, c4, c5 */
  28. /* 72 */ { 1209886, -36468, 515, 417, -13123, 203},
  29. /* 108 */ { 1130804, -27659, 296, 298, -10834, 221},
  30. /* 180 */ { 1162871, -27110, 247, 238, -10681, 268},
  31. /* 252 */ { 1220458, -28654, 247, 179, -10376, 298},
  32. /* 324 */ { 1280953, -30204, 247, 119, -9766, 304},
  33. /* 396 */ { 1344547, -31777, 247, 119, -8545, 292},
  34. /* 468 */ { 1420168, -34227, 269, 60, -7172, 256},
  35. /* 540 */ { 1490757, -35955, 274, 60, -5188, 197},
  36. /* 612 */ { 1599112, -42583, 398, 0, -1831, 119},
  37. /* 648 */ { 1366986, -16459, -274, 0, -3204, 72},
  38. /* 684 */ { 1391884, -17078, -274, -60, -1526, 30},
  39. /* 708 */ { 1415522, -17497, -274, -60, -458, 0},
  40. /* 756 */ { 1464061, -18331, -274, -119, 1831, -72},
  41. /* 804 */ { 1524225, -20064, -254, -119, 4272, -155},
  42. /* 852 */ { 1608418, -21643, -269, 0, 763, -48},
  43. };
  44. /**
  45. * cvb_mv = ((c2 * speedo / s_scale + c1) * speedo / s_scale + c0)
  46. */
  47. static inline int
  48. gk20a_volt_get_cvb_voltage(int speedo, int s_scale, const struct cvb_coef *coef)
  49. {
  50. int mv;
  51. mv = DIV_ROUND_CLOSEST(coef->c2 * speedo, s_scale);
  52. mv = DIV_ROUND_CLOSEST((mv + coef->c1) * speedo, s_scale) + coef->c0;
  53. return mv;
  54. }
  55. /**
  56. * cvb_t_mv =
  57. * ((c2 * speedo / s_scale + c1) * speedo / s_scale + c0) +
  58. * ((c3 * speedo / s_scale + c4 + c5 * T / t_scale) * T / t_scale)
  59. */
  60. static inline int
  61. gk20a_volt_get_cvb_t_voltage(int speedo, int temp, int s_scale, int t_scale,
  62. const struct cvb_coef *coef)
  63. {
  64. int cvb_mv, mv;
  65. cvb_mv = gk20a_volt_get_cvb_voltage(speedo, s_scale, coef);
  66. mv = DIV_ROUND_CLOSEST(coef->c3 * speedo, s_scale) + coef->c4 +
  67. DIV_ROUND_CLOSEST(coef->c5 * temp, t_scale);
  68. mv = DIV_ROUND_CLOSEST(mv * temp, t_scale) + cvb_mv;
  69. return mv;
  70. }
  71. static int
  72. gk20a_volt_calc_voltage(const struct cvb_coef *coef, int speedo)
  73. {
  74. static const int v_scale = 1000;
  75. int mv;
  76. mv = gk20a_volt_get_cvb_t_voltage(speedo, -10, 100, 10, coef);
  77. mv = DIV_ROUND_UP(mv, v_scale);
  78. return mv * 1000;
  79. }
  80. static int
  81. gk20a_volt_vid_get(struct nvkm_volt *base)
  82. {
  83. struct gk20a_volt *volt = gk20a_volt(base);
  84. int i, uv;
  85. uv = regulator_get_voltage(volt->vdd);
  86. for (i = 0; i < volt->base.vid_nr; i++)
  87. if (volt->base.vid[i].uv >= uv)
  88. return i;
  89. return -EINVAL;
  90. }
  91. static int
  92. gk20a_volt_vid_set(struct nvkm_volt *base, u8 vid)
  93. {
  94. struct gk20a_volt *volt = gk20a_volt(base);
  95. struct nvkm_subdev *subdev = &volt->base.subdev;
  96. nvkm_debug(subdev, "set voltage as %duv\n", volt->base.vid[vid].uv);
  97. return regulator_set_voltage(volt->vdd, volt->base.vid[vid].uv, 1200000);
  98. }
  99. static int
  100. gk20a_volt_set_id(struct nvkm_volt *base, u8 id, int condition)
  101. {
  102. struct gk20a_volt *volt = gk20a_volt(base);
  103. struct nvkm_subdev *subdev = &volt->base.subdev;
  104. int prev_uv = regulator_get_voltage(volt->vdd);
  105. int target_uv = volt->base.vid[id].uv;
  106. int ret;
  107. nvkm_debug(subdev, "prev=%d, target=%d, condition=%d\n",
  108. prev_uv, target_uv, condition);
  109. if (!condition ||
  110. (condition < 0 && target_uv < prev_uv) ||
  111. (condition > 0 && target_uv > prev_uv)) {
  112. ret = gk20a_volt_vid_set(&volt->base, volt->base.vid[id].vid);
  113. } else {
  114. ret = 0;
  115. }
  116. return ret;
  117. }
  118. static const struct nvkm_volt_func
  119. gk20a_volt = {
  120. .vid_get = gk20a_volt_vid_get,
  121. .vid_set = gk20a_volt_vid_set,
  122. .set_id = gk20a_volt_set_id,
  123. };
  124. int
  125. gk20a_volt_ctor(struct nvkm_device *device, int index,
  126. const struct cvb_coef *coefs, int nb_coefs,
  127. int vmin, struct gk20a_volt *volt)
  128. {
  129. struct nvkm_device_tegra *tdev = device->func->tegra(device);
  130. int i, uv;
  131. nvkm_volt_ctor(&gk20a_volt, device, index, &volt->base);
  132. uv = regulator_get_voltage(tdev->vdd);
  133. nvkm_debug(&volt->base.subdev, "the default voltage is %duV\n", uv);
  134. volt->vdd = tdev->vdd;
  135. volt->base.vid_nr = nb_coefs;
  136. for (i = 0; i < volt->base.vid_nr; i++) {
  137. volt->base.vid[i].vid = i;
  138. volt->base.vid[i].uv = max(
  139. gk20a_volt_calc_voltage(&coefs[i], tdev->gpu_speedo),
  140. vmin);
  141. nvkm_debug(&volt->base.subdev, "%2d: vid=%d, uv=%d\n", i,
  142. volt->base.vid[i].vid, volt->base.vid[i].uv);
  143. }
  144. return 0;
  145. }
  146. int
  147. gk20a_volt_new(struct nvkm_device *device, int index, struct nvkm_volt **pvolt)
  148. {
  149. struct gk20a_volt *volt;
  150. volt = kzalloc(sizeof(*volt), GFP_KERNEL);
  151. if (!volt)
  152. return -ENOMEM;
  153. *pvolt = &volt->base;
  154. return gk20a_volt_ctor(device, index, gk20a_cvb_coef,
  155. ARRAY_SIZE(gk20a_cvb_coef), 0, volt);
  156. }