vc.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. /*
  20. * Channel configuration bits, common for OMAP3 & 4
  21. * OMAP3 register: PRM_VC_CH_CONF
  22. * OMAP4 register: PRM_VC_CFG_CHANNEL
  23. */
  24. #define CFG_CHANNEL_SA BIT(0)
  25. #define CFG_CHANNEL_RAV BIT(1)
  26. #define CFG_CHANNEL_RAC BIT(2)
  27. #define CFG_CHANNEL_RACEN BIT(3)
  28. #define CFG_CHANNEL_CMD BIT(4)
  29. #define CFG_CHANNEL_MASK 0x3f
  30. /**
  31. * omap_vc_config_channel - configure VC channel to PMIC mappings
  32. * @voltdm: pointer to voltagdomain defining the desired VC channel
  33. *
  34. * Configures the VC channel to PMIC mappings for the following
  35. * PMIC settings
  36. * - i2c slave address (SA)
  37. * - voltage configuration address (RAV)
  38. * - command configuration address (RAC) and enable bit (RACEN)
  39. * - command values for ON, ONLP, RET and OFF (CMD)
  40. *
  41. * This function currently only allows flexible configuration of the
  42. * non-default channel. Starting with OMAP4, there are more than 2
  43. * channels, with one defined as the default (on OMAP4, it's MPU.)
  44. * Only the non-default channel can be configured.
  45. */
  46. static int omap_vc_config_channel(struct voltagedomain *voltdm)
  47. {
  48. struct omap_vc_channel *vc = voltdm->vc;
  49. /*
  50. * For default channel, the only configurable bit is RACEN.
  51. * All others must stay at zero (see function comment above.)
  52. */
  53. if (vc->flags & OMAP_VC_CHANNEL_DEFAULT)
  54. vc->cfg_channel &= CFG_CHANNEL_RACEN;
  55. voltdm->rmw(CFG_CHANNEL_MASK << vc->cfg_channel_sa_shift,
  56. vc->cfg_channel << vc->cfg_channel_sa_shift,
  57. vc->common->cfg_channel_reg);
  58. return 0;
  59. }
  60. /* Voltage scale and accessory APIs */
  61. int omap_vc_pre_scale(struct voltagedomain *voltdm,
  62. unsigned long target_volt,
  63. u8 *target_vsel, u8 *current_vsel)
  64. {
  65. struct omap_vc_channel *vc = voltdm->vc;
  66. struct omap_vdd_info *vdd = voltdm->vdd;
  67. struct omap_volt_data *volt_data;
  68. const struct omap_vp_common_data *vp_common;
  69. u32 vc_cmdval, vp_errgain_val;
  70. vp_common = vdd->vp_data->vp_common;
  71. /* Check if sufficient pmic info is available for this vdd */
  72. if (!voltdm->pmic) {
  73. pr_err("%s: Insufficient pmic info to scale the vdd_%s\n",
  74. __func__, voltdm->name);
  75. return -EINVAL;
  76. }
  77. if (!voltdm->pmic->uv_to_vsel) {
  78. pr_err("%s: PMIC function to convert voltage in uV to"
  79. "vsel not registered. Hence unable to scale voltage"
  80. "for vdd_%s\n", __func__, voltdm->name);
  81. return -ENODATA;
  82. }
  83. if (!voltdm->read || !voltdm->write) {
  84. pr_err("%s: No read/write API for accessing vdd_%s regs\n",
  85. __func__, voltdm->name);
  86. return -EINVAL;
  87. }
  88. /* Get volt_data corresponding to target_volt */
  89. volt_data = omap_voltage_get_voltdata(voltdm, target_volt);
  90. if (IS_ERR(volt_data))
  91. volt_data = NULL;
  92. *target_vsel = voltdm->pmic->uv_to_vsel(target_volt);
  93. *current_vsel = voltdm->read(vdd->vp_data->voltage);
  94. /* Setting the ON voltage to the new target voltage */
  95. vc_cmdval = voltdm->read(vc->cmdval_reg);
  96. vc_cmdval &= ~vc->common->cmd_on_mask;
  97. vc_cmdval |= (*target_vsel << vc->common->cmd_on_shift);
  98. voltdm->write(vc_cmdval, vc->cmdval_reg);
  99. /* Setting vp errorgain based on the voltage */
  100. if (volt_data) {
  101. vp_errgain_val = voltdm->read(vdd->vp_data->vpconfig);
  102. vdd->vp_rt_data.vpconfig_errorgain = volt_data->vp_errgain;
  103. vp_errgain_val &= ~vp_common->vpconfig_errorgain_mask;
  104. vp_errgain_val |= vdd->vp_rt_data.vpconfig_errorgain <<
  105. vp_common->vpconfig_errorgain_shift;
  106. voltdm->write(vp_errgain_val, vdd->vp_data->vpconfig);
  107. }
  108. return 0;
  109. }
  110. void omap_vc_post_scale(struct voltagedomain *voltdm,
  111. unsigned long target_volt,
  112. u8 target_vsel, u8 current_vsel)
  113. {
  114. struct omap_vdd_info *vdd = voltdm->vdd;
  115. u32 smps_steps = 0, smps_delay = 0;
  116. smps_steps = abs(target_vsel - current_vsel);
  117. /* SMPS slew rate / step size. 2us added as buffer. */
  118. smps_delay = ((smps_steps * voltdm->pmic->step_size) /
  119. voltdm->pmic->slew_rate) + 2;
  120. udelay(smps_delay);
  121. vdd->curr_volt = target_volt;
  122. }
  123. /* vc_bypass_scale - VC bypass method of voltage scaling */
  124. int omap_vc_bypass_scale(struct voltagedomain *voltdm,
  125. unsigned long target_volt)
  126. {
  127. struct omap_vc_channel *vc = voltdm->vc;
  128. u32 loop_cnt = 0, retries_cnt = 0;
  129. u32 vc_valid, vc_bypass_val_reg, vc_bypass_value;
  130. u8 target_vsel, current_vsel;
  131. int ret;
  132. ret = omap_vc_pre_scale(voltdm, target_volt, &target_vsel, &current_vsel);
  133. if (ret)
  134. return ret;
  135. vc_valid = vc->common->valid;
  136. vc_bypass_val_reg = vc->common->bypass_val_reg;
  137. vc_bypass_value = (target_vsel << vc->common->data_shift) |
  138. (vc->volt_reg_addr << vc->common->regaddr_shift) |
  139. (vc->i2c_slave_addr << vc->common->slaveaddr_shift);
  140. voltdm->write(vc_bypass_value, vc_bypass_val_reg);
  141. voltdm->write(vc_bypass_value | vc_valid, vc_bypass_val_reg);
  142. vc_bypass_value = voltdm->read(vc_bypass_val_reg);
  143. /*
  144. * Loop till the bypass command is acknowledged from the SMPS.
  145. * NOTE: This is legacy code. The loop count and retry count needs
  146. * to be revisited.
  147. */
  148. while (!(vc_bypass_value & vc_valid)) {
  149. loop_cnt++;
  150. if (retries_cnt > 10) {
  151. pr_warning("%s: Retry count exceeded\n", __func__);
  152. return -ETIMEDOUT;
  153. }
  154. if (loop_cnt > 50) {
  155. retries_cnt++;
  156. loop_cnt = 0;
  157. udelay(10);
  158. }
  159. vc_bypass_value = voltdm->read(vc_bypass_val_reg);
  160. }
  161. omap_vc_post_scale(voltdm, target_volt, target_vsel, current_vsel);
  162. return 0;
  163. }
  164. static void __init omap3_vfsm_init(struct voltagedomain *voltdm)
  165. {
  166. /*
  167. * Voltage Manager FSM parameters init
  168. * XXX This data should be passed in from the board file
  169. */
  170. voltdm->write(OMAP3_CLKSETUP, OMAP3_PRM_CLKSETUP_OFFSET);
  171. voltdm->write(OMAP3_VOLTOFFSET, OMAP3_PRM_VOLTOFFSET_OFFSET);
  172. voltdm->write(OMAP3_VOLTSETUP2, OMAP3_PRM_VOLTSETUP2_OFFSET);
  173. }
  174. static void __init omap3_vc_init_channel(struct voltagedomain *voltdm)
  175. {
  176. static bool is_initialized;
  177. if (is_initialized)
  178. return;
  179. omap3_vfsm_init(voltdm);
  180. is_initialized = true;
  181. }
  182. /* OMAP4 specific voltage init functions */
  183. static void __init omap4_vc_init_channel(struct voltagedomain *voltdm)
  184. {
  185. static bool is_initialized;
  186. u32 vc_val;
  187. if (is_initialized)
  188. return;
  189. /* XXX These are magic numbers and do not belong! */
  190. vc_val = (0x60 << OMAP4430_SCLL_SHIFT | 0x26 << OMAP4430_SCLH_SHIFT);
  191. voltdm->write(vc_val, OMAP4_PRM_VC_CFG_I2C_CLK_OFFSET);
  192. is_initialized = true;
  193. }
  194. /**
  195. * omap_vc_i2c_init - initialize I2C interface to PMIC
  196. * @voltdm: voltage domain containing VC data
  197. *
  198. * Use PMIC supplied seetings for I2C high-speed mode and
  199. * master code (if set) and program the VC I2C configuration
  200. * register.
  201. *
  202. * The VC I2C configuration is common to all VC channels,
  203. * so this function only configures I2C for the first VC
  204. * channel registers. All other VC channels will use the
  205. * same configuration.
  206. */
  207. static void __init omap_vc_i2c_init(struct voltagedomain *voltdm)
  208. {
  209. struct omap_vc_channel *vc = voltdm->vc;
  210. static bool initialized;
  211. static bool i2c_high_speed;
  212. u8 mcode;
  213. if (initialized) {
  214. if (voltdm->pmic->i2c_high_speed != i2c_high_speed)
  215. pr_warn("%s: I2C config for all channels must match.",
  216. __func__);
  217. return;
  218. }
  219. i2c_high_speed = voltdm->pmic->i2c_high_speed;
  220. if (i2c_high_speed)
  221. voltdm->rmw(vc->common->i2c_cfg_hsen_mask,
  222. vc->common->i2c_cfg_hsen_mask,
  223. vc->common->i2c_cfg_reg);
  224. mcode = voltdm->pmic->i2c_mcode;
  225. if (mcode)
  226. voltdm->rmw(vc->common->i2c_mcode_mask,
  227. mcode << __ffs(vc->common->i2c_mcode_mask),
  228. vc->common->i2c_cfg_reg);
  229. initialized = true;
  230. }
  231. void __init omap_vc_init_channel(struct voltagedomain *voltdm)
  232. {
  233. struct omap_vc_channel *vc = voltdm->vc;
  234. u8 on_vsel, onlp_vsel, ret_vsel, off_vsel;
  235. u32 val;
  236. if (!voltdm->pmic || !voltdm->pmic->uv_to_vsel) {
  237. pr_err("%s: PMIC info requried to configure vc for"
  238. "vdd_%s not populated.Hence cannot initialize vc\n",
  239. __func__, voltdm->name);
  240. return;
  241. }
  242. if (!voltdm->read || !voltdm->write) {
  243. pr_err("%s: No read/write API for accessing vdd_%s regs\n",
  244. __func__, voltdm->name);
  245. return;
  246. }
  247. vc->cfg_channel = 0;
  248. /* get PMIC/board specific settings */
  249. vc->i2c_slave_addr = voltdm->pmic->i2c_slave_addr;
  250. vc->volt_reg_addr = voltdm->pmic->volt_reg_addr;
  251. vc->cmd_reg_addr = voltdm->pmic->cmd_reg_addr;
  252. vc->setup_time = voltdm->pmic->volt_setup_time;
  253. /* Configure the i2c slave address for this VC */
  254. voltdm->rmw(vc->smps_sa_mask,
  255. vc->i2c_slave_addr << __ffs(vc->smps_sa_mask),
  256. vc->common->smps_sa_reg);
  257. vc->cfg_channel |= CFG_CHANNEL_SA;
  258. /*
  259. * Configure the PMIC register addresses.
  260. */
  261. voltdm->rmw(vc->smps_volra_mask,
  262. vc->volt_reg_addr << __ffs(vc->smps_volra_mask),
  263. vc->common->smps_volra_reg);
  264. vc->cfg_channel |= CFG_CHANNEL_RAV;
  265. if (vc->cmd_reg_addr) {
  266. voltdm->rmw(vc->smps_cmdra_mask,
  267. vc->cmd_reg_addr << __ffs(vc->smps_cmdra_mask),
  268. vc->common->smps_cmdra_reg);
  269. vc->cfg_channel |= CFG_CHANNEL_RAC | CFG_CHANNEL_RACEN;
  270. }
  271. /* Set up the on, inactive, retention and off voltage */
  272. on_vsel = voltdm->pmic->uv_to_vsel(voltdm->pmic->on_volt);
  273. onlp_vsel = voltdm->pmic->uv_to_vsel(voltdm->pmic->onlp_volt);
  274. ret_vsel = voltdm->pmic->uv_to_vsel(voltdm->pmic->ret_volt);
  275. off_vsel = voltdm->pmic->uv_to_vsel(voltdm->pmic->off_volt);
  276. val = ((on_vsel << vc->common->cmd_on_shift) |
  277. (onlp_vsel << vc->common->cmd_onlp_shift) |
  278. (ret_vsel << vc->common->cmd_ret_shift) |
  279. (off_vsel << vc->common->cmd_off_shift));
  280. voltdm->write(val, vc->cmdval_reg);
  281. vc->cfg_channel |= CFG_CHANNEL_CMD;
  282. /* Channel configuration */
  283. omap_vc_config_channel(voltdm);
  284. /* Configure the setup times */
  285. voltdm->rmw(voltdm->vfsm->voltsetup_mask,
  286. vc->setup_time << __ffs(voltdm->vfsm->voltsetup_mask),
  287. voltdm->vfsm->voltsetup_reg);
  288. omap_vc_i2c_init(voltdm);
  289. if (cpu_is_omap34xx())
  290. omap3_vc_init_channel(voltdm);
  291. else if (cpu_is_omap44xx())
  292. omap4_vc_init_channel(voltdm);
  293. }