vc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 <linux/bug.h>
  14. #include "soc.h"
  15. #include "voltage.h"
  16. #include "vc.h"
  17. #include "prm-regbits-34xx.h"
  18. #include "prm-regbits-44xx.h"
  19. #include "prm44xx.h"
  20. /**
  21. * struct omap_vc_channel_cfg - describe the cfg_channel bitfield
  22. * @sa: bit for slave address
  23. * @rav: bit for voltage configuration register
  24. * @rac: bit for command configuration register
  25. * @racen: enable bit for RAC
  26. * @cmd: bit for command value set selection
  27. *
  28. * Channel configuration bits, common for OMAP3+
  29. * OMAP3 register: PRM_VC_CH_CONF
  30. * OMAP4 register: PRM_VC_CFG_CHANNEL
  31. * OMAP5 register: PRM_VC_SMPS_<voltdm>_CONFIG
  32. */
  33. struct omap_vc_channel_cfg {
  34. u8 sa;
  35. u8 rav;
  36. u8 rac;
  37. u8 racen;
  38. u8 cmd;
  39. };
  40. static struct omap_vc_channel_cfg vc_default_channel_cfg = {
  41. .sa = BIT(0),
  42. .rav = BIT(1),
  43. .rac = BIT(2),
  44. .racen = BIT(3),
  45. .cmd = BIT(4),
  46. };
  47. /*
  48. * On OMAP3+, all VC channels have the above default bitfield
  49. * configuration, except the OMAP4 MPU channel. This appears
  50. * to be a freak accident as every other VC channel has the
  51. * default configuration, thus creating a mutant channel config.
  52. */
  53. static struct omap_vc_channel_cfg vc_mutant_channel_cfg = {
  54. .sa = BIT(0),
  55. .rav = BIT(2),
  56. .rac = BIT(3),
  57. .racen = BIT(4),
  58. .cmd = BIT(1),
  59. };
  60. static struct omap_vc_channel_cfg *vc_cfg_bits;
  61. #define CFG_CHANNEL_MASK 0x1f
  62. /**
  63. * omap_vc_config_channel - configure VC channel to PMIC mappings
  64. * @voltdm: pointer to voltagdomain defining the desired VC channel
  65. *
  66. * Configures the VC channel to PMIC mappings for the following
  67. * PMIC settings
  68. * - i2c slave address (SA)
  69. * - voltage configuration address (RAV)
  70. * - command configuration address (RAC) and enable bit (RACEN)
  71. * - command values for ON, ONLP, RET and OFF (CMD)
  72. *
  73. * This function currently only allows flexible configuration of the
  74. * non-default channel. Starting with OMAP4, there are more than 2
  75. * channels, with one defined as the default (on OMAP4, it's MPU.)
  76. * Only the non-default channel can be configured.
  77. */
  78. static int omap_vc_config_channel(struct voltagedomain *voltdm)
  79. {
  80. struct omap_vc_channel *vc = voltdm->vc;
  81. /*
  82. * For default channel, the only configurable bit is RACEN.
  83. * All others must stay at zero (see function comment above.)
  84. */
  85. if (vc->flags & OMAP_VC_CHANNEL_DEFAULT)
  86. vc->cfg_channel &= vc_cfg_bits->racen;
  87. voltdm->rmw(CFG_CHANNEL_MASK << vc->cfg_channel_sa_shift,
  88. vc->cfg_channel << vc->cfg_channel_sa_shift,
  89. vc->cfg_channel_reg);
  90. return 0;
  91. }
  92. /* Voltage scale and accessory APIs */
  93. int omap_vc_pre_scale(struct voltagedomain *voltdm,
  94. unsigned long target_volt,
  95. u8 *target_vsel, u8 *current_vsel)
  96. {
  97. struct omap_vc_channel *vc = voltdm->vc;
  98. u32 vc_cmdval;
  99. /* Check if sufficient pmic info is available for this vdd */
  100. if (!voltdm->pmic) {
  101. pr_err("%s: Insufficient pmic info to scale the vdd_%s\n",
  102. __func__, voltdm->name);
  103. return -EINVAL;
  104. }
  105. if (!voltdm->pmic->uv_to_vsel) {
  106. pr_err("%s: PMIC function to convert voltage in uV to vsel not registered. Hence unable to scale voltage for vdd_%s\n",
  107. __func__, voltdm->name);
  108. return -ENODATA;
  109. }
  110. if (!voltdm->read || !voltdm->write) {
  111. pr_err("%s: No read/write API for accessing vdd_%s regs\n",
  112. __func__, voltdm->name);
  113. return -EINVAL;
  114. }
  115. *target_vsel = voltdm->pmic->uv_to_vsel(target_volt);
  116. *current_vsel = voltdm->pmic->uv_to_vsel(voltdm->nominal_volt);
  117. /* Setting the ON voltage to the new target voltage */
  118. vc_cmdval = voltdm->read(vc->cmdval_reg);
  119. vc_cmdval &= ~vc->common->cmd_on_mask;
  120. vc_cmdval |= (*target_vsel << vc->common->cmd_on_shift);
  121. voltdm->write(vc_cmdval, vc->cmdval_reg);
  122. voltdm->vc_param->on = target_volt;
  123. omap_vp_update_errorgain(voltdm, target_volt);
  124. return 0;
  125. }
  126. void omap_vc_post_scale(struct voltagedomain *voltdm,
  127. unsigned long target_volt,
  128. u8 target_vsel, u8 current_vsel)
  129. {
  130. u32 smps_steps = 0, smps_delay = 0;
  131. smps_steps = abs(target_vsel - current_vsel);
  132. /* SMPS slew rate / step size. 2us added as buffer. */
  133. smps_delay = ((smps_steps * voltdm->pmic->step_size) /
  134. voltdm->pmic->slew_rate) + 2;
  135. udelay(smps_delay);
  136. }
  137. /* vc_bypass_scale - VC bypass method of voltage scaling */
  138. int omap_vc_bypass_scale(struct voltagedomain *voltdm,
  139. unsigned long target_volt)
  140. {
  141. struct omap_vc_channel *vc = voltdm->vc;
  142. u32 loop_cnt = 0, retries_cnt = 0;
  143. u32 vc_valid, vc_bypass_val_reg, vc_bypass_value;
  144. u8 target_vsel, current_vsel;
  145. int ret;
  146. ret = omap_vc_pre_scale(voltdm, target_volt, &target_vsel, &current_vsel);
  147. if (ret)
  148. return ret;
  149. vc_valid = vc->common->valid;
  150. vc_bypass_val_reg = vc->common->bypass_val_reg;
  151. vc_bypass_value = (target_vsel << vc->common->data_shift) |
  152. (vc->volt_reg_addr << vc->common->regaddr_shift) |
  153. (vc->i2c_slave_addr << vc->common->slaveaddr_shift);
  154. voltdm->write(vc_bypass_value, vc_bypass_val_reg);
  155. voltdm->write(vc_bypass_value | vc_valid, vc_bypass_val_reg);
  156. vc_bypass_value = voltdm->read(vc_bypass_val_reg);
  157. /*
  158. * Loop till the bypass command is acknowledged from the SMPS.
  159. * NOTE: This is legacy code. The loop count and retry count needs
  160. * to be revisited.
  161. */
  162. while (!(vc_bypass_value & vc_valid)) {
  163. loop_cnt++;
  164. if (retries_cnt > 10) {
  165. pr_warning("%s: Retry count exceeded\n", __func__);
  166. return -ETIMEDOUT;
  167. }
  168. if (loop_cnt > 50) {
  169. retries_cnt++;
  170. loop_cnt = 0;
  171. udelay(10);
  172. }
  173. vc_bypass_value = voltdm->read(vc_bypass_val_reg);
  174. }
  175. omap_vc_post_scale(voltdm, target_volt, target_vsel, current_vsel);
  176. return 0;
  177. }
  178. /**
  179. * omap3_set_i2c_timings - sets i2c sleep timings for a channel
  180. * @voltdm: channel to configure
  181. * @off_mode: select whether retention or off mode values used
  182. *
  183. * Calculates and sets up voltage controller to use I2C based
  184. * voltage scaling for sleep modes. This can be used for either off mode
  185. * or retention. Off mode has additionally an option to use sys_off_mode
  186. * pad, which uses a global signal to program the whole power IC to
  187. * off-mode.
  188. */
  189. static void omap3_set_i2c_timings(struct voltagedomain *voltdm, bool off_mode)
  190. {
  191. unsigned long voltsetup1;
  192. u32 tgt_volt;
  193. if (off_mode)
  194. tgt_volt = voltdm->vc_param->off;
  195. else
  196. tgt_volt = voltdm->vc_param->ret;
  197. voltsetup1 = (voltdm->vc_param->on - tgt_volt) /
  198. voltdm->pmic->slew_rate;
  199. voltsetup1 = voltsetup1 * voltdm->sys_clk.rate / 8 / 1000000 + 1;
  200. voltdm->rmw(voltdm->vfsm->voltsetup_mask,
  201. voltsetup1 << __ffs(voltdm->vfsm->voltsetup_mask),
  202. voltdm->vfsm->voltsetup_reg);
  203. /*
  204. * pmic is not controlling the voltage scaling during retention,
  205. * thus set voltsetup2 to 0
  206. */
  207. voltdm->write(0, OMAP3_PRM_VOLTSETUP2_OFFSET);
  208. }
  209. /**
  210. * omap3_set_off_timings - sets off-mode timings for a channel
  211. * @voltdm: channel to configure
  212. *
  213. * Calculates and sets up off-mode timings for a channel. Off-mode
  214. * can use either I2C based voltage scaling, or alternatively
  215. * sys_off_mode pad can be used to send a global command to power IC.
  216. * This function first checks which mode is being used, and calls
  217. * omap3_set_i2c_timings() if the system is using I2C control mode.
  218. * sys_off_mode has the additional benefit that voltages can be
  219. * scaled to zero volt level with TWL4030 / TWL5030, I2C can only
  220. * scale to 600mV.
  221. */
  222. static void omap3_set_off_timings(struct voltagedomain *voltdm)
  223. {
  224. unsigned long clksetup;
  225. unsigned long voltsetup2;
  226. unsigned long voltsetup2_old;
  227. u32 val;
  228. /* check if sys_off_mode is used to control off-mode voltages */
  229. val = voltdm->read(OMAP3_PRM_VOLTCTRL_OFFSET);
  230. if (!(val & OMAP3430_SEL_OFF_MASK)) {
  231. /* No, omap is controlling them over I2C */
  232. omap3_set_i2c_timings(voltdm, true);
  233. return;
  234. }
  235. clksetup = voltdm->read(OMAP3_PRM_CLKSETUP_OFFSET);
  236. /* voltsetup 2 in us */
  237. voltsetup2 = voltdm->vc_param->on / voltdm->pmic->slew_rate;
  238. /* convert to 32k clk cycles */
  239. voltsetup2 = DIV_ROUND_UP(voltsetup2 * 32768, 1000000);
  240. voltsetup2_old = voltdm->read(OMAP3_PRM_VOLTSETUP2_OFFSET);
  241. /*
  242. * Update voltsetup2 if higher than current value (needed because
  243. * we have multiple channels with different ramp times), also
  244. * update voltoffset always to value recommended by TRM
  245. */
  246. if (voltsetup2 > voltsetup2_old) {
  247. voltdm->write(voltsetup2, OMAP3_PRM_VOLTSETUP2_OFFSET);
  248. voltdm->write(clksetup - voltsetup2,
  249. OMAP3_PRM_VOLTOFFSET_OFFSET);
  250. } else
  251. voltdm->write(clksetup - voltsetup2_old,
  252. OMAP3_PRM_VOLTOFFSET_OFFSET);
  253. /*
  254. * omap is not controlling voltage scaling during off-mode,
  255. * thus set voltsetup1 to 0
  256. */
  257. voltdm->rmw(voltdm->vfsm->voltsetup_mask, 0,
  258. voltdm->vfsm->voltsetup_reg);
  259. /* voltoffset must be clksetup minus voltsetup2 according to TRM */
  260. voltdm->write(clksetup - voltsetup2, OMAP3_PRM_VOLTOFFSET_OFFSET);
  261. }
  262. static void __init omap3_vc_init_channel(struct voltagedomain *voltdm)
  263. {
  264. omap3_set_off_timings(voltdm);
  265. }
  266. /* OMAP4 specific voltage init functions */
  267. static void __init omap4_vc_init_channel(struct voltagedomain *voltdm)
  268. {
  269. static bool is_initialized;
  270. u32 vc_val;
  271. if (is_initialized)
  272. return;
  273. /* XXX These are magic numbers and do not belong! */
  274. vc_val = (0x60 << OMAP4430_SCLL_SHIFT | 0x26 << OMAP4430_SCLH_SHIFT);
  275. voltdm->write(vc_val, OMAP4_PRM_VC_CFG_I2C_CLK_OFFSET);
  276. is_initialized = true;
  277. }
  278. /**
  279. * omap_vc_i2c_init - initialize I2C interface to PMIC
  280. * @voltdm: voltage domain containing VC data
  281. *
  282. * Use PMIC supplied settings for I2C high-speed mode and
  283. * master code (if set) and program the VC I2C configuration
  284. * register.
  285. *
  286. * The VC I2C configuration is common to all VC channels,
  287. * so this function only configures I2C for the first VC
  288. * channel registers. All other VC channels will use the
  289. * same configuration.
  290. */
  291. static void __init omap_vc_i2c_init(struct voltagedomain *voltdm)
  292. {
  293. struct omap_vc_channel *vc = voltdm->vc;
  294. static bool initialized;
  295. static bool i2c_high_speed;
  296. u8 mcode;
  297. if (initialized) {
  298. if (voltdm->pmic->i2c_high_speed != i2c_high_speed)
  299. pr_warn("%s: I2C config for vdd_%s does not match other channels (%u).",
  300. __func__, voltdm->name, i2c_high_speed);
  301. return;
  302. }
  303. i2c_high_speed = voltdm->pmic->i2c_high_speed;
  304. if (i2c_high_speed)
  305. voltdm->rmw(vc->common->i2c_cfg_hsen_mask,
  306. vc->common->i2c_cfg_hsen_mask,
  307. vc->common->i2c_cfg_reg);
  308. mcode = voltdm->pmic->i2c_mcode;
  309. if (mcode)
  310. voltdm->rmw(vc->common->i2c_mcode_mask,
  311. mcode << __ffs(vc->common->i2c_mcode_mask),
  312. vc->common->i2c_cfg_reg);
  313. initialized = true;
  314. }
  315. /**
  316. * omap_vc_calc_vsel - calculate vsel value for a channel
  317. * @voltdm: channel to calculate value for
  318. * @uvolt: microvolt value to convert to vsel
  319. *
  320. * Converts a microvolt value to vsel value for the used PMIC.
  321. * This checks whether the microvolt value is out of bounds, and
  322. * adjusts the value accordingly. If unsupported value detected,
  323. * warning is thrown.
  324. */
  325. static u8 omap_vc_calc_vsel(struct voltagedomain *voltdm, u32 uvolt)
  326. {
  327. if (voltdm->pmic->vddmin > uvolt)
  328. uvolt = voltdm->pmic->vddmin;
  329. if (voltdm->pmic->vddmax < uvolt) {
  330. WARN(1, "%s: voltage not supported by pmic: %u vs max %u\n",
  331. __func__, uvolt, voltdm->pmic->vddmax);
  332. /* Lets try maximum value anyway */
  333. uvolt = voltdm->pmic->vddmax;
  334. }
  335. return voltdm->pmic->uv_to_vsel(uvolt);
  336. }
  337. void __init omap_vc_init_channel(struct voltagedomain *voltdm)
  338. {
  339. struct omap_vc_channel *vc = voltdm->vc;
  340. u8 on_vsel, onlp_vsel, ret_vsel, off_vsel;
  341. u32 val;
  342. if (!voltdm->pmic || !voltdm->pmic->uv_to_vsel) {
  343. pr_err("%s: No PMIC info for vdd_%s\n", __func__, voltdm->name);
  344. return;
  345. }
  346. if (!voltdm->read || !voltdm->write) {
  347. pr_err("%s: No read/write API for accessing vdd_%s regs\n",
  348. __func__, voltdm->name);
  349. return;
  350. }
  351. vc->cfg_channel = 0;
  352. if (vc->flags & OMAP_VC_CHANNEL_CFG_MUTANT)
  353. vc_cfg_bits = &vc_mutant_channel_cfg;
  354. else
  355. vc_cfg_bits = &vc_default_channel_cfg;
  356. /* get PMIC/board specific settings */
  357. vc->i2c_slave_addr = voltdm->pmic->i2c_slave_addr;
  358. vc->volt_reg_addr = voltdm->pmic->volt_reg_addr;
  359. vc->cmd_reg_addr = voltdm->pmic->cmd_reg_addr;
  360. /* Configure the i2c slave address for this VC */
  361. voltdm->rmw(vc->smps_sa_mask,
  362. vc->i2c_slave_addr << __ffs(vc->smps_sa_mask),
  363. vc->smps_sa_reg);
  364. vc->cfg_channel |= vc_cfg_bits->sa;
  365. /*
  366. * Configure the PMIC register addresses.
  367. */
  368. voltdm->rmw(vc->smps_volra_mask,
  369. vc->volt_reg_addr << __ffs(vc->smps_volra_mask),
  370. vc->smps_volra_reg);
  371. vc->cfg_channel |= vc_cfg_bits->rav;
  372. if (vc->cmd_reg_addr) {
  373. voltdm->rmw(vc->smps_cmdra_mask,
  374. vc->cmd_reg_addr << __ffs(vc->smps_cmdra_mask),
  375. vc->smps_cmdra_reg);
  376. vc->cfg_channel |= vc_cfg_bits->rac | vc_cfg_bits->racen;
  377. }
  378. /* Set up the on, inactive, retention and off voltage */
  379. on_vsel = omap_vc_calc_vsel(voltdm, voltdm->vc_param->on);
  380. onlp_vsel = omap_vc_calc_vsel(voltdm, voltdm->vc_param->onlp);
  381. ret_vsel = omap_vc_calc_vsel(voltdm, voltdm->vc_param->ret);
  382. off_vsel = omap_vc_calc_vsel(voltdm, voltdm->vc_param->off);
  383. val = ((on_vsel << vc->common->cmd_on_shift) |
  384. (onlp_vsel << vc->common->cmd_onlp_shift) |
  385. (ret_vsel << vc->common->cmd_ret_shift) |
  386. (off_vsel << vc->common->cmd_off_shift));
  387. voltdm->write(val, vc->cmdval_reg);
  388. vc->cfg_channel |= vc_cfg_bits->cmd;
  389. /* Channel configuration */
  390. omap_vc_config_channel(voltdm);
  391. omap_vc_i2c_init(voltdm);
  392. if (cpu_is_omap34xx())
  393. omap3_vc_init_channel(voltdm);
  394. else if (cpu_is_omap44xx())
  395. omap4_vc_init_channel(voltdm);
  396. }