pll.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * Copyright (C) 2014 Texas Instruments Incorporated
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published by
  6. * the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #define DSS_SUBSYS_NAME "PLL"
  17. #include <linux/clk.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <linux/regulator/consumer.h>
  21. #include <linux/sched.h>
  22. #include <video/omapdss.h>
  23. #include "dss.h"
  24. #define PLL_CONTROL 0x0000
  25. #define PLL_STATUS 0x0004
  26. #define PLL_GO 0x0008
  27. #define PLL_CONFIGURATION1 0x000C
  28. #define PLL_CONFIGURATION2 0x0010
  29. #define PLL_CONFIGURATION3 0x0014
  30. #define PLL_SSC_CONFIGURATION1 0x0018
  31. #define PLL_SSC_CONFIGURATION2 0x001C
  32. #define PLL_CONFIGURATION4 0x0020
  33. static struct dss_pll *dss_plls[4];
  34. int dss_pll_register(struct dss_pll *pll)
  35. {
  36. int i;
  37. for (i = 0; i < ARRAY_SIZE(dss_plls); ++i) {
  38. if (!dss_plls[i]) {
  39. dss_plls[i] = pll;
  40. return 0;
  41. }
  42. }
  43. return -EBUSY;
  44. }
  45. void dss_pll_unregister(struct dss_pll *pll)
  46. {
  47. int i;
  48. for (i = 0; i < ARRAY_SIZE(dss_plls); ++i) {
  49. if (dss_plls[i] == pll) {
  50. dss_plls[i] = NULL;
  51. return;
  52. }
  53. }
  54. }
  55. struct dss_pll *dss_pll_find(const char *name)
  56. {
  57. int i;
  58. for (i = 0; i < ARRAY_SIZE(dss_plls); ++i) {
  59. if (dss_plls[i] && strcmp(dss_plls[i]->name, name) == 0)
  60. return dss_plls[i];
  61. }
  62. return NULL;
  63. }
  64. int dss_pll_enable(struct dss_pll *pll)
  65. {
  66. int r;
  67. r = clk_prepare_enable(pll->clkin);
  68. if (r)
  69. return r;
  70. if (pll->regulator) {
  71. r = regulator_enable(pll->regulator);
  72. if (r)
  73. goto err_reg;
  74. }
  75. r = pll->ops->enable(pll);
  76. if (r)
  77. goto err_enable;
  78. return 0;
  79. err_enable:
  80. if (pll->regulator)
  81. regulator_disable(pll->regulator);
  82. err_reg:
  83. clk_disable_unprepare(pll->clkin);
  84. return r;
  85. }
  86. void dss_pll_disable(struct dss_pll *pll)
  87. {
  88. pll->ops->disable(pll);
  89. if (pll->regulator)
  90. regulator_disable(pll->regulator);
  91. clk_disable_unprepare(pll->clkin);
  92. memset(&pll->cinfo, 0, sizeof(pll->cinfo));
  93. }
  94. int dss_pll_set_config(struct dss_pll *pll, const struct dss_pll_clock_info *cinfo)
  95. {
  96. int r;
  97. r = pll->ops->set_config(pll, cinfo);
  98. if (r)
  99. return r;
  100. pll->cinfo = *cinfo;
  101. return 0;
  102. }
  103. bool dss_pll_hsdiv_calc(const struct dss_pll *pll, unsigned long clkdco,
  104. unsigned long out_min, unsigned long out_max,
  105. dss_hsdiv_calc_func func, void *data)
  106. {
  107. const struct dss_pll_hw *hw = pll->hw;
  108. int m, m_start, m_stop;
  109. unsigned long out;
  110. out_min = out_min ? out_min : 1;
  111. out_max = out_max ? out_max : ULONG_MAX;
  112. m_start = max(DIV_ROUND_UP(clkdco, out_max), 1ul);
  113. m_stop = min((unsigned)(clkdco / out_min), hw->mX_max);
  114. for (m = m_start; m <= m_stop; ++m) {
  115. out = clkdco / m;
  116. if (func(m, out, data))
  117. return true;
  118. }
  119. return false;
  120. }
  121. bool dss_pll_calc(const struct dss_pll *pll, unsigned long clkin,
  122. unsigned long pll_min, unsigned long pll_max,
  123. dss_pll_calc_func func, void *data)
  124. {
  125. const struct dss_pll_hw *hw = pll->hw;
  126. int n, n_start, n_stop;
  127. int m, m_start, m_stop;
  128. unsigned long fint, clkdco;
  129. unsigned long pll_hw_max;
  130. unsigned long fint_hw_min, fint_hw_max;
  131. pll_hw_max = hw->clkdco_max;
  132. fint_hw_min = hw->fint_min;
  133. fint_hw_max = hw->fint_max;
  134. n_start = max(DIV_ROUND_UP(clkin, fint_hw_max), 1ul);
  135. n_stop = min((unsigned)(clkin / fint_hw_min), hw->n_max);
  136. pll_max = pll_max ? pll_max : ULONG_MAX;
  137. for (n = n_start; n <= n_stop; ++n) {
  138. fint = clkin / n;
  139. m_start = max(DIV_ROUND_UP(DIV_ROUND_UP(pll_min, fint), 2),
  140. 1ul);
  141. m_stop = min3((unsigned)(pll_max / fint / 2),
  142. (unsigned)(pll_hw_max / fint / 2),
  143. hw->m_max);
  144. for (m = m_start; m <= m_stop; ++m) {
  145. clkdco = 2 * m * fint;
  146. if (func(n, m, fint, clkdco, data))
  147. return true;
  148. }
  149. }
  150. return false;
  151. }
  152. static int wait_for_bit_change(void __iomem *reg, int bitnum, int value)
  153. {
  154. unsigned long timeout;
  155. ktime_t wait;
  156. int t;
  157. /* first busyloop to see if the bit changes right away */
  158. t = 100;
  159. while (t-- > 0) {
  160. if (FLD_GET(readl_relaxed(reg), bitnum, bitnum) == value)
  161. return value;
  162. }
  163. /* then loop for 500ms, sleeping for 1ms in between */
  164. timeout = jiffies + msecs_to_jiffies(500);
  165. while (time_before(jiffies, timeout)) {
  166. if (FLD_GET(readl_relaxed(reg), bitnum, bitnum) == value)
  167. return value;
  168. wait = ns_to_ktime(1000 * 1000);
  169. set_current_state(TASK_UNINTERRUPTIBLE);
  170. schedule_hrtimeout(&wait, HRTIMER_MODE_REL);
  171. }
  172. return !value;
  173. }
  174. int dss_pll_wait_reset_done(struct dss_pll *pll)
  175. {
  176. void __iomem *base = pll->base;
  177. if (wait_for_bit_change(base + PLL_STATUS, 0, 1) != 1)
  178. return -ETIMEDOUT;
  179. else
  180. return 0;
  181. }
  182. static int dss_wait_hsdiv_ack(struct dss_pll *pll, u32 hsdiv_ack_mask)
  183. {
  184. int t = 100;
  185. while (t-- > 0) {
  186. u32 v = readl_relaxed(pll->base + PLL_STATUS);
  187. v &= hsdiv_ack_mask;
  188. if (v == hsdiv_ack_mask)
  189. return 0;
  190. }
  191. return -ETIMEDOUT;
  192. }
  193. int dss_pll_write_config_type_a(struct dss_pll *pll,
  194. const struct dss_pll_clock_info *cinfo)
  195. {
  196. const struct dss_pll_hw *hw = pll->hw;
  197. void __iomem *base = pll->base;
  198. int r = 0;
  199. u32 l;
  200. l = 0;
  201. if (hw->has_stopmode)
  202. l = FLD_MOD(l, 1, 0, 0); /* PLL_STOPMODE */
  203. l = FLD_MOD(l, cinfo->n - 1, hw->n_msb, hw->n_lsb); /* PLL_REGN */
  204. l = FLD_MOD(l, cinfo->m, hw->m_msb, hw->m_lsb); /* PLL_REGM */
  205. /* M4 */
  206. l = FLD_MOD(l, cinfo->mX[0] ? cinfo->mX[0] - 1 : 0,
  207. hw->mX_msb[0], hw->mX_lsb[0]);
  208. /* M5 */
  209. l = FLD_MOD(l, cinfo->mX[1] ? cinfo->mX[1] - 1 : 0,
  210. hw->mX_msb[1], hw->mX_lsb[1]);
  211. writel_relaxed(l, base + PLL_CONFIGURATION1);
  212. l = 0;
  213. /* M6 */
  214. l = FLD_MOD(l, cinfo->mX[2] ? cinfo->mX[2] - 1 : 0,
  215. hw->mX_msb[2], hw->mX_lsb[2]);
  216. /* M7 */
  217. l = FLD_MOD(l, cinfo->mX[3] ? cinfo->mX[3] - 1 : 0,
  218. hw->mX_msb[3], hw->mX_lsb[3]);
  219. writel_relaxed(l, base + PLL_CONFIGURATION3);
  220. l = readl_relaxed(base + PLL_CONFIGURATION2);
  221. if (hw->has_freqsel) {
  222. u32 f = cinfo->fint < 1000000 ? 0x3 :
  223. cinfo->fint < 1250000 ? 0x4 :
  224. cinfo->fint < 1500000 ? 0x5 :
  225. cinfo->fint < 1750000 ? 0x6 :
  226. 0x7;
  227. l = FLD_MOD(l, f, 4, 1); /* PLL_FREQSEL */
  228. } else if (hw->has_selfreqdco) {
  229. u32 f = cinfo->clkdco < hw->clkdco_low ? 0x2 : 0x4;
  230. l = FLD_MOD(l, f, 3, 1); /* PLL_SELFREQDCO */
  231. }
  232. l = FLD_MOD(l, 1, 13, 13); /* PLL_REFEN */
  233. l = FLD_MOD(l, 0, 14, 14); /* PHY_CLKINEN */
  234. l = FLD_MOD(l, 0, 16, 16); /* M4_CLOCK_EN */
  235. l = FLD_MOD(l, 0, 18, 18); /* M5_CLOCK_EN */
  236. l = FLD_MOD(l, 1, 20, 20); /* HSDIVBYPASS */
  237. if (hw->has_refsel)
  238. l = FLD_MOD(l, 3, 22, 21); /* REFSEL = sysclk */
  239. l = FLD_MOD(l, 0, 23, 23); /* M6_CLOCK_EN */
  240. l = FLD_MOD(l, 0, 25, 25); /* M7_CLOCK_EN */
  241. writel_relaxed(l, base + PLL_CONFIGURATION2);
  242. writel_relaxed(1, base + PLL_GO); /* PLL_GO */
  243. if (wait_for_bit_change(base + PLL_GO, 0, 0) != 0) {
  244. DSSERR("DSS DPLL GO bit not going down.\n");
  245. r = -EIO;
  246. goto err;
  247. }
  248. if (wait_for_bit_change(base + PLL_STATUS, 1, 1) != 1) {
  249. DSSERR("cannot lock DSS DPLL\n");
  250. r = -EIO;
  251. goto err;
  252. }
  253. l = readl_relaxed(base + PLL_CONFIGURATION2);
  254. l = FLD_MOD(l, 1, 14, 14); /* PHY_CLKINEN */
  255. l = FLD_MOD(l, cinfo->mX[0] ? 1 : 0, 16, 16); /* M4_CLOCK_EN */
  256. l = FLD_MOD(l, cinfo->mX[1] ? 1 : 0, 18, 18); /* M5_CLOCK_EN */
  257. l = FLD_MOD(l, 0, 20, 20); /* HSDIVBYPASS */
  258. l = FLD_MOD(l, cinfo->mX[2] ? 1 : 0, 23, 23); /* M6_CLOCK_EN */
  259. l = FLD_MOD(l, cinfo->mX[3] ? 1 : 0, 25, 25); /* M7_CLOCK_EN */
  260. writel_relaxed(l, base + PLL_CONFIGURATION2);
  261. r = dss_wait_hsdiv_ack(pll,
  262. (cinfo->mX[0] ? BIT(7) : 0) |
  263. (cinfo->mX[1] ? BIT(8) : 0) |
  264. (cinfo->mX[2] ? BIT(10) : 0) |
  265. (cinfo->mX[3] ? BIT(11) : 0));
  266. if (r) {
  267. DSSERR("failed to enable HSDIV clocks\n");
  268. goto err;
  269. }
  270. err:
  271. return r;
  272. }
  273. int dss_pll_write_config_type_b(struct dss_pll *pll,
  274. const struct dss_pll_clock_info *cinfo)
  275. {
  276. const struct dss_pll_hw *hw = pll->hw;
  277. void __iomem *base = pll->base;
  278. u32 l;
  279. l = 0;
  280. l = FLD_MOD(l, cinfo->m, 20, 9); /* PLL_REGM */
  281. l = FLD_MOD(l, cinfo->n - 1, 8, 1); /* PLL_REGN */
  282. writel_relaxed(l, base + PLL_CONFIGURATION1);
  283. l = readl_relaxed(base + PLL_CONFIGURATION2);
  284. l = FLD_MOD(l, 0x0, 12, 12); /* PLL_HIGHFREQ divide by 2 */
  285. l = FLD_MOD(l, 0x1, 13, 13); /* PLL_REFEN */
  286. l = FLD_MOD(l, 0x0, 14, 14); /* PHY_CLKINEN */
  287. if (hw->has_refsel)
  288. l = FLD_MOD(l, 0x3, 22, 21); /* REFSEL = SYSCLK */
  289. /* PLL_SELFREQDCO */
  290. if (cinfo->clkdco > hw->clkdco_low)
  291. l = FLD_MOD(l, 0x4, 3, 1);
  292. else
  293. l = FLD_MOD(l, 0x2, 3, 1);
  294. writel_relaxed(l, base + PLL_CONFIGURATION2);
  295. l = readl_relaxed(base + PLL_CONFIGURATION3);
  296. l = FLD_MOD(l, cinfo->sd, 17, 10); /* PLL_REGSD */
  297. writel_relaxed(l, base + PLL_CONFIGURATION3);
  298. l = readl_relaxed(base + PLL_CONFIGURATION4);
  299. l = FLD_MOD(l, cinfo->mX[0], 24, 18); /* PLL_REGM2 */
  300. l = FLD_MOD(l, cinfo->mf, 17, 0); /* PLL_REGM_F */
  301. writel_relaxed(l, base + PLL_CONFIGURATION4);
  302. writel_relaxed(1, base + PLL_GO); /* PLL_GO */
  303. if (wait_for_bit_change(base + PLL_GO, 0, 0) != 0) {
  304. DSSERR("DSS DPLL GO bit not going down.\n");
  305. return -EIO;
  306. }
  307. if (wait_for_bit_change(base + PLL_STATUS, 1, 1) != 1) {
  308. DSSERR("cannot lock DSS DPLL\n");
  309. return -ETIMEDOUT;
  310. }
  311. return 0;
  312. }