vc.c 11 KB

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