cvb.c 4.2 KB

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