vc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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 <linux/io.h>
  15. #include <asm/div64.h>
  16. #include "iomap.h"
  17. #include "soc.h"
  18. #include "voltage.h"
  19. #include "vc.h"
  20. #include "prm-regbits-34xx.h"
  21. #include "prm-regbits-44xx.h"
  22. #include "prm44xx.h"
  23. #include "pm.h"
  24. #include "scrm44xx.h"
  25. #include "control.h"
  26. /**
  27. * struct omap_vc_channel_cfg - describe the cfg_channel bitfield
  28. * @sa: bit for slave address
  29. * @rav: bit for voltage configuration register
  30. * @rac: bit for command configuration register
  31. * @racen: enable bit for RAC
  32. * @cmd: bit for command value set selection
  33. *
  34. * Channel configuration bits, common for OMAP3+
  35. * OMAP3 register: PRM_VC_CH_CONF
  36. * OMAP4 register: PRM_VC_CFG_CHANNEL
  37. * OMAP5 register: PRM_VC_SMPS_<voltdm>_CONFIG
  38. */
  39. struct omap_vc_channel_cfg {
  40. u8 sa;
  41. u8 rav;
  42. u8 rac;
  43. u8 racen;
  44. u8 cmd;
  45. };
  46. static struct omap_vc_channel_cfg vc_default_channel_cfg = {
  47. .sa = BIT(0),
  48. .rav = BIT(1),
  49. .rac = BIT(2),
  50. .racen = BIT(3),
  51. .cmd = BIT(4),
  52. };
  53. /*
  54. * On OMAP3+, all VC channels have the above default bitfield
  55. * configuration, except the OMAP4 MPU channel. This appears
  56. * to be a freak accident as every other VC channel has the
  57. * default configuration, thus creating a mutant channel config.
  58. */
  59. static struct omap_vc_channel_cfg vc_mutant_channel_cfg = {
  60. .sa = BIT(0),
  61. .rav = BIT(2),
  62. .rac = BIT(3),
  63. .racen = BIT(4),
  64. .cmd = BIT(1),
  65. };
  66. static struct omap_vc_channel_cfg *vc_cfg_bits;
  67. /* Default I2C trace length on pcb, 6.3cm. Used for capacitance calculations. */
  68. static u32 sr_i2c_pcb_length = 63;
  69. #define CFG_CHANNEL_MASK 0x1f
  70. /**
  71. * omap_vc_config_channel - configure VC channel to PMIC mappings
  72. * @voltdm: pointer to voltagdomain defining the desired VC channel
  73. *
  74. * Configures the VC channel to PMIC mappings for the following
  75. * PMIC settings
  76. * - i2c slave address (SA)
  77. * - voltage configuration address (RAV)
  78. * - command configuration address (RAC) and enable bit (RACEN)
  79. * - command values for ON, ONLP, RET and OFF (CMD)
  80. *
  81. * This function currently only allows flexible configuration of the
  82. * non-default channel. Starting with OMAP4, there are more than 2
  83. * channels, with one defined as the default (on OMAP4, it's MPU.)
  84. * Only the non-default channel can be configured.
  85. */
  86. static int omap_vc_config_channel(struct voltagedomain *voltdm)
  87. {
  88. struct omap_vc_channel *vc = voltdm->vc;
  89. /*
  90. * For default channel, the only configurable bit is RACEN.
  91. * All others must stay at zero (see function comment above.)
  92. */
  93. if (vc->flags & OMAP_VC_CHANNEL_DEFAULT)
  94. vc->cfg_channel &= vc_cfg_bits->racen;
  95. voltdm->rmw(CFG_CHANNEL_MASK << vc->cfg_channel_sa_shift,
  96. vc->cfg_channel << vc->cfg_channel_sa_shift,
  97. vc->cfg_channel_reg);
  98. return 0;
  99. }
  100. /* Voltage scale and accessory APIs */
  101. int omap_vc_pre_scale(struct voltagedomain *voltdm,
  102. unsigned long target_volt,
  103. u8 *target_vsel, u8 *current_vsel)
  104. {
  105. struct omap_vc_channel *vc = voltdm->vc;
  106. u32 vc_cmdval;
  107. /* Check if sufficient pmic info is available for this vdd */
  108. if (!voltdm->pmic) {
  109. pr_err("%s: Insufficient pmic info to scale the vdd_%s\n",
  110. __func__, voltdm->name);
  111. return -EINVAL;
  112. }
  113. if (!voltdm->pmic->uv_to_vsel) {
  114. pr_err("%s: PMIC function to convert voltage in uV to vsel not registered. Hence unable to scale voltage for vdd_%s\n",
  115. __func__, voltdm->name);
  116. return -ENODATA;
  117. }
  118. if (!voltdm->read || !voltdm->write) {
  119. pr_err("%s: No read/write API for accessing vdd_%s regs\n",
  120. __func__, voltdm->name);
  121. return -EINVAL;
  122. }
  123. *target_vsel = voltdm->pmic->uv_to_vsel(target_volt);
  124. *current_vsel = voltdm->pmic->uv_to_vsel(voltdm->nominal_volt);
  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. voltdm->vc_param->on = target_volt;
  131. omap_vp_update_errorgain(voltdm, target_volt);
  132. return 0;
  133. }
  134. void omap_vc_post_scale(struct voltagedomain *voltdm,
  135. unsigned long target_volt,
  136. u8 target_vsel, u8 current_vsel)
  137. {
  138. u32 smps_steps = 0, smps_delay = 0;
  139. smps_steps = abs(target_vsel - current_vsel);
  140. /* SMPS slew rate / step size. 2us added as buffer. */
  141. smps_delay = ((smps_steps * voltdm->pmic->step_size) /
  142. voltdm->pmic->slew_rate) + 2;
  143. udelay(smps_delay);
  144. }
  145. /* vc_bypass_scale - VC bypass method of voltage scaling */
  146. int omap_vc_bypass_scale(struct voltagedomain *voltdm,
  147. unsigned long target_volt)
  148. {
  149. struct omap_vc_channel *vc = voltdm->vc;
  150. u32 loop_cnt = 0, retries_cnt = 0;
  151. u32 vc_valid, vc_bypass_val_reg, vc_bypass_value;
  152. u8 target_vsel, current_vsel;
  153. int ret;
  154. ret = omap_vc_pre_scale(voltdm, target_volt, &target_vsel, &current_vsel);
  155. if (ret)
  156. return ret;
  157. vc_valid = vc->common->valid;
  158. vc_bypass_val_reg = vc->common->bypass_val_reg;
  159. vc_bypass_value = (target_vsel << vc->common->data_shift) |
  160. (vc->volt_reg_addr << vc->common->regaddr_shift) |
  161. (vc->i2c_slave_addr << vc->common->slaveaddr_shift);
  162. voltdm->write(vc_bypass_value, vc_bypass_val_reg);
  163. voltdm->write(vc_bypass_value | vc_valid, vc_bypass_val_reg);
  164. vc_bypass_value = voltdm->read(vc_bypass_val_reg);
  165. /*
  166. * Loop till the bypass command is acknowledged from the SMPS.
  167. * NOTE: This is legacy code. The loop count and retry count needs
  168. * to be revisited.
  169. */
  170. while (!(vc_bypass_value & vc_valid)) {
  171. loop_cnt++;
  172. if (retries_cnt > 10) {
  173. pr_warn("%s: Retry count exceeded\n", __func__);
  174. return -ETIMEDOUT;
  175. }
  176. if (loop_cnt > 50) {
  177. retries_cnt++;
  178. loop_cnt = 0;
  179. udelay(10);
  180. }
  181. vc_bypass_value = voltdm->read(vc_bypass_val_reg);
  182. }
  183. omap_vc_post_scale(voltdm, target_volt, target_vsel, current_vsel);
  184. return 0;
  185. }
  186. /* Convert microsecond value to number of 32kHz clock cycles */
  187. static inline u32 omap_usec_to_32k(u32 usec)
  188. {
  189. return DIV_ROUND_UP_ULL(32768ULL * (u64)usec, 1000000ULL);
  190. }
  191. struct omap3_vc_timings {
  192. u32 voltsetup1;
  193. u32 voltsetup2;
  194. };
  195. struct omap3_vc {
  196. struct voltagedomain *vd;
  197. u32 voltctrl;
  198. u32 voltsetup1;
  199. u32 voltsetup2;
  200. struct omap3_vc_timings timings[2];
  201. };
  202. static struct omap3_vc vc;
  203. void omap3_vc_set_pmic_signaling(int core_next_state)
  204. {
  205. struct voltagedomain *vd = vc.vd;
  206. struct omap3_vc_timings *c = vc.timings;
  207. u32 voltctrl, voltsetup1, voltsetup2;
  208. voltctrl = vc.voltctrl;
  209. voltsetup1 = vc.voltsetup1;
  210. voltsetup2 = vc.voltsetup2;
  211. switch (core_next_state) {
  212. case PWRDM_POWER_OFF:
  213. voltctrl &= ~(OMAP3430_PRM_VOLTCTRL_AUTO_RET |
  214. OMAP3430_PRM_VOLTCTRL_AUTO_SLEEP);
  215. voltctrl |= OMAP3430_PRM_VOLTCTRL_AUTO_OFF;
  216. if (voltctrl & OMAP3430_PRM_VOLTCTRL_SEL_OFF)
  217. voltsetup2 = c->voltsetup2;
  218. else
  219. voltsetup1 = c->voltsetup1;
  220. break;
  221. case PWRDM_POWER_RET:
  222. default:
  223. c++;
  224. voltctrl &= ~(OMAP3430_PRM_VOLTCTRL_AUTO_OFF |
  225. OMAP3430_PRM_VOLTCTRL_AUTO_SLEEP);
  226. voltctrl |= OMAP3430_PRM_VOLTCTRL_AUTO_RET;
  227. voltsetup1 = c->voltsetup1;
  228. break;
  229. }
  230. if (voltctrl != vc.voltctrl) {
  231. vd->write(voltctrl, OMAP3_PRM_VOLTCTRL_OFFSET);
  232. vc.voltctrl = voltctrl;
  233. }
  234. if (voltsetup1 != vc.voltsetup1) {
  235. vd->write(c->voltsetup1,
  236. OMAP3_PRM_VOLTSETUP1_OFFSET);
  237. vc.voltsetup1 = voltsetup1;
  238. }
  239. if (voltsetup2 != vc.voltsetup2) {
  240. vd->write(c->voltsetup2,
  241. OMAP3_PRM_VOLTSETUP2_OFFSET);
  242. vc.voltsetup2 = voltsetup2;
  243. }
  244. }
  245. #define PRM_POLCTRL_TWL_MASK (OMAP3430_PRM_POLCTRL_CLKREQ_POL | \
  246. OMAP3430_PRM_POLCTRL_CLKREQ_POL)
  247. #define PRM_POLCTRL_TWL_VAL OMAP3430_PRM_POLCTRL_CLKREQ_POL
  248. /*
  249. * Configure signal polarity for sys_clkreq and sys_off_mode pins
  250. * as the default values are wrong and can cause the system to hang
  251. * if any twl4030 scripts are loaded.
  252. */
  253. static void __init omap3_vc_init_pmic_signaling(struct voltagedomain *voltdm)
  254. {
  255. u32 val;
  256. if (vc.vd)
  257. return;
  258. vc.vd = voltdm;
  259. val = voltdm->read(OMAP3_PRM_POLCTRL_OFFSET);
  260. if (!(val & OMAP3430_PRM_POLCTRL_CLKREQ_POL) ||
  261. (val & OMAP3430_PRM_POLCTRL_OFFMODE_POL)) {
  262. val |= OMAP3430_PRM_POLCTRL_CLKREQ_POL;
  263. val &= ~OMAP3430_PRM_POLCTRL_OFFMODE_POL;
  264. pr_debug("PM: fixing sys_clkreq and sys_off_mode polarity to 0x%x\n",
  265. val);
  266. voltdm->write(val, OMAP3_PRM_POLCTRL_OFFSET);
  267. }
  268. /*
  269. * By default let's use I2C4 signaling for retention idle
  270. * and sys_off_mode pin signaling for off idle. This way we
  271. * have sys_clk_req pin go down for retention and both
  272. * sys_clk_req and sys_off_mode pins will go down for off
  273. * idle. And we can also scale voltages to zero for off-idle.
  274. * Note that no actual voltage scaling during off-idle will
  275. * happen unless the board specific twl4030 PMIC scripts are
  276. * loaded. See also omap_vc_i2c_init for comments regarding
  277. * erratum i531.
  278. */
  279. val = voltdm->read(OMAP3_PRM_VOLTCTRL_OFFSET);
  280. if (!(val & OMAP3430_PRM_VOLTCTRL_SEL_OFF)) {
  281. val |= OMAP3430_PRM_VOLTCTRL_SEL_OFF;
  282. pr_debug("PM: setting voltctrl sys_off_mode signaling to 0x%x\n",
  283. val);
  284. voltdm->write(val, OMAP3_PRM_VOLTCTRL_OFFSET);
  285. }
  286. vc.voltctrl = val;
  287. omap3_vc_set_pmic_signaling(PWRDM_POWER_ON);
  288. }
  289. static void omap3_init_voltsetup1(struct voltagedomain *voltdm,
  290. struct omap3_vc_timings *c, u32 idle)
  291. {
  292. unsigned long val;
  293. val = (voltdm->vc_param->on - idle) / voltdm->pmic->slew_rate;
  294. val *= voltdm->sys_clk.rate / 8 / 1000000 + 1;
  295. val <<= __ffs(voltdm->vfsm->voltsetup_mask);
  296. c->voltsetup1 &= ~voltdm->vfsm->voltsetup_mask;
  297. c->voltsetup1 |= val;
  298. }
  299. /**
  300. * omap3_set_i2c_timings - sets i2c sleep timings for a channel
  301. * @voltdm: channel to configure
  302. * @off_mode: select whether retention or off mode values used
  303. *
  304. * Calculates and sets up voltage controller to use I2C based
  305. * voltage scaling for sleep modes. This can be used for either off mode
  306. * or retention. Off mode has additionally an option to use sys_off_mode
  307. * pad, which uses a global signal to program the whole power IC to
  308. * off-mode.
  309. *
  310. * Note that pmic is not controlling the voltage scaling during
  311. * retention signaled over I2C4, so we can keep voltsetup2 as 0.
  312. * And the oscillator is not shut off over I2C4, so no need to
  313. * set clksetup.
  314. */
  315. static void omap3_set_i2c_timings(struct voltagedomain *voltdm)
  316. {
  317. struct omap3_vc_timings *c = vc.timings;
  318. /* Configure PRWDM_POWER_OFF over I2C4 */
  319. omap3_init_voltsetup1(voltdm, c, voltdm->vc_param->off);
  320. c++;
  321. /* Configure PRWDM_POWER_RET over I2C4 */
  322. omap3_init_voltsetup1(voltdm, c, voltdm->vc_param->ret);
  323. }
  324. /**
  325. * omap3_set_off_timings - sets off-mode timings for a channel
  326. * @voltdm: channel to configure
  327. *
  328. * Calculates and sets up off-mode timings for a channel. Off-mode
  329. * can use either I2C based voltage scaling, or alternatively
  330. * sys_off_mode pad can be used to send a global command to power IC.n,
  331. * sys_off_mode has the additional benefit that voltages can be
  332. * scaled to zero volt level with TWL4030 / TWL5030, I2C can only
  333. * scale to 600mV.
  334. *
  335. * Note that omap is not controlling the voltage scaling during
  336. * off idle signaled by sys_off_mode, so we can keep voltsetup1
  337. * as 0.
  338. */
  339. static void omap3_set_off_timings(struct voltagedomain *voltdm)
  340. {
  341. struct omap3_vc_timings *c = vc.timings;
  342. u32 tstart, tshut, clksetup, voltoffset;
  343. if (c->voltsetup2)
  344. return;
  345. omap_pm_get_oscillator(&tstart, &tshut);
  346. if (tstart == ULONG_MAX) {
  347. pr_debug("PM: oscillator start-up time not initialized, using 10ms\n");
  348. clksetup = omap_usec_to_32k(10000);
  349. } else {
  350. clksetup = omap_usec_to_32k(tstart);
  351. }
  352. /*
  353. * For twl4030 errata 27, we need to allow minimum ~488.32 us wait to
  354. * switch from HFCLKIN to internal oscillator. That means timings
  355. * have voltoffset fixed to 0xa in rounded up 32 KiHz cycles. And
  356. * that means we can calculate the value based on the oscillator
  357. * start-up time since voltoffset2 = clksetup - voltoffset.
  358. */
  359. voltoffset = omap_usec_to_32k(488);
  360. c->voltsetup2 = clksetup - voltoffset;
  361. voltdm->write(clksetup, OMAP3_PRM_CLKSETUP_OFFSET);
  362. voltdm->write(voltoffset, OMAP3_PRM_VOLTOFFSET_OFFSET);
  363. }
  364. static void __init omap3_vc_init_channel(struct voltagedomain *voltdm)
  365. {
  366. omap3_vc_init_pmic_signaling(voltdm);
  367. omap3_set_off_timings(voltdm);
  368. omap3_set_i2c_timings(voltdm);
  369. }
  370. /**
  371. * omap4_calc_volt_ramp - calculates voltage ramping delays on omap4
  372. * @voltdm: channel to calculate values for
  373. * @voltage_diff: voltage difference in microvolts
  374. *
  375. * Calculates voltage ramp prescaler + counter values for a voltage
  376. * difference on omap4. Returns a field value suitable for writing to
  377. * VOLTSETUP register for a channel in following format:
  378. * bits[8:9] prescaler ... bits[0:5] counter. See OMAP4 TRM for reference.
  379. */
  380. static u32 omap4_calc_volt_ramp(struct voltagedomain *voltdm, u32 voltage_diff)
  381. {
  382. u32 prescaler;
  383. u32 cycles;
  384. u32 time;
  385. time = voltage_diff / voltdm->pmic->slew_rate;
  386. cycles = voltdm->sys_clk.rate / 1000 * time / 1000;
  387. cycles /= 64;
  388. prescaler = 0;
  389. /* shift to next prescaler until no overflow */
  390. /* scale for div 256 = 64 * 4 */
  391. if (cycles > 63) {
  392. cycles /= 4;
  393. prescaler++;
  394. }
  395. /* scale for div 512 = 256 * 2 */
  396. if (cycles > 63) {
  397. cycles /= 2;
  398. prescaler++;
  399. }
  400. /* scale for div 2048 = 512 * 4 */
  401. if (cycles > 63) {
  402. cycles /= 4;
  403. prescaler++;
  404. }
  405. /* check for overflow => invalid ramp time */
  406. if (cycles > 63) {
  407. pr_warn("%s: invalid setuptime for vdd_%s\n", __func__,
  408. voltdm->name);
  409. return 0;
  410. }
  411. cycles++;
  412. return (prescaler << OMAP4430_RAMP_UP_PRESCAL_SHIFT) |
  413. (cycles << OMAP4430_RAMP_UP_COUNT_SHIFT);
  414. }
  415. /**
  416. * omap4_usec_to_val_scrm - convert microsecond value to SCRM module bitfield
  417. * @usec: microseconds
  418. * @shift: number of bits to shift left
  419. * @mask: bitfield mask
  420. *
  421. * Converts microsecond value to OMAP4 SCRM bitfield. Bitfield is
  422. * shifted to requested position, and checked agains the mask value.
  423. * If larger, forced to the max value of the field (i.e. the mask itself.)
  424. * Returns the SCRM bitfield value.
  425. */
  426. static u32 omap4_usec_to_val_scrm(u32 usec, int shift, u32 mask)
  427. {
  428. u32 val;
  429. val = omap_usec_to_32k(usec) << shift;
  430. /* Check for overflow, if yes, force to max value */
  431. if (val > mask)
  432. val = mask;
  433. return val;
  434. }
  435. /**
  436. * omap4_set_timings - set voltage ramp timings for a channel
  437. * @voltdm: channel to configure
  438. * @off_mode: whether off-mode values are used
  439. *
  440. * Calculates and sets the voltage ramp up / down values for a channel.
  441. */
  442. static void omap4_set_timings(struct voltagedomain *voltdm, bool off_mode)
  443. {
  444. u32 val;
  445. u32 ramp;
  446. int offset;
  447. u32 tstart, tshut;
  448. if (off_mode) {
  449. ramp = omap4_calc_volt_ramp(voltdm,
  450. voltdm->vc_param->on - voltdm->vc_param->off);
  451. offset = voltdm->vfsm->voltsetup_off_reg;
  452. } else {
  453. ramp = omap4_calc_volt_ramp(voltdm,
  454. voltdm->vc_param->on - voltdm->vc_param->ret);
  455. offset = voltdm->vfsm->voltsetup_reg;
  456. }
  457. if (!ramp)
  458. return;
  459. val = voltdm->read(offset);
  460. val |= ramp << OMAP4430_RAMP_DOWN_COUNT_SHIFT;
  461. val |= ramp << OMAP4430_RAMP_UP_COUNT_SHIFT;
  462. voltdm->write(val, offset);
  463. omap_pm_get_oscillator(&tstart, &tshut);
  464. val = omap4_usec_to_val_scrm(tstart, OMAP4_SETUPTIME_SHIFT,
  465. OMAP4_SETUPTIME_MASK);
  466. val |= omap4_usec_to_val_scrm(tshut, OMAP4_DOWNTIME_SHIFT,
  467. OMAP4_DOWNTIME_MASK);
  468. writel_relaxed(val, OMAP4_SCRM_CLKSETUPTIME);
  469. }
  470. /* OMAP4 specific voltage init functions */
  471. static void __init omap4_vc_init_channel(struct voltagedomain *voltdm)
  472. {
  473. omap4_set_timings(voltdm, true);
  474. omap4_set_timings(voltdm, false);
  475. }
  476. struct i2c_init_data {
  477. u8 loadbits;
  478. u8 load;
  479. u8 hsscll_38_4;
  480. u8 hsscll_26;
  481. u8 hsscll_19_2;
  482. u8 hsscll_16_8;
  483. u8 hsscll_12;
  484. };
  485. static const struct i2c_init_data const omap4_i2c_timing_data[] __initconst = {
  486. {
  487. .load = 50,
  488. .loadbits = 0x3,
  489. .hsscll_38_4 = 13,
  490. .hsscll_26 = 11,
  491. .hsscll_19_2 = 9,
  492. .hsscll_16_8 = 9,
  493. .hsscll_12 = 8,
  494. },
  495. {
  496. .load = 25,
  497. .loadbits = 0x2,
  498. .hsscll_38_4 = 13,
  499. .hsscll_26 = 11,
  500. .hsscll_19_2 = 9,
  501. .hsscll_16_8 = 9,
  502. .hsscll_12 = 8,
  503. },
  504. {
  505. .load = 12,
  506. .loadbits = 0x1,
  507. .hsscll_38_4 = 11,
  508. .hsscll_26 = 10,
  509. .hsscll_19_2 = 9,
  510. .hsscll_16_8 = 9,
  511. .hsscll_12 = 8,
  512. },
  513. {
  514. .load = 0,
  515. .loadbits = 0x0,
  516. .hsscll_38_4 = 12,
  517. .hsscll_26 = 10,
  518. .hsscll_19_2 = 9,
  519. .hsscll_16_8 = 8,
  520. .hsscll_12 = 8,
  521. },
  522. };
  523. /**
  524. * omap4_vc_i2c_timing_init - sets up board I2C timing parameters
  525. * @voltdm: voltagedomain pointer to get data from
  526. *
  527. * Use PMIC + board supplied settings for calculating the total I2C
  528. * channel capacitance and set the timing parameters based on this.
  529. * Pre-calculated values are provided in data tables, as it is not
  530. * too straightforward to calculate these runtime.
  531. */
  532. static void __init omap4_vc_i2c_timing_init(struct voltagedomain *voltdm)
  533. {
  534. u32 capacitance;
  535. u32 val;
  536. u16 hsscll;
  537. const struct i2c_init_data *i2c_data;
  538. if (!voltdm->pmic->i2c_high_speed) {
  539. pr_warn("%s: only high speed supported!\n", __func__);
  540. return;
  541. }
  542. /* PCB trace capacitance, 0.125pF / mm => mm / 8 */
  543. capacitance = DIV_ROUND_UP(sr_i2c_pcb_length, 8);
  544. /* OMAP pad capacitance */
  545. capacitance += 4;
  546. /* PMIC pad capacitance */
  547. capacitance += voltdm->pmic->i2c_pad_load;
  548. /* Search for capacitance match in the table */
  549. i2c_data = omap4_i2c_timing_data;
  550. while (i2c_data->load > capacitance)
  551. i2c_data++;
  552. /* Select proper values based on sysclk frequency */
  553. switch (voltdm->sys_clk.rate) {
  554. case 38400000:
  555. hsscll = i2c_data->hsscll_38_4;
  556. break;
  557. case 26000000:
  558. hsscll = i2c_data->hsscll_26;
  559. break;
  560. case 19200000:
  561. hsscll = i2c_data->hsscll_19_2;
  562. break;
  563. case 16800000:
  564. hsscll = i2c_data->hsscll_16_8;
  565. break;
  566. case 12000000:
  567. hsscll = i2c_data->hsscll_12;
  568. break;
  569. default:
  570. pr_warn("%s: unsupported sysclk rate: %d!\n", __func__,
  571. voltdm->sys_clk.rate);
  572. return;
  573. }
  574. /* Loadbits define pull setup for the I2C channels */
  575. val = i2c_data->loadbits << 25 | i2c_data->loadbits << 29;
  576. /* Write to SYSCTRL_PADCONF_WKUP_CTRL_I2C_2 to setup I2C pull */
  577. writel_relaxed(val, OMAP2_L4_IO_ADDRESS(OMAP4_CTRL_MODULE_PAD_WKUP +
  578. OMAP4_CTRL_MODULE_PAD_WKUP_CONTROL_I2C_2));
  579. /* HSSCLH can always be zero */
  580. val = hsscll << OMAP4430_HSSCLL_SHIFT;
  581. val |= (0x28 << OMAP4430_SCLL_SHIFT | 0x2c << OMAP4430_SCLH_SHIFT);
  582. /* Write setup times to I2C config register */
  583. voltdm->write(val, OMAP4_PRM_VC_CFG_I2C_CLK_OFFSET);
  584. }
  585. /**
  586. * omap_vc_i2c_init - initialize I2C interface to PMIC
  587. * @voltdm: voltage domain containing VC data
  588. *
  589. * Use PMIC supplied settings for I2C high-speed mode and
  590. * master code (if set) and program the VC I2C configuration
  591. * register.
  592. *
  593. * The VC I2C configuration is common to all VC channels,
  594. * so this function only configures I2C for the first VC
  595. * channel registers. All other VC channels will use the
  596. * same configuration.
  597. */
  598. static void __init omap_vc_i2c_init(struct voltagedomain *voltdm)
  599. {
  600. struct omap_vc_channel *vc = voltdm->vc;
  601. static bool initialized;
  602. static bool i2c_high_speed;
  603. u8 mcode;
  604. if (initialized) {
  605. if (voltdm->pmic->i2c_high_speed != i2c_high_speed)
  606. pr_warn("%s: I2C config for vdd_%s does not match other channels (%u).\n",
  607. __func__, voltdm->name, i2c_high_speed);
  608. return;
  609. }
  610. /*
  611. * Note that for omap3 OMAP3430_SREN_MASK clears SREN to work around
  612. * erratum i531 "Extra Power Consumed When Repeated Start Operation
  613. * Mode Is Enabled on I2C Interface Dedicated for Smart Reflex (I2C4)".
  614. * Otherwise I2C4 eventually leads into about 23mW extra power being
  615. * consumed even during off idle using VMODE.
  616. */
  617. i2c_high_speed = voltdm->pmic->i2c_high_speed;
  618. if (i2c_high_speed)
  619. voltdm->rmw(vc->common->i2c_cfg_clear_mask,
  620. vc->common->i2c_cfg_hsen_mask,
  621. vc->common->i2c_cfg_reg);
  622. mcode = voltdm->pmic->i2c_mcode;
  623. if (mcode)
  624. voltdm->rmw(vc->common->i2c_mcode_mask,
  625. mcode << __ffs(vc->common->i2c_mcode_mask),
  626. vc->common->i2c_cfg_reg);
  627. if (cpu_is_omap44xx())
  628. omap4_vc_i2c_timing_init(voltdm);
  629. initialized = true;
  630. }
  631. /**
  632. * omap_vc_calc_vsel - calculate vsel value for a channel
  633. * @voltdm: channel to calculate value for
  634. * @uvolt: microvolt value to convert to vsel
  635. *
  636. * Converts a microvolt value to vsel value for the used PMIC.
  637. * This checks whether the microvolt value is out of bounds, and
  638. * adjusts the value accordingly. If unsupported value detected,
  639. * warning is thrown.
  640. */
  641. static u8 omap_vc_calc_vsel(struct voltagedomain *voltdm, u32 uvolt)
  642. {
  643. if (voltdm->pmic->vddmin > uvolt)
  644. uvolt = voltdm->pmic->vddmin;
  645. if (voltdm->pmic->vddmax < uvolt) {
  646. WARN(1, "%s: voltage not supported by pmic: %u vs max %u\n",
  647. __func__, uvolt, voltdm->pmic->vddmax);
  648. /* Lets try maximum value anyway */
  649. uvolt = voltdm->pmic->vddmax;
  650. }
  651. return voltdm->pmic->uv_to_vsel(uvolt);
  652. }
  653. #ifdef CONFIG_PM
  654. /**
  655. * omap_pm_setup_sr_i2c_pcb_length - set length of SR I2C traces on PCB
  656. * @mm: length of the PCB trace in millimetres
  657. *
  658. * Sets the PCB trace length for the I2C channel. By default uses 63mm.
  659. * This is needed for properly calculating the capacitance value for
  660. * the PCB trace, and for setting the SR I2C channel timing parameters.
  661. */
  662. void __init omap_pm_setup_sr_i2c_pcb_length(u32 mm)
  663. {
  664. sr_i2c_pcb_length = mm;
  665. }
  666. #endif
  667. void __init omap_vc_init_channel(struct voltagedomain *voltdm)
  668. {
  669. struct omap_vc_channel *vc = voltdm->vc;
  670. u8 on_vsel, onlp_vsel, ret_vsel, off_vsel;
  671. u32 val;
  672. if (!voltdm->pmic || !voltdm->pmic->uv_to_vsel) {
  673. pr_err("%s: No PMIC info for vdd_%s\n", __func__, voltdm->name);
  674. return;
  675. }
  676. if (!voltdm->read || !voltdm->write) {
  677. pr_err("%s: No read/write API for accessing vdd_%s regs\n",
  678. __func__, voltdm->name);
  679. return;
  680. }
  681. vc->cfg_channel = 0;
  682. if (vc->flags & OMAP_VC_CHANNEL_CFG_MUTANT)
  683. vc_cfg_bits = &vc_mutant_channel_cfg;
  684. else
  685. vc_cfg_bits = &vc_default_channel_cfg;
  686. /* get PMIC/board specific settings */
  687. vc->i2c_slave_addr = voltdm->pmic->i2c_slave_addr;
  688. vc->volt_reg_addr = voltdm->pmic->volt_reg_addr;
  689. vc->cmd_reg_addr = voltdm->pmic->cmd_reg_addr;
  690. /* Configure the i2c slave address for this VC */
  691. voltdm->rmw(vc->smps_sa_mask,
  692. vc->i2c_slave_addr << __ffs(vc->smps_sa_mask),
  693. vc->smps_sa_reg);
  694. vc->cfg_channel |= vc_cfg_bits->sa;
  695. /*
  696. * Configure the PMIC register addresses.
  697. */
  698. voltdm->rmw(vc->smps_volra_mask,
  699. vc->volt_reg_addr << __ffs(vc->smps_volra_mask),
  700. vc->smps_volra_reg);
  701. vc->cfg_channel |= vc_cfg_bits->rav;
  702. if (vc->cmd_reg_addr) {
  703. voltdm->rmw(vc->smps_cmdra_mask,
  704. vc->cmd_reg_addr << __ffs(vc->smps_cmdra_mask),
  705. vc->smps_cmdra_reg);
  706. vc->cfg_channel |= vc_cfg_bits->rac;
  707. }
  708. if (vc->cmd_reg_addr == vc->volt_reg_addr)
  709. vc->cfg_channel |= vc_cfg_bits->racen;
  710. /* Set up the on, inactive, retention and off voltage */
  711. on_vsel = omap_vc_calc_vsel(voltdm, voltdm->vc_param->on);
  712. onlp_vsel = omap_vc_calc_vsel(voltdm, voltdm->vc_param->onlp);
  713. ret_vsel = omap_vc_calc_vsel(voltdm, voltdm->vc_param->ret);
  714. off_vsel = omap_vc_calc_vsel(voltdm, voltdm->vc_param->off);
  715. val = ((on_vsel << vc->common->cmd_on_shift) |
  716. (onlp_vsel << vc->common->cmd_onlp_shift) |
  717. (ret_vsel << vc->common->cmd_ret_shift) |
  718. (off_vsel << vc->common->cmd_off_shift));
  719. voltdm->write(val, vc->cmdval_reg);
  720. vc->cfg_channel |= vc_cfg_bits->cmd;
  721. /* Channel configuration */
  722. omap_vc_config_channel(voltdm);
  723. omap_vc_i2c_init(voltdm);
  724. if (cpu_is_omap34xx())
  725. omap3_vc_init_channel(voltdm);
  726. else if (cpu_is_omap44xx())
  727. omap4_vc_init_channel(voltdm);
  728. }