acpuclock-8625q.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /*
  2. * Copyright (C) 2007 Google, Inc.
  3. * Copyright (c) 2007-2012, Linux Foundation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 and
  7. * only version 2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/errno.h>
  18. #include <linux/string.h>
  19. #include <linux/delay.h>
  20. #include <linux/clk.h>
  21. #include <linux/cpufreq.h>
  22. #include <linux/mutex.h>
  23. #include <linux/io.h>
  24. #include <linux/sort.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/smp.h>
  27. #include <mach/board.h>
  28. #include <mach/msm_iomap.h>
  29. #include <mach/clk-provider.h>
  30. #include <asm/cpu.h>
  31. #include "acpuclock.h"
  32. #include "acpuclock-8625q.h"
  33. #define A11S_CLK_CNTL_ADDR (MSM_CSR_BASE + 0x100)
  34. #define A11S_CLK_SEL_ADDR (MSM_CSR_BASE + 0x104)
  35. #define PLL4_L_VAL_ADDR (MSM_CLK_CTL_BASE + 0x378)
  36. #define PLL4_M_VAL_ADDR (MSM_CLK_CTL_BASE + 0x37C)
  37. #define PLL4_N_VAL_ADDR (MSM_CLK_CTL_BASE + 0x380)
  38. #define POWER_COLLAPSE_KHZ 19200
  39. /* Max CPU frequency allowed by hardware while in standby waiting for an irq. */
  40. #define MAX_WAIT_FOR_IRQ_KHZ 128000
  41. /**
  42. * enum - For acpuclock PLL IDs
  43. */
  44. enum {
  45. ACPU_PLL_0 = 0,
  46. ACPU_PLL_1,
  47. ACPU_PLL_2,
  48. ACPU_PLL_3,
  49. ACPU_PLL_4,
  50. ACPU_PLL_TCXO,
  51. ACPU_PLL_END,
  52. };
  53. struct acpu_clk_src {
  54. struct clk *clk;
  55. const char *name;
  56. };
  57. struct pll_config {
  58. unsigned int l;
  59. unsigned int m;
  60. unsigned int n;
  61. };
  62. static struct acpu_clk_src pll_clk[ACPU_PLL_END] = {
  63. [ACPU_PLL_0] = { .name = "pll0_clk" },
  64. [ACPU_PLL_1] = { .name = "pll1_clk" },
  65. [ACPU_PLL_2] = { .name = "pll2_clk" },
  66. [ACPU_PLL_4] = { .name = "pll4_clk" },
  67. };
  68. static struct pll_config pll4_cfg_tbl[] = {
  69. [0] = { 36, 1, 2 }, /* 700.8 MHz */
  70. [1] = { 52, 1, 2 }, /* 1008 MHz */
  71. [2] = { 63, 0, 1 }, /* 1209.6 MHz */
  72. [3] = { 73, 0, 1 }, /* 1401.6 MHz */
  73. };
  74. struct clock_state {
  75. struct clkctl_acpu_speed *current_speed;
  76. struct mutex lock;
  77. uint32_t max_speed_delta_khz;
  78. struct clk *ebi1_clk;
  79. struct regulator *vreg_cpu;
  80. };
  81. struct clkctl_acpu_speed {
  82. unsigned int use_for_scaling;
  83. unsigned int a11clk_khz;
  84. int pll;
  85. unsigned int a11clk_src_sel;
  86. unsigned int a11clk_src_div;
  87. unsigned int ahbclk_khz;
  88. unsigned int ahbclk_div;
  89. int vdd;
  90. unsigned int axiclk_khz;
  91. struct pll_config *pll_rate;
  92. unsigned long lpj;
  93. };
  94. static struct clock_state drv_state = { 0 };
  95. /* PVS MAX Voltage in uV as per frequencies*/
  96. # define MAX_14GHZ_VOLTAGE 1350000
  97. # define MAX_12GHZ_VOLTAGE 1275000
  98. # define MAX_1GHZ_VOLTAGE 1175000
  99. # define MAX_NOMINAL_VOLTAGE 1150000
  100. /* PVS deltas as per formula*/
  101. # define DELTA_LEVEL_1_UV 0
  102. # define DELTA_LEVEL_2_UV 75000
  103. # define DELTA_LEVEL_3_UV 150000
  104. /*
  105. * The default initialization is according to the requirements of
  106. * SKUD_prime. If the target is quad core, we reinitialize this table using
  107. * the reinitalize_freq_table() function.
  108. */
  109. static struct clkctl_acpu_speed acpu_freq_tbl_cmn[] = {
  110. { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 30720 },
  111. { 1, 245760, ACPU_PLL_1, 1, 0, 30720, 3, MAX_NOMINAL_VOLTAGE, 61440 },
  112. { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, MAX_NOMINAL_VOLTAGE, 122880 },
  113. { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, MAX_NOMINAL_VOLTAGE, 122880 },
  114. { 0, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 0, 160000 },
  115. { 1, 700800, ACPU_PLL_4, 6, 0, 87500, 3, MAX_NOMINAL_VOLTAGE, 160000,
  116. &pll4_cfg_tbl[0]},
  117. { 1, 1008000, ACPU_PLL_4, 6, 0, 126000, 3, MAX_1GHZ_VOLTAGE, 200000,
  118. &pll4_cfg_tbl[1]},
  119. };
  120. static struct clkctl_acpu_speed acpu_freq_tbl_1209[] = {
  121. { 1, 1209600, ACPU_PLL_4, 6, 0, 151200, 3, MAX_12GHZ_VOLTAGE, 200000,
  122. &pll4_cfg_tbl[2]},
  123. };
  124. static struct clkctl_acpu_speed acpu_freq_tbl_1401[] = {
  125. { 1, 1401600, ACPU_PLL_4, 6, 0, 175000, 3, MAX_14GHZ_VOLTAGE, 200000,
  126. &pll4_cfg_tbl[3]},
  127. };
  128. /* Entry corresponding to CDMA build*/
  129. static struct clkctl_acpu_speed acpu_freq_tbl_196608[] = {
  130. { 1, 196608, ACPU_PLL_1, 1, 0, 65536, 2, MAX_NOMINAL_VOLTAGE, 98304 },
  131. };
  132. static struct clkctl_acpu_speed acpu_freq_tbl_null[] = {
  133. { 0 },
  134. };
  135. static struct clkctl_acpu_speed acpu_freq_tbl[ARRAY_SIZE(acpu_freq_tbl_cmn)
  136. + ARRAY_SIZE(acpu_freq_tbl_1209)
  137. + ARRAY_SIZE(acpu_freq_tbl_1401)
  138. + ARRAY_SIZE(acpu_freq_tbl_null)];
  139. /* Switch to this when reprogramming PLL4 */
  140. static struct clkctl_acpu_speed *backup_s;
  141. #ifdef CONFIG_CPU_FREQ_MSM
  142. static struct cpufreq_frequency_table freq_table[NR_CPUS][20];
  143. static void __devinit cpufreq_table_init(void)
  144. {
  145. int cpu;
  146. for_each_possible_cpu(cpu) {
  147. unsigned int i, freq_cnt = 0;
  148. /* Construct the freq_table table from acpu_freq_tbl since
  149. * the freq_table values need to match frequencies specified
  150. * in acpu_freq_tbl and acpu_freq_tbl needs to be fixed up
  151. * during init.
  152. */
  153. for (i = 0; acpu_freq_tbl[i].a11clk_khz != 0
  154. && freq_cnt < ARRAY_SIZE(*freq_table)-1; i++) {
  155. if (acpu_freq_tbl[i].use_for_scaling) {
  156. freq_table[cpu][freq_cnt].index = freq_cnt;
  157. freq_table[cpu][freq_cnt].frequency
  158. = acpu_freq_tbl[i].a11clk_khz;
  159. freq_cnt++;
  160. }
  161. }
  162. /* freq_table not big enough to store all usable freqs. */
  163. BUG_ON(acpu_freq_tbl[i].a11clk_khz != 0);
  164. freq_table[cpu][freq_cnt].index = freq_cnt;
  165. freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
  166. /* Register table with CPUFreq. */
  167. cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
  168. pr_info("CPU%d: %d scaling frequencies supported.\n",
  169. cpu, freq_cnt);
  170. }
  171. }
  172. #else
  173. static void __devinit cpufreq_table_init(void) { }
  174. #endif
  175. static void update_jiffies(int cpu, unsigned long loops)
  176. {
  177. #ifdef CONFIG_SMP
  178. for_each_possible_cpu(cpu) {
  179. per_cpu(cpu_data, cpu).loops_per_jiffy =
  180. loops;
  181. }
  182. #endif
  183. /* Adjust the global one */
  184. loops_per_jiffy = loops;
  185. }
  186. /* Assumes PLL4 is off and the acpuclock isn't sourced from PLL4 */
  187. static void acpuclk_config_pll4(struct pll_config *pll)
  188. {
  189. /*
  190. * Make sure write to disable PLL_4 has completed
  191. * before reconfiguring that PLL.
  192. */
  193. mb();
  194. writel_relaxed(pll->l, PLL4_L_VAL_ADDR);
  195. writel_relaxed(pll->m, PLL4_M_VAL_ADDR);
  196. writel_relaxed(pll->n, PLL4_N_VAL_ADDR);
  197. /* Make sure PLL is programmed before returning. */
  198. mb();
  199. }
  200. /* Set proper dividers for the given clock speed. */
  201. static void acpuclk_set_div(const struct clkctl_acpu_speed *hunt_s)
  202. {
  203. uint32_t reg_clkctl, reg_clksel, clk_div, src_sel;
  204. reg_clksel = readl_relaxed(A11S_CLK_SEL_ADDR);
  205. /* AHB_CLK_DIV */
  206. clk_div = (reg_clksel >> 1) & 0x03;
  207. /* CLK_SEL_SRC1NO */
  208. src_sel = reg_clksel & 1;
  209. /*
  210. * If the new clock divider is higher than the previous, then
  211. * program the divider before switching the clock
  212. */
  213. if (hunt_s->ahbclk_div > clk_div) {
  214. reg_clksel &= ~(0x3 << 1);
  215. reg_clksel |= (hunt_s->ahbclk_div << 1);
  216. writel_relaxed(reg_clksel, A11S_CLK_SEL_ADDR);
  217. }
  218. /* Program clock source and divider */
  219. reg_clkctl = readl_relaxed(A11S_CLK_CNTL_ADDR);
  220. reg_clkctl &= ~(0xFF << (8 * src_sel));
  221. reg_clkctl |= hunt_s->a11clk_src_sel << (4 + 8 * src_sel);
  222. reg_clkctl |= hunt_s->a11clk_src_div << (0 + 8 * src_sel);
  223. writel_relaxed(reg_clkctl, A11S_CLK_CNTL_ADDR);
  224. /* Program clock source selection */
  225. reg_clksel ^= 1;
  226. writel_relaxed(reg_clksel, A11S_CLK_SEL_ADDR);
  227. /* Wait for the clock switch to complete */
  228. mb();
  229. udelay(50);
  230. /*
  231. * If the new clock divider is lower than the previous, then
  232. * program the divider after switching the clock
  233. */
  234. if (hunt_s->ahbclk_div < clk_div) {
  235. reg_clksel &= ~(0x3 << 1);
  236. reg_clksel |= (hunt_s->ahbclk_div << 1);
  237. writel_relaxed(reg_clksel, A11S_CLK_SEL_ADDR);
  238. }
  239. }
  240. static int acpuclk_set_vdd_level(int vdd)
  241. {
  242. int rc;
  243. rc = regulator_set_voltage(drv_state.vreg_cpu, vdd, vdd);
  244. if (rc) {
  245. pr_err("failed to set vdd=%d uV\n", vdd);
  246. return rc;
  247. }
  248. return 0;
  249. }
  250. static int acpuclk_8625q_set_rate(int cpu, unsigned long rate,
  251. enum setrate_reason reason)
  252. {
  253. uint32_t reg_clkctl;
  254. struct clkctl_acpu_speed *cur_s, *tgt_s, *strt_s;
  255. int res, rc = 0;
  256. unsigned int plls_enabled = 0, pll;
  257. int delta;
  258. if (reason == SETRATE_CPUFREQ)
  259. mutex_lock(&drv_state.lock);
  260. strt_s = cur_s = drv_state.current_speed;
  261. WARN_ONCE(cur_s == NULL, "%s: not initialized\n", __func__);
  262. if (cur_s == NULL) {
  263. rc = -ENOENT;
  264. goto out;
  265. }
  266. cur_s->vdd = regulator_get_voltage(drv_state.vreg_cpu);
  267. if (cur_s->vdd <= 0)
  268. goto out;
  269. pr_debug("current freq=%dKhz vdd=%duV\n",
  270. cur_s->a11clk_khz, cur_s->vdd);
  271. if (rate == cur_s->a11clk_khz)
  272. goto out;
  273. for (tgt_s = acpu_freq_tbl; tgt_s->a11clk_khz != 0; tgt_s++) {
  274. if (tgt_s->a11clk_khz == rate)
  275. break;
  276. }
  277. if (tgt_s->a11clk_khz == 0) {
  278. rc = -EINVAL;
  279. goto out;
  280. }
  281. /* Choose the highest speed at or below 'rate' with same PLL. */
  282. if (reason != SETRATE_CPUFREQ
  283. && tgt_s->a11clk_khz < cur_s->a11clk_khz) {
  284. while (tgt_s->pll != ACPU_PLL_TCXO &&
  285. tgt_s->pll != cur_s->pll) {
  286. pr_debug("Intermediate frequency changes: %u\n",
  287. tgt_s->a11clk_khz);
  288. tgt_s--;
  289. }
  290. }
  291. if (strt_s->pll != ACPU_PLL_TCXO)
  292. plls_enabled |= 1 << strt_s->pll;
  293. /* Need to do this when coming out of power collapse since some modem
  294. * firmwares reset the VDD when the application processor enters power
  295. * collapse.
  296. */
  297. if (reason == SETRATE_CPUFREQ || reason == SETRATE_PC) {
  298. /* Increase VDD if needed. */
  299. if (tgt_s->vdd > cur_s->vdd) {
  300. rc = acpuclk_set_vdd_level(tgt_s->vdd);
  301. if (rc < 0) {
  302. pr_err("Unable to switch ACPU vdd (%d)\n", rc);
  303. goto out;
  304. }
  305. pr_debug("Increased Vdd to %duV\n", tgt_s->vdd);
  306. }
  307. }
  308. /* Set wait states for CPU inbetween frequency changes */
  309. reg_clkctl = readl_relaxed(A11S_CLK_CNTL_ADDR);
  310. reg_clkctl |= (100 << 16); /* set WT_ST_CNT */
  311. writel_relaxed(reg_clkctl, A11S_CLK_CNTL_ADDR);
  312. pr_debug("Switching from ACPU rate %u KHz -> %u KHz\n",
  313. strt_s->a11clk_khz, tgt_s->a11clk_khz);
  314. delta = abs((int)(strt_s->a11clk_khz - tgt_s->a11clk_khz));
  315. if (tgt_s->pll == ACPU_PLL_4) {
  316. if (strt_s->pll == ACPU_PLL_4 ||
  317. delta > drv_state.max_speed_delta_khz) {
  318. /*
  319. * Enable the backup PLL if required
  320. * and switch to it.
  321. */
  322. clk_enable(pll_clk[backup_s->pll].clk);
  323. acpuclk_set_div(backup_s);
  324. update_jiffies(cpu, backup_s->lpj);
  325. }
  326. /* Make sure PLL4 is off before reprogramming */
  327. if ((plls_enabled & (1 << tgt_s->pll))) {
  328. clk_disable(pll_clk[tgt_s->pll].clk);
  329. plls_enabled &= ~(1 << tgt_s->pll);
  330. }
  331. acpuclk_config_pll4(tgt_s->pll_rate);
  332. pll_clk[tgt_s->pll].clk->rate = tgt_s->a11clk_khz*1000;
  333. } else if (strt_s->pll == ACPU_PLL_4) {
  334. if (delta > drv_state.max_speed_delta_khz) {
  335. /*
  336. * Enable the bcackup PLL if required
  337. * and switch to it.
  338. */
  339. clk_enable(pll_clk[backup_s->pll].clk);
  340. acpuclk_set_div(backup_s);
  341. update_jiffies(cpu, backup_s->lpj);
  342. }
  343. }
  344. if ((tgt_s->pll != ACPU_PLL_TCXO) &&
  345. !(plls_enabled & (1 << tgt_s->pll))) {
  346. rc = clk_enable(pll_clk[tgt_s->pll].clk);
  347. if (rc < 0) {
  348. pr_err("PLL%d enable failed (%d)\n",
  349. tgt_s->pll, rc);
  350. goto out;
  351. }
  352. plls_enabled |= 1 << tgt_s->pll;
  353. }
  354. acpuclk_set_div(tgt_s);
  355. drv_state.current_speed = tgt_s;
  356. pr_debug("The new clock speed is %u\n", tgt_s->a11clk_khz);
  357. /* Re-adjust lpj for the new clock speed. */
  358. update_jiffies(cpu, tgt_s->lpj);
  359. /* Disable the backup PLL */
  360. if ((delta > drv_state.max_speed_delta_khz)
  361. || (strt_s->pll == ACPU_PLL_4 &&
  362. tgt_s->pll == ACPU_PLL_4))
  363. clk_disable(pll_clk[backup_s->pll].clk);
  364. /* Nothing else to do for SWFI. */
  365. if (reason == SETRATE_SWFI)
  366. goto out;
  367. /* Change the AXI bus frequency if we can. */
  368. if (reason != SETRATE_PC &&
  369. strt_s->axiclk_khz != tgt_s->axiclk_khz) {
  370. res = clk_set_rate(drv_state.ebi1_clk,
  371. tgt_s->axiclk_khz * 1000);
  372. pr_debug("AXI bus set freq %d\n",
  373. tgt_s->axiclk_khz * 1000);
  374. if (res < 0)
  375. pr_warning("Setting AXI min rate failed (%d)\n", res);
  376. }
  377. /* Disable PLLs we are not using anymore. */
  378. if (tgt_s->pll != ACPU_PLL_TCXO)
  379. plls_enabled &= ~(1 << tgt_s->pll);
  380. for (pll = ACPU_PLL_0; pll < ACPU_PLL_END; pll++)
  381. if (plls_enabled & (1 << pll))
  382. clk_disable(pll_clk[pll].clk);
  383. /* Nothing else to do for power collapse. */
  384. if (reason == SETRATE_PC)
  385. goto out;
  386. /* Drop VDD level if we can. */
  387. if (tgt_s->vdd < strt_s->vdd) {
  388. res = acpuclk_set_vdd_level(tgt_s->vdd);
  389. if (res < 0)
  390. pr_warning("Unable to drop ACPU vdd (%d)\n", res);
  391. pr_debug("Decreased Vdd to %duV\n", tgt_s->vdd);
  392. }
  393. pr_debug("ACPU speed change complete\n");
  394. out:
  395. if (reason == SETRATE_CPUFREQ)
  396. mutex_unlock(&drv_state.lock);
  397. return rc;
  398. }
  399. static int __devinit acpuclk_hw_init(void)
  400. {
  401. struct clkctl_acpu_speed *speed;
  402. uint32_t div, sel, reg_clksel;
  403. int res;
  404. /*
  405. * Prepare all the PLLs because we enable/disable them
  406. * from atomic context and can't always ensure they're
  407. * all prepared in non-atomic context. Same goes for
  408. * ebi1_acpu_clk.
  409. */
  410. BUG_ON(clk_prepare(pll_clk[ACPU_PLL_0].clk));
  411. BUG_ON(clk_prepare(pll_clk[ACPU_PLL_1].clk));
  412. BUG_ON(clk_prepare(pll_clk[ACPU_PLL_2].clk));
  413. BUG_ON(clk_prepare(pll_clk[ACPU_PLL_4].clk));
  414. BUG_ON(clk_prepare(drv_state.ebi1_clk));
  415. /*
  416. * Determine the rate of ACPU clock
  417. */
  418. if (!(readl_relaxed(A11S_CLK_SEL_ADDR) & 0x01)) { /* CLK_SEL_SRC1N0 */
  419. /* CLK_SRC0_SEL */
  420. sel = (readl_relaxed(A11S_CLK_CNTL_ADDR) >> 12) & 0x7;
  421. /* CLK_SRC0_DIV */
  422. div = (readl_relaxed(A11S_CLK_CNTL_ADDR) >> 8) & 0x0f;
  423. } else {
  424. /* CLK_SRC1_SEL */
  425. sel = (readl_relaxed(A11S_CLK_CNTL_ADDR) >> 4) & 0x07;
  426. /* CLK_SRC1_DIV */
  427. div = readl_relaxed(A11S_CLK_CNTL_ADDR) & 0x0f;
  428. }
  429. for (speed = acpu_freq_tbl; speed->a11clk_khz != 0; speed++) {
  430. if (speed->a11clk_src_sel == sel
  431. && (speed->a11clk_src_div == div))
  432. break;
  433. }
  434. if (speed->a11clk_khz == 0) {
  435. pr_err("Error - ACPU clock reports invalid speed\n");
  436. return -EINVAL;
  437. }
  438. drv_state.current_speed = speed;
  439. if (speed->pll != ACPU_PLL_TCXO) {
  440. if (clk_enable(pll_clk[speed->pll].clk)) {
  441. pr_warning("Failed to vote for boot PLL\n");
  442. return -ENODEV;
  443. }
  444. }
  445. reg_clksel = readl_relaxed(A11S_CLK_SEL_ADDR);
  446. reg_clksel &= ~(0x3 << 14);
  447. reg_clksel |= (0x1 << 14);
  448. writel_relaxed(reg_clksel, A11S_CLK_SEL_ADDR);
  449. res = clk_set_rate(drv_state.ebi1_clk, speed->axiclk_khz * 1000);
  450. if (res < 0) {
  451. pr_warning("Setting AXI min rate failed (%d)\n", res);
  452. return -ENODEV;
  453. }
  454. res = clk_enable(drv_state.ebi1_clk);
  455. if (res < 0) {
  456. pr_warning("Enabling AXI clock failed (%d)\n", res);
  457. return -ENODEV;
  458. }
  459. drv_state.vreg_cpu = regulator_get(NULL, "vddx_cx");
  460. if (IS_ERR(drv_state.vreg_cpu)) {
  461. res = PTR_ERR(drv_state.vreg_cpu);
  462. pr_err("could not get regulator: %d\n", res);
  463. }
  464. pr_info("ACPU running at %d KHz\n", speed->a11clk_khz);
  465. return 0;
  466. }
  467. static unsigned long acpuclk_8625q_get_rate(int cpu)
  468. {
  469. WARN_ONCE(drv_state.current_speed == NULL,
  470. "%s: not initialized\n", __func__);
  471. if (drv_state.current_speed)
  472. return drv_state.current_speed->a11clk_khz;
  473. else
  474. return 0;
  475. }
  476. static int reinitialize_freq_table(bool target_select)
  477. {
  478. /*
  479. * target_flag is set only if it is a Quad core chip,
  480. * In that case, we modify the initialization
  481. * of the table according to the specific requirement
  482. * for this target. Otherwise the default initialized table is
  483. * used for SKUD_prime.
  484. */
  485. if (target_select) {
  486. struct clkctl_acpu_speed *tbl;
  487. for (tbl = acpu_freq_tbl; tbl->a11clk_khz; tbl++) {
  488. if (tbl->a11clk_khz >= 1008000) {
  489. tbl->axiclk_khz = 300000;
  490. if (tbl->a11clk_khz == 1209600)
  491. tbl->vdd = 0;
  492. } else {
  493. if (tbl->a11clk_khz != 600000
  494. && tbl->a11clk_khz != 19200)
  495. tbl->vdd = 1050000;
  496. if (tbl->a11clk_khz == 700800)
  497. tbl->axiclk_khz = 245000;
  498. }
  499. }
  500. }
  501. return 0;
  502. }
  503. #define MHZ 1000000
  504. static void __devinit select_freq_plan(unsigned int pvs_voltage,
  505. bool target_sel)
  506. {
  507. unsigned long pll_mhz[ACPU_PLL_END];
  508. int i;
  509. int size;
  510. int delta[3] = {DELTA_LEVEL_1_UV, DELTA_LEVEL_2_UV, DELTA_LEVEL_3_UV};
  511. struct clkctl_acpu_speed *tbl;
  512. /* Get PLL clocks */
  513. for (i = 0; i < ACPU_PLL_END; i++) {
  514. if (pll_clk[i].name) {
  515. pll_clk[i].clk = clk_get_sys("acpu", pll_clk[i].name);
  516. if (IS_ERR(pll_clk[i].clk)) {
  517. pll_mhz[i] = 0;
  518. continue;
  519. }
  520. /* Get PLL's Rate */
  521. pll_mhz[i] = clk_get_rate(pll_clk[i].clk)/MHZ;
  522. }
  523. }
  524. memcpy(acpu_freq_tbl, acpu_freq_tbl_cmn, sizeof(acpu_freq_tbl_cmn));
  525. size = ARRAY_SIZE(acpu_freq_tbl_cmn);
  526. i = 0; /* needed if we have a 1Ghz part */
  527. /* select if it is a 1.2Ghz part */
  528. if (pll_mhz[ACPU_PLL_4] == 1209) {
  529. memcpy(acpu_freq_tbl + size, acpu_freq_tbl_1209,
  530. sizeof(acpu_freq_tbl_1209));
  531. size += sizeof(acpu_freq_tbl_1209);
  532. i = 1; /* set the delta index */
  533. }
  534. /* select if it is a 1.4Ghz part */
  535. if (pll_mhz[ACPU_PLL_4] == 1401) {
  536. memcpy(acpu_freq_tbl + size, acpu_freq_tbl_1209,
  537. sizeof(acpu_freq_tbl_1209));
  538. size += ARRAY_SIZE(acpu_freq_tbl_1209);
  539. memcpy(acpu_freq_tbl + size, acpu_freq_tbl_1401,
  540. sizeof(acpu_freq_tbl_1401));
  541. size += ARRAY_SIZE(acpu_freq_tbl_1401);
  542. i = 2; /* set the delta index */
  543. }
  544. memcpy(acpu_freq_tbl + size, acpu_freq_tbl_null,
  545. sizeof(acpu_freq_tbl_null));
  546. size += sizeof(acpu_freq_tbl_null);
  547. /* Alter the freq value in freq_tbl if it is a CDMA build*/
  548. if (pll_mhz[ACPU_PLL_1] == 196) {
  549. for (tbl = acpu_freq_tbl; tbl->a11clk_khz; tbl++) {
  550. if (tbl->a11clk_khz == 245760 &&
  551. tbl->pll == ACPU_PLL_1) {
  552. pr_debug("Upgrading pll1 freq to 196 Mhz\n");
  553. memcpy(tbl, acpu_freq_tbl_196608,
  554. sizeof(acpu_freq_tbl_196608));
  555. break;
  556. }
  557. }
  558. }
  559. reinitialize_freq_table(target_sel);
  560. /*
  561. *PVS Voltage calculation formula
  562. *1.4 Ghz device
  563. *1.4 Ghz: Max(PVS_voltage,1.35V)
  564. *1.2 Ghz: Max(PVS_volatge - 75mV,1.275V)
  565. *1.0 Ghz: Max(PVS_voltage - 150mV, 1.175V)
  566. *1.2 Ghz device
  567. *1.2 Ghz: Max(PVS_voltage,1.275V)
  568. *1.0 Ghz: Max(PVS_volatge - 75mV,1.175V)
  569. *Nominal Mode: 1.15V
  570. */
  571. for (tbl = acpu_freq_tbl; tbl->a11clk_khz; tbl++) {
  572. if (tbl->a11clk_khz >= 1008000) {
  573. /*
  574. * Change voltage as per PVS formula,
  575. * i is initialized above with 2 or 1
  576. * depending upon whether it is a 1.4Ghz
  577. * or 1.2Ghz, so, we get the proper value
  578. * from delta[i] which is to be deducted
  579. * from PVS voltage.
  580. */
  581. tbl->vdd = max((int)(pvs_voltage - delta[i]), tbl->vdd);
  582. i--;
  583. }
  584. }
  585. /* find the backup PLL entry from the table */
  586. for (tbl = acpu_freq_tbl; tbl->a11clk_khz; tbl++) {
  587. if (tbl->pll == ACPU_PLL_2 &&
  588. tbl->a11clk_src_div == 1) {
  589. backup_s = tbl;
  590. break;
  591. }
  592. }
  593. BUG_ON(!backup_s);
  594. }
  595. /*
  596. * Hardware requires the CPU to be dropped to less than MAX_WAIT_FOR_IRQ_KHZ
  597. * before entering a wait for irq low-power mode. Find a suitable rate.
  598. */
  599. static unsigned long __devinit find_wait_for_irq_khz(void)
  600. {
  601. unsigned long found_khz = 0;
  602. int i;
  603. for (i = 0; acpu_freq_tbl[i].a11clk_khz &&
  604. acpu_freq_tbl[i].a11clk_khz <= MAX_WAIT_FOR_IRQ_KHZ; i++)
  605. found_khz = acpu_freq_tbl[i].a11clk_khz;
  606. return found_khz;
  607. }
  608. static void __devinit lpj_init(void)
  609. {
  610. int i = 0, cpu;
  611. const struct clkctl_acpu_speed *base_clk = drv_state.current_speed;
  612. unsigned long loops;
  613. for_each_possible_cpu(cpu) {
  614. #ifdef CONFIG_SMP
  615. loops = per_cpu(cpu_data, cpu).loops_per_jiffy;
  616. #else
  617. loops = loops_per_jiffy;
  618. #endif
  619. for (i = 0; acpu_freq_tbl[i].a11clk_khz; i++) {
  620. acpu_freq_tbl[i].lpj = cpufreq_scale(
  621. loops,
  622. base_clk->a11clk_khz,
  623. acpu_freq_tbl[i].a11clk_khz);
  624. }
  625. }
  626. }
  627. static struct acpuclk_data acpuclk_8625q_data = {
  628. .set_rate = acpuclk_8625q_set_rate,
  629. .get_rate = acpuclk_8625q_get_rate,
  630. .power_collapse_khz = POWER_COLLAPSE_KHZ,
  631. .switch_time_us = 50,
  632. };
  633. static void __devinit print_acpu_freq_tbl(void)
  634. {
  635. struct clkctl_acpu_speed *t;
  636. int i;
  637. pr_info("Id CPU-KHz PLL DIV AHB-KHz ADIV AXI-KHz Vdd\n");
  638. t = &acpu_freq_tbl[0];
  639. for (i = 0; t->a11clk_khz != 0; i++) {
  640. pr_info("%2d %7d %3d %3d %7d %4d %7d %3d\n",
  641. i, t->a11clk_khz, t->pll, t->a11clk_src_div + 1,
  642. t->ahbclk_khz, t->ahbclk_div + 1, t->axiclk_khz,
  643. t->vdd);
  644. t++;
  645. }
  646. }
  647. static int __devinit acpuclk_8625q_probe(struct platform_device *pdev)
  648. {
  649. const struct acpuclk_pdata_8625q *pdata = pdev->dev.platform_data;
  650. unsigned int pvs_voltage = pdata->pvs_voltage_uv;
  651. bool target_sel = pdata->flag;
  652. drv_state.max_speed_delta_khz = pdata->acpu_clk_data->
  653. max_speed_delta_khz;
  654. drv_state.ebi1_clk = clk_get(NULL, "ebi1_acpu_clk");
  655. BUG_ON(IS_ERR(drv_state.ebi1_clk));
  656. mutex_init(&drv_state.lock);
  657. select_freq_plan(pvs_voltage, target_sel);
  658. acpuclk_8625q_data.wait_for_irq_khz = find_wait_for_irq_khz();
  659. if (acpuclk_hw_init() < 0)
  660. pr_err("acpuclk_hw_init not successful.\n");
  661. print_acpu_freq_tbl();
  662. lpj_init();
  663. acpuclk_register(&acpuclk_8625q_data);
  664. cpufreq_table_init();
  665. return 0;
  666. }
  667. static struct platform_driver acpuclk_8625q_driver = {
  668. .probe = acpuclk_8625q_probe,
  669. .driver = {
  670. .name = "acpuclock-8625q",
  671. .owner = THIS_MODULE,
  672. },
  673. };
  674. static int __init acpuclk_8625q_init(void)
  675. {
  676. return platform_driver_register(&acpuclk_8625q_driver);
  677. }
  678. postcore_initcall(acpuclk_8625q_init);