vc.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * OMAP Voltage Controller (VC) interface
  3. *
  4. * Copyright (C) 2011 Texas Instruments, Inc.
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/delay.h>
  12. #include <linux/init.h>
  13. #include <plat/cpu.h>
  14. #include "voltage.h"
  15. #include "vc.h"
  16. #include "prm-regbits-34xx.h"
  17. #include "prm-regbits-44xx.h"
  18. #include "prm44xx.h"
  19. /* Voltage scale and accessory APIs */
  20. int omap_vc_pre_scale(struct voltagedomain *voltdm,
  21. unsigned long target_volt,
  22. u8 *target_vsel, u8 *current_vsel)
  23. {
  24. struct omap_vc_channel *vc = voltdm->vc;
  25. struct omap_vdd_info *vdd = voltdm->vdd;
  26. struct omap_volt_data *volt_data;
  27. const struct omap_vp_common_data *vp_common;
  28. u32 vc_cmdval, vp_errgain_val;
  29. vp_common = vdd->vp_data->vp_common;
  30. /* Check if sufficient pmic info is available for this vdd */
  31. if (!vdd->pmic_info) {
  32. pr_err("%s: Insufficient pmic info to scale the vdd_%s\n",
  33. __func__, voltdm->name);
  34. return -EINVAL;
  35. }
  36. if (!vdd->pmic_info->uv_to_vsel) {
  37. pr_err("%s: PMIC function to convert voltage in uV to"
  38. "vsel not registered. Hence unable to scale voltage"
  39. "for vdd_%s\n", __func__, voltdm->name);
  40. return -ENODATA;
  41. }
  42. if (!vdd->read_reg || !vdd->write_reg) {
  43. pr_err("%s: No read/write API for accessing vdd_%s regs\n",
  44. __func__, voltdm->name);
  45. return -EINVAL;
  46. }
  47. /* Get volt_data corresponding to target_volt */
  48. volt_data = omap_voltage_get_voltdata(voltdm, target_volt);
  49. if (IS_ERR(volt_data))
  50. volt_data = NULL;
  51. *target_vsel = vdd->pmic_info->uv_to_vsel(target_volt);
  52. *current_vsel = vdd->read_reg(vdd->vp_data->vp_common->prm_mod, vdd->vp_data->voltage);
  53. /* Setting the ON voltage to the new target voltage */
  54. vc_cmdval = vdd->read_reg(vc->common->prm_mod, vc->cmdval_reg);
  55. vc_cmdval &= ~vc->common->cmd_on_mask;
  56. vc_cmdval |= (*target_vsel << vc->common->cmd_on_shift);
  57. vdd->write_reg(vc_cmdval, vc->common->prm_mod, vc->cmdval_reg);
  58. /* Setting vp errorgain based on the voltage */
  59. if (volt_data) {
  60. vp_errgain_val = vdd->read_reg(vdd->vp_data->vp_common->prm_mod,
  61. vdd->vp_data->vpconfig);
  62. vdd->vp_rt_data.vpconfig_errorgain = volt_data->vp_errgain;
  63. vp_errgain_val &= ~vp_common->vpconfig_errorgain_mask;
  64. vp_errgain_val |= vdd->vp_rt_data.vpconfig_errorgain <<
  65. vp_common->vpconfig_errorgain_shift;
  66. vdd->write_reg(vp_errgain_val, vdd->vp_data->vp_common->prm_mod,
  67. vdd->vp_data->vpconfig);
  68. }
  69. return 0;
  70. }
  71. void omap_vc_post_scale(struct voltagedomain *voltdm,
  72. unsigned long target_volt,
  73. u8 target_vsel, u8 current_vsel)
  74. {
  75. struct omap_vdd_info *vdd = voltdm->vdd;
  76. u32 smps_steps = 0, smps_delay = 0;
  77. smps_steps = abs(target_vsel - current_vsel);
  78. /* SMPS slew rate / step size. 2us added as buffer. */
  79. smps_delay = ((smps_steps * vdd->pmic_info->step_size) /
  80. vdd->pmic_info->slew_rate) + 2;
  81. udelay(smps_delay);
  82. vdd->curr_volt = target_volt;
  83. }
  84. /* vc_bypass_scale - VC bypass method of voltage scaling */
  85. int omap_vc_bypass_scale(struct voltagedomain *voltdm,
  86. unsigned long target_volt)
  87. {
  88. struct omap_vc_channel *vc = voltdm->vc;
  89. struct omap_vdd_info *vdd = voltdm->vdd;
  90. u32 loop_cnt = 0, retries_cnt = 0;
  91. u32 vc_valid, vc_bypass_val_reg, vc_bypass_value;
  92. u8 target_vsel, current_vsel;
  93. int ret;
  94. ret = omap_vc_pre_scale(voltdm, target_volt, &target_vsel, &current_vsel);
  95. if (ret)
  96. return ret;
  97. vc_valid = vc->common->valid;
  98. vc_bypass_val_reg = vc->common->bypass_val_reg;
  99. vc_bypass_value = (target_vsel << vc->common->data_shift) |
  100. (vdd->pmic_info->pmic_reg <<
  101. vc->common->regaddr_shift) |
  102. (vdd->pmic_info->i2c_slave_addr <<
  103. vc->common->slaveaddr_shift);
  104. vdd->write_reg(vc_bypass_value, vc->common->prm_mod, vc_bypass_val_reg);
  105. vdd->write_reg(vc_bypass_value | vc_valid, vc->common->prm_mod,
  106. vc_bypass_val_reg);
  107. vc_bypass_value = vdd->read_reg(vc->common->prm_mod, vc_bypass_val_reg);
  108. /*
  109. * Loop till the bypass command is acknowledged from the SMPS.
  110. * NOTE: This is legacy code. The loop count and retry count needs
  111. * to be revisited.
  112. */
  113. while (!(vc_bypass_value & vc_valid)) {
  114. loop_cnt++;
  115. if (retries_cnt > 10) {
  116. pr_warning("%s: Retry count exceeded\n", __func__);
  117. return -ETIMEDOUT;
  118. }
  119. if (loop_cnt > 50) {
  120. retries_cnt++;
  121. loop_cnt = 0;
  122. udelay(10);
  123. }
  124. vc_bypass_value = vdd->read_reg(vc->common->prm_mod,
  125. vc_bypass_val_reg);
  126. }
  127. omap_vc_post_scale(voltdm, target_volt, target_vsel, current_vsel);
  128. return 0;
  129. }
  130. static void __init omap3_vfsm_init(struct voltagedomain *voltdm)
  131. {
  132. struct omap_vc_channel *vc = voltdm->vc;
  133. struct omap_vdd_info *vdd = voltdm->vdd;
  134. /*
  135. * Voltage Manager FSM parameters init
  136. * XXX This data should be passed in from the board file
  137. */
  138. vdd->write_reg(OMAP3_CLKSETUP, vc->common->prm_mod, OMAP3_PRM_CLKSETUP_OFFSET);
  139. vdd->write_reg(OMAP3_VOLTOFFSET, vc->common->prm_mod,
  140. OMAP3_PRM_VOLTOFFSET_OFFSET);
  141. vdd->write_reg(OMAP3_VOLTSETUP2, vc->common->prm_mod,
  142. OMAP3_PRM_VOLTSETUP2_OFFSET);
  143. }
  144. static void __init omap3_vc_init_channel(struct voltagedomain *voltdm)
  145. {
  146. struct omap_vc_channel *vc = voltdm->vc;
  147. struct omap_vdd_info *vdd = voltdm->vdd;
  148. static bool is_initialized;
  149. u8 on_vsel, onlp_vsel, ret_vsel, off_vsel;
  150. u32 vc_val;
  151. if (is_initialized)
  152. return;
  153. /* Set up the on, inactive, retention and off voltage */
  154. on_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->on_volt);
  155. onlp_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->onlp_volt);
  156. ret_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->ret_volt);
  157. off_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->off_volt);
  158. vc_val = ((on_vsel << vc->common->cmd_on_shift) |
  159. (onlp_vsel << vc->common->cmd_onlp_shift) |
  160. (ret_vsel << vc->common->cmd_ret_shift) |
  161. (off_vsel << vc->common->cmd_off_shift));
  162. vdd->write_reg(vc_val, vc->common->prm_mod, vc->cmdval_reg);
  163. /*
  164. * Generic VC parameters init
  165. * XXX This data should be abstracted out
  166. */
  167. vdd->write_reg(OMAP3430_CMD1_MASK | OMAP3430_RAV1_MASK, vc->common->prm_mod,
  168. OMAP3_PRM_VC_CH_CONF_OFFSET);
  169. vdd->write_reg(OMAP3430_MCODE_SHIFT | OMAP3430_HSEN_MASK, vc->common->prm_mod,
  170. OMAP3_PRM_VC_I2C_CFG_OFFSET);
  171. omap3_vfsm_init(voltdm);
  172. is_initialized = true;
  173. }
  174. /* OMAP4 specific voltage init functions */
  175. static void __init omap4_vc_init_channel(struct voltagedomain *voltdm)
  176. {
  177. struct omap_vc_channel *vc = voltdm->vc;
  178. struct omap_vdd_info *vdd = voltdm->vdd;
  179. static bool is_initialized;
  180. u32 vc_val;
  181. if (is_initialized)
  182. return;
  183. /* TODO: Configure setup times and CMD_VAL values*/
  184. /*
  185. * Generic VC parameters init
  186. * XXX This data should be abstracted out
  187. */
  188. vc_val = (OMAP4430_RAV_VDD_MPU_L_MASK | OMAP4430_CMD_VDD_MPU_L_MASK |
  189. OMAP4430_RAV_VDD_IVA_L_MASK | OMAP4430_CMD_VDD_IVA_L_MASK |
  190. OMAP4430_RAV_VDD_CORE_L_MASK | OMAP4430_CMD_VDD_CORE_L_MASK);
  191. vdd->write_reg(vc_val, vc->common->prm_mod, OMAP4_PRM_VC_CFG_CHANNEL_OFFSET);
  192. /* XXX These are magic numbers and do not belong! */
  193. vc_val = (0x60 << OMAP4430_SCLL_SHIFT | 0x26 << OMAP4430_SCLH_SHIFT);
  194. vdd->write_reg(vc_val, vc->common->prm_mod, OMAP4_PRM_VC_CFG_I2C_CLK_OFFSET);
  195. is_initialized = true;
  196. }
  197. void __init omap_vc_init_channel(struct voltagedomain *voltdm)
  198. {
  199. struct omap_vc_channel *vc = voltdm->vc;
  200. struct omap_vdd_info *vdd = voltdm->vdd;
  201. u32 vc_val;
  202. if (!vdd->pmic_info || !vdd->pmic_info->uv_to_vsel) {
  203. pr_err("%s: PMIC info requried to configure vc for"
  204. "vdd_%s not populated.Hence cannot initialize vc\n",
  205. __func__, voltdm->name);
  206. return;
  207. }
  208. if (!vdd->read_reg || !vdd->write_reg) {
  209. pr_err("%s: No read/write API for accessing vdd_%s regs\n",
  210. __func__, voltdm->name);
  211. return;
  212. }
  213. /* Set up the SMPS_SA(i2c slave address in VC */
  214. vc_val = vdd->read_reg(vc->common->prm_mod,
  215. vc->common->smps_sa_reg);
  216. vc_val &= ~vc->smps_sa_mask;
  217. vc_val |= vdd->pmic_info->i2c_slave_addr << vc->smps_sa_shift;
  218. vdd->write_reg(vc_val, vc->common->prm_mod,
  219. vc->common->smps_sa_reg);
  220. /* Setup the VOLRA(pmic reg addr) in VC */
  221. vc_val = vdd->read_reg(vc->common->prm_mod,
  222. vc->common->smps_volra_reg);
  223. vc_val &= ~vc->smps_volra_mask;
  224. vc_val |= vdd->pmic_info->pmic_reg << vc->smps_volra_shift;
  225. vdd->write_reg(vc_val, vc->common->prm_mod,
  226. vc->common->smps_volra_reg);
  227. /* Configure the setup times */
  228. vc_val = vdd->read_reg(vc->common->prm_mod, vdd->vfsm->voltsetup_reg);
  229. vc_val &= ~vdd->vfsm->voltsetup_mask;
  230. vc_val |= vdd->pmic_info->volt_setup_time <<
  231. vdd->vfsm->voltsetup_shift;
  232. vdd->write_reg(vc_val, vc->common->prm_mod, vdd->vfsm->voltsetup_reg);
  233. if (cpu_is_omap34xx())
  234. omap3_vc_init_channel(voltdm);
  235. else if (cpu_is_omap44xx())
  236. omap4_vc_init_channel(voltdm);
  237. }