vc.c 11 KB

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