vp.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include "common.h"
  5. #include "voltage.h"
  6. #include "vp.h"
  7. #include "prm-regbits-34xx.h"
  8. #include "prm-regbits-44xx.h"
  9. #include "prm44xx.h"
  10. static u32 _vp_set_init_voltage(struct voltagedomain *voltdm, u32 volt)
  11. {
  12. struct omap_vp_instance *vp = voltdm->vp;
  13. u32 vpconfig;
  14. char vsel;
  15. vsel = voltdm->pmic->uv_to_vsel(volt);
  16. vpconfig = voltdm->read(vp->vpconfig);
  17. vpconfig &= ~(vp->common->vpconfig_initvoltage_mask |
  18. vp->common->vpconfig_forceupdate |
  19. vp->common->vpconfig_initvdd);
  20. vpconfig |= vsel << __ffs(vp->common->vpconfig_initvoltage_mask);
  21. voltdm->write(vpconfig, vp->vpconfig);
  22. /* Trigger initVDD value copy to voltage processor */
  23. voltdm->write((vpconfig | vp->common->vpconfig_initvdd),
  24. vp->vpconfig);
  25. /* Clear initVDD copy trigger bit */
  26. voltdm->write(vpconfig, vp->vpconfig);
  27. return vpconfig;
  28. }
  29. /* Generic voltage init functions */
  30. void __init omap_vp_init(struct voltagedomain *voltdm)
  31. {
  32. struct omap_vp_instance *vp = voltdm->vp;
  33. u32 val, sys_clk_rate, timeout, waittime;
  34. u32 vddmin, vddmax, vstepmin, vstepmax;
  35. if (!voltdm->pmic || !voltdm->pmic->uv_to_vsel) {
  36. pr_err("%s: No PMIC info for vdd_%s\n", __func__, voltdm->name);
  37. return;
  38. }
  39. if (!voltdm->read || !voltdm->write) {
  40. pr_err("%s: No read/write API for accessing vdd_%s regs\n",
  41. __func__, voltdm->name);
  42. return;
  43. }
  44. vp->enabled = false;
  45. /* Divide to avoid overflow */
  46. sys_clk_rate = voltdm->sys_clk.rate / 1000;
  47. timeout = (sys_clk_rate * voltdm->pmic->vp_timeout_us) / 1000;
  48. vddmin = max(voltdm->vp_param->vddmin, voltdm->pmic->vddmin);
  49. vddmax = min(voltdm->vp_param->vddmax, voltdm->pmic->vddmax);
  50. vddmin = voltdm->pmic->uv_to_vsel(vddmin);
  51. vddmax = voltdm->pmic->uv_to_vsel(vddmax);
  52. waittime = DIV_ROUND_UP(voltdm->pmic->step_size * sys_clk_rate,
  53. 1000 * voltdm->pmic->slew_rate);
  54. vstepmin = voltdm->pmic->vp_vstepmin;
  55. vstepmax = voltdm->pmic->vp_vstepmax;
  56. /*
  57. * VP_CONFIG: error gain is not set here, it will be updated
  58. * on each scale, based on OPP.
  59. */
  60. val = (voltdm->pmic->vp_erroroffset <<
  61. __ffs(voltdm->vp->common->vpconfig_erroroffset_mask)) |
  62. vp->common->vpconfig_timeouten;
  63. voltdm->write(val, vp->vpconfig);
  64. /* VSTEPMIN */
  65. val = (waittime << vp->common->vstepmin_smpswaittimemin_shift) |
  66. (vstepmin << vp->common->vstepmin_stepmin_shift);
  67. voltdm->write(val, vp->vstepmin);
  68. /* VSTEPMAX */
  69. val = (vstepmax << vp->common->vstepmax_stepmax_shift) |
  70. (waittime << vp->common->vstepmax_smpswaittimemax_shift);
  71. voltdm->write(val, vp->vstepmax);
  72. /* VLIMITTO */
  73. val = (vddmax << vp->common->vlimitto_vddmax_shift) |
  74. (vddmin << vp->common->vlimitto_vddmin_shift) |
  75. (timeout << vp->common->vlimitto_timeout_shift);
  76. voltdm->write(val, vp->vlimitto);
  77. }
  78. int omap_vp_update_errorgain(struct voltagedomain *voltdm,
  79. unsigned long target_volt)
  80. {
  81. struct omap_volt_data *volt_data;
  82. if (!voltdm->vp)
  83. return -EINVAL;
  84. /* Get volt_data corresponding to target_volt */
  85. volt_data = omap_voltage_get_voltdata(voltdm, target_volt);
  86. if (IS_ERR(volt_data))
  87. return -EINVAL;
  88. /* Setting vp errorgain based on the voltage */
  89. voltdm->rmw(voltdm->vp->common->vpconfig_errorgain_mask,
  90. volt_data->vp_errgain <<
  91. __ffs(voltdm->vp->common->vpconfig_errorgain_mask),
  92. voltdm->vp->vpconfig);
  93. return 0;
  94. }
  95. /* VP force update method of voltage scaling */
  96. int omap_vp_forceupdate_scale(struct voltagedomain *voltdm,
  97. unsigned long target_volt)
  98. {
  99. struct omap_vp_instance *vp = voltdm->vp;
  100. u32 vpconfig;
  101. u8 target_vsel, current_vsel;
  102. int ret, timeout = 0;
  103. ret = omap_vc_pre_scale(voltdm, target_volt, &target_vsel, &current_vsel);
  104. if (ret)
  105. return ret;
  106. /*
  107. * Clear all pending TransactionDone interrupt/status. Typical latency
  108. * is <3us
  109. */
  110. while (timeout++ < VP_TRANXDONE_TIMEOUT) {
  111. vp->common->ops->clear_txdone(vp->id);
  112. if (!vp->common->ops->check_txdone(vp->id))
  113. break;
  114. udelay(1);
  115. }
  116. if (timeout >= VP_TRANXDONE_TIMEOUT) {
  117. pr_warn("%s: vdd_%s TRANXDONE timeout exceeded. Voltage change aborted\n",
  118. __func__, voltdm->name);
  119. return -ETIMEDOUT;
  120. }
  121. vpconfig = _vp_set_init_voltage(voltdm, target_volt);
  122. /* Force update of voltage */
  123. voltdm->write(vpconfig | vp->common->vpconfig_forceupdate,
  124. voltdm->vp->vpconfig);
  125. /*
  126. * Wait for TransactionDone. Typical latency is <200us.
  127. * Depends on SMPSWAITTIMEMIN/MAX and voltage change
  128. */
  129. timeout = 0;
  130. omap_test_timeout(vp->common->ops->check_txdone(vp->id),
  131. VP_TRANXDONE_TIMEOUT, timeout);
  132. if (timeout >= VP_TRANXDONE_TIMEOUT)
  133. pr_err("%s: vdd_%s TRANXDONE timeout exceeded. TRANXDONE never got set after the voltage update\n",
  134. __func__, voltdm->name);
  135. omap_vc_post_scale(voltdm, target_volt, target_vsel, current_vsel);
  136. /*
  137. * Disable TransactionDone interrupt , clear all status, clear
  138. * control registers
  139. */
  140. timeout = 0;
  141. while (timeout++ < VP_TRANXDONE_TIMEOUT) {
  142. vp->common->ops->clear_txdone(vp->id);
  143. if (!vp->common->ops->check_txdone(vp->id))
  144. break;
  145. udelay(1);
  146. }
  147. if (timeout >= VP_TRANXDONE_TIMEOUT)
  148. pr_warn("%s: vdd_%s TRANXDONE timeout exceeded while trying to clear the TRANXDONE status\n",
  149. __func__, voltdm->name);
  150. /* Clear force bit */
  151. voltdm->write(vpconfig, vp->vpconfig);
  152. return 0;
  153. }
  154. /**
  155. * omap_vp_enable() - API to enable a particular VP
  156. * @voltdm: pointer to the VDD whose VP is to be enabled.
  157. *
  158. * This API enables a particular voltage processor. Needed by the smartreflex
  159. * class drivers.
  160. */
  161. void omap_vp_enable(struct voltagedomain *voltdm)
  162. {
  163. struct omap_vp_instance *vp;
  164. u32 vpconfig, volt;
  165. if (!voltdm || IS_ERR(voltdm)) {
  166. pr_warn("%s: VDD specified does not exist!\n", __func__);
  167. return;
  168. }
  169. vp = voltdm->vp;
  170. if (!voltdm->read || !voltdm->write) {
  171. pr_err("%s: No read/write API for accessing vdd_%s regs\n",
  172. __func__, voltdm->name);
  173. return;
  174. }
  175. /* If VP is already enabled, do nothing. Return */
  176. if (vp->enabled)
  177. return;
  178. volt = voltdm_get_voltage(voltdm);
  179. if (!volt) {
  180. pr_warn("%s: unable to find current voltage for %s\n",
  181. __func__, voltdm->name);
  182. return;
  183. }
  184. vpconfig = _vp_set_init_voltage(voltdm, volt);
  185. /* Enable VP */
  186. vpconfig |= vp->common->vpconfig_vpenable;
  187. voltdm->write(vpconfig, vp->vpconfig);
  188. vp->enabled = true;
  189. }
  190. /**
  191. * omap_vp_disable() - API to disable a particular VP
  192. * @voltdm: pointer to the VDD whose VP is to be disabled.
  193. *
  194. * This API disables a particular voltage processor. Needed by the smartreflex
  195. * class drivers.
  196. */
  197. void omap_vp_disable(struct voltagedomain *voltdm)
  198. {
  199. struct omap_vp_instance *vp;
  200. u32 vpconfig;
  201. int timeout;
  202. if (!voltdm || IS_ERR(voltdm)) {
  203. pr_warn("%s: VDD specified does not exist!\n", __func__);
  204. return;
  205. }
  206. vp = voltdm->vp;
  207. if (!voltdm->read || !voltdm->write) {
  208. pr_err("%s: No read/write API for accessing vdd_%s regs\n",
  209. __func__, voltdm->name);
  210. return;
  211. }
  212. /* If VP is already disabled, do nothing. Return */
  213. if (!vp->enabled) {
  214. pr_warn("%s: Trying to disable VP for vdd_%s when it is already disabled\n",
  215. __func__, voltdm->name);
  216. return;
  217. }
  218. /* Disable VP */
  219. vpconfig = voltdm->read(vp->vpconfig);
  220. vpconfig &= ~vp->common->vpconfig_vpenable;
  221. voltdm->write(vpconfig, vp->vpconfig);
  222. /*
  223. * Wait for VP idle Typical latency is <2us. Maximum latency is ~100us
  224. */
  225. omap_test_timeout((voltdm->read(vp->vstatus)),
  226. VP_IDLE_TIMEOUT, timeout);
  227. if (timeout >= VP_IDLE_TIMEOUT)
  228. pr_warn("%s: vdd_%s idle timedout\n", __func__, voltdm->name);
  229. vp->enabled = false;
  230. return;
  231. }