vc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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->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. u32 vc_cmdval;
  98. /* Check if sufficient pmic info is available for this vdd */
  99. if (!voltdm->pmic) {
  100. pr_err("%s: Insufficient pmic info to scale the vdd_%s\n",
  101. __func__, voltdm->name);
  102. return -EINVAL;
  103. }
  104. if (!voltdm->pmic->uv_to_vsel) {
  105. pr_err("%s: PMIC function to convert voltage in uV to"
  106. "vsel not registered. Hence unable to scale voltage"
  107. "for vdd_%s\n", __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. omap_vp_update_errorgain(voltdm, target_volt);
  123. return 0;
  124. }
  125. void omap_vc_post_scale(struct voltagedomain *voltdm,
  126. unsigned long target_volt,
  127. u8 target_vsel, u8 current_vsel)
  128. {
  129. u32 smps_steps = 0, smps_delay = 0;
  130. smps_steps = abs(target_vsel - current_vsel);
  131. /* SMPS slew rate / step size. 2us added as buffer. */
  132. smps_delay = ((smps_steps * voltdm->pmic->step_size) /
  133. voltdm->pmic->slew_rate) + 2;
  134. udelay(smps_delay);
  135. }
  136. /* vc_bypass_scale - VC bypass method of voltage scaling */
  137. int omap_vc_bypass_scale(struct voltagedomain *voltdm,
  138. unsigned long target_volt)
  139. {
  140. struct omap_vc_channel *vc = voltdm->vc;
  141. u32 loop_cnt = 0, retries_cnt = 0;
  142. u32 vc_valid, vc_bypass_val_reg, vc_bypass_value;
  143. u8 target_vsel, current_vsel;
  144. int ret;
  145. ret = omap_vc_pre_scale(voltdm, target_volt, &target_vsel, &current_vsel);
  146. if (ret)
  147. return ret;
  148. vc_valid = vc->common->valid;
  149. vc_bypass_val_reg = vc->common->bypass_val_reg;
  150. vc_bypass_value = (target_vsel << vc->common->data_shift) |
  151. (vc->volt_reg_addr << vc->common->regaddr_shift) |
  152. (vc->i2c_slave_addr << vc->common->slaveaddr_shift);
  153. voltdm->write(vc_bypass_value, vc_bypass_val_reg);
  154. voltdm->write(vc_bypass_value | vc_valid, vc_bypass_val_reg);
  155. vc_bypass_value = voltdm->read(vc_bypass_val_reg);
  156. /*
  157. * Loop till the bypass command is acknowledged from the SMPS.
  158. * NOTE: This is legacy code. The loop count and retry count needs
  159. * to be revisited.
  160. */
  161. while (!(vc_bypass_value & vc_valid)) {
  162. loop_cnt++;
  163. if (retries_cnt > 10) {
  164. pr_warning("%s: Retry count exceeded\n", __func__);
  165. return -ETIMEDOUT;
  166. }
  167. if (loop_cnt > 50) {
  168. retries_cnt++;
  169. loop_cnt = 0;
  170. udelay(10);
  171. }
  172. vc_bypass_value = voltdm->read(vc_bypass_val_reg);
  173. }
  174. omap_vc_post_scale(voltdm, target_volt, target_vsel, current_vsel);
  175. return 0;
  176. }
  177. static void __init omap3_vfsm_init(struct voltagedomain *voltdm)
  178. {
  179. /*
  180. * Voltage Manager FSM parameters init
  181. * XXX This data should be passed in from the board file
  182. */
  183. voltdm->write(OMAP3_CLKSETUP, OMAP3_PRM_CLKSETUP_OFFSET);
  184. voltdm->write(OMAP3_VOLTOFFSET, OMAP3_PRM_VOLTOFFSET_OFFSET);
  185. voltdm->write(OMAP3_VOLTSETUP2, OMAP3_PRM_VOLTSETUP2_OFFSET);
  186. }
  187. static void __init omap3_vc_init_channel(struct voltagedomain *voltdm)
  188. {
  189. static bool is_initialized;
  190. if (is_initialized)
  191. return;
  192. omap3_vfsm_init(voltdm);
  193. is_initialized = true;
  194. }
  195. /* OMAP4 specific voltage init functions */
  196. static void __init omap4_vc_init_channel(struct voltagedomain *voltdm)
  197. {
  198. static bool is_initialized;
  199. u32 vc_val;
  200. if (is_initialized)
  201. return;
  202. /* XXX These are magic numbers and do not belong! */
  203. vc_val = (0x60 << OMAP4430_SCLL_SHIFT | 0x26 << OMAP4430_SCLH_SHIFT);
  204. voltdm->write(vc_val, OMAP4_PRM_VC_CFG_I2C_CLK_OFFSET);
  205. is_initialized = true;
  206. }
  207. /**
  208. * omap_vc_i2c_init - initialize I2C interface to PMIC
  209. * @voltdm: voltage domain containing VC data
  210. *
  211. * Use PMIC supplied settings for I2C high-speed mode and
  212. * master code (if set) and program the VC I2C configuration
  213. * register.
  214. *
  215. * The VC I2C configuration is common to all VC channels,
  216. * so this function only configures I2C for the first VC
  217. * channel registers. All other VC channels will use the
  218. * same configuration.
  219. */
  220. static void __init omap_vc_i2c_init(struct voltagedomain *voltdm)
  221. {
  222. struct omap_vc_channel *vc = voltdm->vc;
  223. static bool initialized;
  224. static bool i2c_high_speed;
  225. u8 mcode;
  226. if (initialized) {
  227. if (voltdm->pmic->i2c_high_speed != i2c_high_speed)
  228. pr_warn("%s: I2C config for vdd_%s does not match other channels (%u).",
  229. __func__, voltdm->name, i2c_high_speed);
  230. return;
  231. }
  232. i2c_high_speed = voltdm->pmic->i2c_high_speed;
  233. if (i2c_high_speed)
  234. voltdm->rmw(vc->common->i2c_cfg_hsen_mask,
  235. vc->common->i2c_cfg_hsen_mask,
  236. vc->common->i2c_cfg_reg);
  237. mcode = voltdm->pmic->i2c_mcode;
  238. if (mcode)
  239. voltdm->rmw(vc->common->i2c_mcode_mask,
  240. mcode << __ffs(vc->common->i2c_mcode_mask),
  241. vc->common->i2c_cfg_reg);
  242. initialized = true;
  243. }
  244. void __init omap_vc_init_channel(struct voltagedomain *voltdm)
  245. {
  246. struct omap_vc_channel *vc = voltdm->vc;
  247. u8 on_vsel, onlp_vsel, ret_vsel, off_vsel;
  248. u32 val;
  249. if (!voltdm->pmic || !voltdm->pmic->uv_to_vsel) {
  250. pr_err("%s: No PMIC info for vdd_%s\n", __func__, voltdm->name);
  251. return;
  252. }
  253. if (!voltdm->read || !voltdm->write) {
  254. pr_err("%s: No read/write API for accessing vdd_%s regs\n",
  255. __func__, voltdm->name);
  256. return;
  257. }
  258. vc->cfg_channel = 0;
  259. if (vc->flags & OMAP_VC_CHANNEL_CFG_MUTANT)
  260. vc_cfg_bits = &vc_mutant_channel_cfg;
  261. else
  262. vc_cfg_bits = &vc_default_channel_cfg;
  263. /* get PMIC/board specific settings */
  264. vc->i2c_slave_addr = voltdm->pmic->i2c_slave_addr;
  265. vc->volt_reg_addr = voltdm->pmic->volt_reg_addr;
  266. vc->cmd_reg_addr = voltdm->pmic->cmd_reg_addr;
  267. vc->setup_time = voltdm->pmic->volt_setup_time;
  268. /* Configure the i2c slave address for this VC */
  269. voltdm->rmw(vc->smps_sa_mask,
  270. vc->i2c_slave_addr << __ffs(vc->smps_sa_mask),
  271. vc->smps_sa_reg);
  272. vc->cfg_channel |= vc_cfg_bits->sa;
  273. /*
  274. * Configure the PMIC register addresses.
  275. */
  276. voltdm->rmw(vc->smps_volra_mask,
  277. vc->volt_reg_addr << __ffs(vc->smps_volra_mask),
  278. vc->smps_volra_reg);
  279. vc->cfg_channel |= vc_cfg_bits->rav;
  280. if (vc->cmd_reg_addr) {
  281. voltdm->rmw(vc->smps_cmdra_mask,
  282. vc->cmd_reg_addr << __ffs(vc->smps_cmdra_mask),
  283. vc->smps_cmdra_reg);
  284. vc->cfg_channel |= vc_cfg_bits->rac | vc_cfg_bits->racen;
  285. }
  286. /* Set up the on, inactive, retention and off voltage */
  287. on_vsel = voltdm->pmic->uv_to_vsel(voltdm->pmic->on_volt);
  288. onlp_vsel = voltdm->pmic->uv_to_vsel(voltdm->pmic->onlp_volt);
  289. ret_vsel = voltdm->pmic->uv_to_vsel(voltdm->pmic->ret_volt);
  290. off_vsel = voltdm->pmic->uv_to_vsel(voltdm->pmic->off_volt);
  291. val = ((on_vsel << vc->common->cmd_on_shift) |
  292. (onlp_vsel << vc->common->cmd_onlp_shift) |
  293. (ret_vsel << vc->common->cmd_ret_shift) |
  294. (off_vsel << vc->common->cmd_off_shift));
  295. voltdm->write(val, vc->cmdval_reg);
  296. vc->cfg_channel |= vc_cfg_bits->cmd;
  297. /* Channel configuration */
  298. omap_vc_config_channel(voltdm);
  299. /* Configure the setup times */
  300. voltdm->rmw(voltdm->vfsm->voltsetup_mask,
  301. vc->setup_time << __ffs(voltdm->vfsm->voltsetup_mask),
  302. voltdm->vfsm->voltsetup_reg);
  303. omap_vc_i2c_init(voltdm);
  304. if (cpu_is_omap34xx())
  305. omap3_vc_init_channel(voltdm);
  306. else if (cpu_is_omap44xx())
  307. omap4_vc_init_channel(voltdm);
  308. }