vc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 <plat/cpu.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"
  107. "vsel not registered. Hence unable to scale voltage"
  108. "for vdd_%s\n", __func__, voltdm->name);
  109. return -ENODATA;
  110. }
  111. if (!voltdm->read || !voltdm->write) {
  112. pr_err("%s: No read/write API for accessing vdd_%s regs\n",
  113. __func__, voltdm->name);
  114. return -EINVAL;
  115. }
  116. *target_vsel = voltdm->pmic->uv_to_vsel(target_volt);
  117. *current_vsel = voltdm->pmic->uv_to_vsel(voltdm->nominal_volt);
  118. /* Setting the ON voltage to the new target voltage */
  119. vc_cmdval = voltdm->read(vc->cmdval_reg);
  120. vc_cmdval &= ~vc->common->cmd_on_mask;
  121. vc_cmdval |= (*target_vsel << vc->common->cmd_on_shift);
  122. voltdm->write(vc_cmdval, vc->cmdval_reg);
  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. static void __init omap3_vfsm_init(struct voltagedomain *voltdm)
  179. {
  180. /*
  181. * Voltage Manager FSM parameters init
  182. * XXX This data should be passed in from the board file
  183. */
  184. voltdm->write(OMAP3_CLKSETUP, OMAP3_PRM_CLKSETUP_OFFSET);
  185. voltdm->write(OMAP3_VOLTOFFSET, OMAP3_PRM_VOLTOFFSET_OFFSET);
  186. voltdm->write(OMAP3_VOLTSETUP2, OMAP3_PRM_VOLTSETUP2_OFFSET);
  187. }
  188. static void __init omap3_vc_init_channel(struct voltagedomain *voltdm)
  189. {
  190. static bool is_initialized;
  191. if (is_initialized)
  192. return;
  193. omap3_vfsm_init(voltdm);
  194. is_initialized = true;
  195. }
  196. /* OMAP4 specific voltage init functions */
  197. static void __init omap4_vc_init_channel(struct voltagedomain *voltdm)
  198. {
  199. static bool is_initialized;
  200. u32 vc_val;
  201. if (is_initialized)
  202. return;
  203. /* XXX These are magic numbers and do not belong! */
  204. vc_val = (0x60 << OMAP4430_SCLL_SHIFT | 0x26 << OMAP4430_SCLH_SHIFT);
  205. voltdm->write(vc_val, OMAP4_PRM_VC_CFG_I2C_CLK_OFFSET);
  206. is_initialized = true;
  207. }
  208. /**
  209. * omap_vc_i2c_init - initialize I2C interface to PMIC
  210. * @voltdm: voltage domain containing VC data
  211. *
  212. * Use PMIC supplied settings for I2C high-speed mode and
  213. * master code (if set) and program the VC I2C configuration
  214. * register.
  215. *
  216. * The VC I2C configuration is common to all VC channels,
  217. * so this function only configures I2C for the first VC
  218. * channel registers. All other VC channels will use the
  219. * same configuration.
  220. */
  221. static void __init omap_vc_i2c_init(struct voltagedomain *voltdm)
  222. {
  223. struct omap_vc_channel *vc = voltdm->vc;
  224. static bool initialized;
  225. static bool i2c_high_speed;
  226. u8 mcode;
  227. if (initialized) {
  228. if (voltdm->pmic->i2c_high_speed != i2c_high_speed)
  229. pr_warn("%s: I2C config for vdd_%s does not match other channels (%u).",
  230. __func__, voltdm->name, i2c_high_speed);
  231. return;
  232. }
  233. i2c_high_speed = voltdm->pmic->i2c_high_speed;
  234. if (i2c_high_speed)
  235. voltdm->rmw(vc->common->i2c_cfg_hsen_mask,
  236. vc->common->i2c_cfg_hsen_mask,
  237. vc->common->i2c_cfg_reg);
  238. mcode = voltdm->pmic->i2c_mcode;
  239. if (mcode)
  240. voltdm->rmw(vc->common->i2c_mcode_mask,
  241. mcode << __ffs(vc->common->i2c_mcode_mask),
  242. vc->common->i2c_cfg_reg);
  243. initialized = true;
  244. }
  245. void __init omap_vc_init_channel(struct voltagedomain *voltdm)
  246. {
  247. struct omap_vc_channel *vc = voltdm->vc;
  248. u8 on_vsel, onlp_vsel, ret_vsel, off_vsel;
  249. u32 val;
  250. if (!voltdm->pmic || !voltdm->pmic->uv_to_vsel) {
  251. pr_err("%s: No PMIC info for vdd_%s\n", __func__, voltdm->name);
  252. return;
  253. }
  254. if (!voltdm->read || !voltdm->write) {
  255. pr_err("%s: No read/write API for accessing vdd_%s regs\n",
  256. __func__, voltdm->name);
  257. return;
  258. }
  259. vc->cfg_channel = 0;
  260. if (vc->flags & OMAP_VC_CHANNEL_CFG_MUTANT)
  261. vc_cfg_bits = &vc_mutant_channel_cfg;
  262. else
  263. vc_cfg_bits = &vc_default_channel_cfg;
  264. /* get PMIC/board specific settings */
  265. vc->i2c_slave_addr = voltdm->pmic->i2c_slave_addr;
  266. vc->volt_reg_addr = voltdm->pmic->volt_reg_addr;
  267. vc->cmd_reg_addr = voltdm->pmic->cmd_reg_addr;
  268. vc->setup_time = voltdm->pmic->volt_setup_time;
  269. /* Configure the i2c slave address for this VC */
  270. voltdm->rmw(vc->smps_sa_mask,
  271. vc->i2c_slave_addr << __ffs(vc->smps_sa_mask),
  272. vc->smps_sa_reg);
  273. vc->cfg_channel |= vc_cfg_bits->sa;
  274. /*
  275. * Configure the PMIC register addresses.
  276. */
  277. voltdm->rmw(vc->smps_volra_mask,
  278. vc->volt_reg_addr << __ffs(vc->smps_volra_mask),
  279. vc->smps_volra_reg);
  280. vc->cfg_channel |= vc_cfg_bits->rav;
  281. if (vc->cmd_reg_addr) {
  282. voltdm->rmw(vc->smps_cmdra_mask,
  283. vc->cmd_reg_addr << __ffs(vc->smps_cmdra_mask),
  284. vc->smps_cmdra_reg);
  285. vc->cfg_channel |= vc_cfg_bits->rac | vc_cfg_bits->racen;
  286. }
  287. /* Set up the on, inactive, retention and off voltage */
  288. on_vsel = voltdm->pmic->uv_to_vsel(voltdm->pmic->on_volt);
  289. onlp_vsel = voltdm->pmic->uv_to_vsel(voltdm->pmic->onlp_volt);
  290. ret_vsel = voltdm->pmic->uv_to_vsel(voltdm->pmic->ret_volt);
  291. off_vsel = voltdm->pmic->uv_to_vsel(voltdm->pmic->off_volt);
  292. val = ((on_vsel << vc->common->cmd_on_shift) |
  293. (onlp_vsel << vc->common->cmd_onlp_shift) |
  294. (ret_vsel << vc->common->cmd_ret_shift) |
  295. (off_vsel << vc->common->cmd_off_shift));
  296. voltdm->write(val, vc->cmdval_reg);
  297. vc->cfg_channel |= vc_cfg_bits->cmd;
  298. /* Channel configuration */
  299. omap_vc_config_channel(voltdm);
  300. /* Configure the setup times */
  301. voltdm->rmw(voltdm->vfsm->voltsetup_mask,
  302. vc->setup_time << __ffs(voltdm->vfsm->voltsetup_mask),
  303. voltdm->vfsm->voltsetup_reg);
  304. omap_vc_i2c_init(voltdm);
  305. if (cpu_is_omap34xx())
  306. omap3_vc_init_channel(voltdm);
  307. else if (cpu_is_omap44xx())
  308. omap4_vc_init_channel(voltdm);
  309. }