cvb.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Utility functions for parsing Tegra CVB voltage tables
  4. *
  5. * Copyright (C) 2012-2019 NVIDIA Corporation. All rights reserved.
  6. */
  7. #include <linux/err.h>
  8. #include <linux/kernel.h>
  9. #include <linux/pm_opp.h>
  10. #include "cvb.h"
  11. /* cvb_mv = ((c2 * speedo / s_scale + c1) * speedo / s_scale + c0) */
  12. static inline int get_cvb_voltage(int speedo, int s_scale,
  13. const struct cvb_coefficients *cvb)
  14. {
  15. int mv;
  16. /* apply only speedo scale: output mv = cvb_mv * v_scale */
  17. mv = DIV_ROUND_CLOSEST(cvb->c2 * speedo, s_scale);
  18. mv = DIV_ROUND_CLOSEST((mv + cvb->c1) * speedo, s_scale) + cvb->c0;
  19. return mv;
  20. }
  21. static int round_cvb_voltage(int mv, int v_scale,
  22. const struct rail_alignment *align)
  23. {
  24. /* combined: apply voltage scale and round to cvb alignment step */
  25. int uv;
  26. int step = (align->step_uv ? : 1000) * v_scale;
  27. int offset = align->offset_uv * v_scale;
  28. uv = max(mv * 1000, offset) - offset;
  29. uv = DIV_ROUND_UP(uv, step) * align->step_uv + align->offset_uv;
  30. return uv / 1000;
  31. }
  32. enum {
  33. DOWN,
  34. UP
  35. };
  36. static int round_voltage(int mv, const struct rail_alignment *align, int up)
  37. {
  38. if (align->step_uv) {
  39. int uv;
  40. uv = max(mv * 1000, align->offset_uv) - align->offset_uv;
  41. uv = (uv + (up ? align->step_uv - 1 : 0)) / align->step_uv;
  42. return (uv * align->step_uv + align->offset_uv) / 1000;
  43. }
  44. return mv;
  45. }
  46. static int build_opp_table(struct device *dev, const struct cvb_table *table,
  47. struct rail_alignment *align,
  48. int speedo_value, unsigned long max_freq)
  49. {
  50. int i, ret, dfll_mv, min_mv, max_mv;
  51. min_mv = round_voltage(table->min_millivolts, align, UP);
  52. max_mv = round_voltage(table->max_millivolts, align, DOWN);
  53. for (i = 0; i < MAX_DVFS_FREQS; i++) {
  54. const struct cvb_table_freq_entry *entry = &table->entries[i];
  55. if (!entry->freq || (entry->freq > max_freq))
  56. break;
  57. dfll_mv = get_cvb_voltage(speedo_value, table->speedo_scale,
  58. &entry->coefficients);
  59. dfll_mv = round_cvb_voltage(dfll_mv, table->voltage_scale,
  60. align);
  61. dfll_mv = clamp(dfll_mv, min_mv, max_mv);
  62. ret = dev_pm_opp_add(dev, entry->freq, dfll_mv * 1000);
  63. if (ret)
  64. return ret;
  65. }
  66. return 0;
  67. }
  68. /**
  69. * tegra_cvb_add_opp_table - build OPP table from Tegra CVB tables
  70. * @dev: the struct device * for which the OPP table is built
  71. * @tables: array of CVB tables
  72. * @count: size of the previously mentioned array
  73. * @process_id: process id of the HW module
  74. * @speedo_id: speedo id of the HW module
  75. * @speedo_value: speedo value of the HW module
  76. * @max_freq: highest safe clock rate
  77. *
  78. * On Tegra, a CVB table encodes the relationship between operating voltage
  79. * and safe maximal frequency for a given module (e.g. GPU or CPU). This
  80. * function calculates the optimal voltage-frequency operating points
  81. * for the given arguments and exports them via the OPP library for the
  82. * given @dev. Returns a pointer to the struct cvb_table that matched
  83. * or an ERR_PTR on failure.
  84. */
  85. const struct cvb_table *
  86. tegra_cvb_add_opp_table(struct device *dev, const struct cvb_table *tables,
  87. size_t count, struct rail_alignment *align,
  88. int process_id, int speedo_id, int speedo_value,
  89. unsigned long max_freq)
  90. {
  91. size_t i;
  92. int ret;
  93. for (i = 0; i < count; i++) {
  94. const struct cvb_table *table = &tables[i];
  95. if (table->speedo_id != -1 && table->speedo_id != speedo_id)
  96. continue;
  97. if (table->process_id != -1 && table->process_id != process_id)
  98. continue;
  99. ret = build_opp_table(dev, table, align, speedo_value,
  100. max_freq);
  101. return ret ? ERR_PTR(ret) : table;
  102. }
  103. return ERR_PTR(-EINVAL);
  104. }
  105. void tegra_cvb_remove_opp_table(struct device *dev,
  106. const struct cvb_table *table,
  107. unsigned long max_freq)
  108. {
  109. unsigned int i;
  110. for (i = 0; i < MAX_DVFS_FREQS; i++) {
  111. const struct cvb_table_freq_entry *entry = &table->entries[i];
  112. if (!entry->freq || (entry->freq > max_freq))
  113. break;
  114. dev_pm_opp_remove(dev, entry->freq);
  115. }
  116. }