hdmi_pll_28lpm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "%s: " fmt, __func__
  6. #include <linux/kernel.h>
  7. #include <linux/err.h>
  8. #include <linux/delay.h>
  9. #include <linux/iopoll.h>
  10. #include <dt-bindings/clock/mdss-28nm-pll-clk.h>
  11. #include "pll_drv.h"
  12. #include "hdmi_pll.h"
  13. /* HDMI PLL macros */
  14. #define HDMI_PHY_PLL_REFCLK_CFG (0x0400)
  15. #define HDMI_PHY_PLL_CHRG_PUMP_CFG (0x0404)
  16. #define HDMI_PHY_PLL_LOOP_FLT_CFG0 (0x0408)
  17. #define HDMI_PHY_PLL_LOOP_FLT_CFG1 (0x040c)
  18. #define HDMI_PHY_PLL_IDAC_ADJ_CFG (0x0410)
  19. #define HDMI_PHY_PLL_I_VI_KVCO_CFG (0x0414)
  20. #define HDMI_PHY_PLL_PWRDN_B (0x0418)
  21. #define HDMI_PHY_PLL_SDM_CFG0 (0x041c)
  22. #define HDMI_PHY_PLL_SDM_CFG1 (0x0420)
  23. #define HDMI_PHY_PLL_SDM_CFG2 (0x0424)
  24. #define HDMI_PHY_PLL_SDM_CFG3 (0x0428)
  25. #define HDMI_PHY_PLL_SDM_CFG4 (0x042c)
  26. #define HDMI_PHY_PLL_SSC_CFG0 (0x0430)
  27. #define HDMI_PHY_PLL_SSC_CFG1 (0x0434)
  28. #define HDMI_PHY_PLL_SSC_CFG2 (0x0438)
  29. #define HDMI_PHY_PLL_SSC_CFG3 (0x043c)
  30. #define HDMI_PHY_PLL_LOCKDET_CFG0 (0x0440)
  31. #define HDMI_PHY_PLL_LOCKDET_CFG1 (0x0444)
  32. #define HDMI_PHY_PLL_LOCKDET_CFG2 (0x0448)
  33. #define HDMI_PHY_PLL_VCOCAL_CFG0 (0x044c)
  34. #define HDMI_PHY_PLL_VCOCAL_CFG1 (0x0450)
  35. #define HDMI_PHY_PLL_VCOCAL_CFG2 (0x0454)
  36. #define HDMI_PHY_PLL_VCOCAL_CFG3 (0x0458)
  37. #define HDMI_PHY_PLL_VCOCAL_CFG4 (0x045c)
  38. #define HDMI_PHY_PLL_VCOCAL_CFG5 (0x0460)
  39. #define HDMI_PHY_PLL_VCOCAL_CFG6 (0x0464)
  40. #define HDMI_PHY_PLL_VCOCAL_CFG7 (0x0468)
  41. #define HDMI_PHY_PLL_DEBUG_SEL (0x046c)
  42. #define HDMI_PHY_PLL_MISC0 (0x0470)
  43. #define HDMI_PHY_PLL_MISC1 (0x0474)
  44. #define HDMI_PHY_PLL_MISC2 (0x0478)
  45. #define HDMI_PHY_PLL_MISC3 (0x047c)
  46. #define HDMI_PHY_PLL_MISC4 (0x0480)
  47. #define HDMI_PHY_PLL_MISC5 (0x0484)
  48. #define HDMI_PHY_PLL_MISC6 (0x0488)
  49. #define HDMI_PHY_PLL_DEBUG_BUS0 (0x048c)
  50. #define HDMI_PHY_PLL_DEBUG_BUS1 (0x0490)
  51. #define HDMI_PHY_PLL_DEBUG_BUS2 (0x0494)
  52. #define HDMI_PHY_PLL_STATUS0 (0x0498)
  53. #define HDMI_PHY_PLL_STATUS1 (0x049c)
  54. #define HDMI_PHY_REG_0 (0x0000)
  55. #define HDMI_PHY_REG_1 (0x0004)
  56. #define HDMI_PHY_REG_2 (0x0008)
  57. #define HDMI_PHY_REG_3 (0x000c)
  58. #define HDMI_PHY_REG_4 (0x0010)
  59. #define HDMI_PHY_REG_5 (0x0014)
  60. #define HDMI_PHY_REG_6 (0x0018)
  61. #define HDMI_PHY_REG_7 (0x001c)
  62. #define HDMI_PHY_REG_8 (0x0020)
  63. #define HDMI_PHY_REG_9 (0x0024)
  64. #define HDMI_PHY_REG_10 (0x0028)
  65. #define HDMI_PHY_REG_11 (0x002c)
  66. #define HDMI_PHY_REG_12 (0x0030)
  67. #define HDMI_PHY_REG_BIST_CFG (0x0034)
  68. #define HDMI_PHY_DEBUG_BUS_SEL (0x0038)
  69. #define HDMI_PHY_REG_MISC0 (0x003c)
  70. #define HDMI_PHY_REG_13 (0x0040)
  71. #define HDMI_PHY_REG_14 (0x0044)
  72. #define HDMI_PHY_REG_15 (0x0048)
  73. /* HDMI PHY/PLL bit field macros */
  74. #define SW_RESET BIT(2)
  75. #define SW_RESET_PLL BIT(0)
  76. #define PWRDN_B BIT(7)
  77. #define PLL_PWRDN_B BIT(3)
  78. #define REG_VTEST_EN BIT(2)
  79. #define PD_PLL BIT(1)
  80. #define PD_PLL_REG BIT(0)
  81. #define HDMI_PLL_POLL_DELAY_US 50
  82. #define HDMI_PLL_POLL_TIMEOUT_US 500
  83. static int hdmi_pll_lock_status(struct mdss_pll_resources *hdmi_pll_res)
  84. {
  85. u32 status;
  86. int pll_locked = 0;
  87. int rc;
  88. rc = mdss_pll_resource_enable(hdmi_pll_res, true);
  89. if (rc) {
  90. pr_err("pll resource can't be enabled\n");
  91. return rc;
  92. }
  93. /* poll for PLL ready status */
  94. if (readl_poll_timeout_atomic(
  95. (hdmi_pll_res->pll_base + HDMI_PHY_PLL_STATUS0),
  96. status, ((status & BIT(0)) == 1),
  97. HDMI_PLL_POLL_DELAY_US,
  98. HDMI_PLL_POLL_TIMEOUT_US)) {
  99. pr_debug("HDMI PLL status=%x failed to Lock\n", status);
  100. pll_locked = 0;
  101. } else {
  102. pr_debug("HDMI PLL locked\n");
  103. pll_locked = 1;
  104. }
  105. mdss_pll_resource_enable(hdmi_pll_res, false);
  106. return pll_locked;
  107. }
  108. static void hdmi_pll_disable_28lpm(struct clk_hw *hw)
  109. {
  110. struct hdmi_pll_vco_clk *vco = to_hdmi_vco_clk_hw(hw);
  111. struct mdss_pll_resources *hdmi_pll_res = vco->priv;
  112. u32 val;
  113. if (!hdmi_pll_res) {
  114. pr_err("Invalid input parameter\n");
  115. return;
  116. }
  117. val = MDSS_PLL_REG_R(hdmi_pll_res->pll_base, HDMI_PHY_REG_12);
  118. val &= (~PWRDN_B);
  119. MDSS_PLL_REG_W(hdmi_pll_res->pll_base, HDMI_PHY_REG_12, val);
  120. val = MDSS_PLL_REG_R(hdmi_pll_res->pll_base, HDMI_PHY_PLL_PWRDN_B);
  121. val |= PD_PLL;
  122. val &= (~PLL_PWRDN_B);
  123. MDSS_PLL_REG_W(hdmi_pll_res->pll_base, HDMI_PHY_PLL_PWRDN_B, val);
  124. /* Make sure HDMI PHY/PLL are powered down */
  125. wmb();
  126. } /* hdmi_pll_disable_28lpm */
  127. static int hdmi_pll_enable_28lpm(struct clk_hw *hw)
  128. {
  129. struct hdmi_pll_vco_clk *vco = to_hdmi_vco_clk_hw(hw);
  130. struct mdss_pll_resources *hdmi_pll_res = vco->priv;
  131. void __iomem *pll_base;
  132. u32 val;
  133. int pll_lock_retry = 10;
  134. pll_base = hdmi_pll_res->pll_base;
  135. /* Assert PLL S/W reset */
  136. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_LOCKDET_CFG2, 0x8d);
  137. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_LOCKDET_CFG0, 0x10);
  138. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_LOCKDET_CFG1, 0x1a);
  139. udelay(10);
  140. /* De-assert PLL S/W reset */
  141. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_LOCKDET_CFG2, 0x0d);
  142. MDSS_PLL_REG_W(pll_base, HDMI_PHY_REG_1, 0xf2);
  143. udelay(10);
  144. MDSS_PLL_REG_W(pll_base, HDMI_PHY_REG_2, 0x1f);
  145. val = MDSS_PLL_REG_R(pll_base, HDMI_PHY_REG_12);
  146. val |= BIT(5);
  147. /* Assert PHY S/W reset */
  148. MDSS_PLL_REG_W(pll_base, HDMI_PHY_REG_12, val);
  149. val &= ~BIT(5);
  150. udelay(10);
  151. /* De-assert PHY S/W reset */
  152. MDSS_PLL_REG_W(pll_base, HDMI_PHY_REG_12, val);
  153. val = MDSS_PLL_REG_R(pll_base, HDMI_PHY_REG_12);
  154. val |= PWRDN_B;
  155. MDSS_PLL_REG_W(pll_base, HDMI_PHY_REG_12, val);
  156. /* Wait 10 us for enabling global power for PHY */
  157. wmb();
  158. udelay(10);
  159. MDSS_PLL_REG_W(pll_base, HDMI_PHY_REG_3, 0x20);
  160. MDSS_PLL_REG_W(pll_base, HDMI_PHY_REG_4, 0x10);
  161. val = MDSS_PLL_REG_R(pll_base, HDMI_PHY_PLL_PWRDN_B);
  162. val |= PLL_PWRDN_B;
  163. val |= REG_VTEST_EN;
  164. val &= ~PD_PLL;
  165. val |= PD_PLL_REG;
  166. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_PWRDN_B, val);
  167. MDSS_PLL_REG_W(pll_base, HDMI_PHY_REG_2, 0x81);
  168. do {
  169. if (!hdmi_pll_lock_status(hdmi_pll_res)) {
  170. /* PLL has still not locked.
  171. * Do a software reset and try again
  172. * Assert PLL S/W reset first
  173. */
  174. MDSS_PLL_REG_W(pll_base,
  175. HDMI_PHY_PLL_LOCKDET_CFG2, 0x8d);
  176. /* Wait for a short time before de-asserting
  177. * to allow the hardware to complete its job.
  178. * This much of delay should be fine for hardware
  179. * to assert and de-assert.
  180. */
  181. udelay(10);
  182. MDSS_PLL_REG_W(pll_base,
  183. HDMI_PHY_PLL_LOCKDET_CFG2, 0xd);
  184. /* Wait for a short duration for the PLL calibration
  185. * before checking if the PLL gets locked
  186. */
  187. udelay(350);
  188. } else {
  189. pr_debug("HDMI PLL locked\n");
  190. break;
  191. }
  192. } while (--pll_lock_retry);
  193. if (!pll_lock_retry) {
  194. pr_err("HDMI PLL not locked\n");
  195. hdmi_pll_disable_28lpm(hw);
  196. return -EAGAIN;
  197. }
  198. return 0;
  199. } /* hdmi_pll_enable_28lpm */
  200. static void hdmi_phy_pll_calculator_28lpm(unsigned long vco_rate,
  201. struct mdss_pll_resources *hdmi_pll_res)
  202. {
  203. u32 ref_clk = 19200000;
  204. u32 integer_mode = 0;
  205. u32 ref_clk_multiplier = integer_mode == 0 ? 2 : 1;
  206. u32 int_ref_clk_freq = ref_clk * ref_clk_multiplier;
  207. u32 refclk_cfg = 0;
  208. u32 ten_power_six = 1000000;
  209. u64 multiplier_q = 0;
  210. u64 multiplier_r = 0;
  211. u32 lf_cfg0 = 0;
  212. u32 lf_cfg1 = 0;
  213. u64 vco_cfg0 = 0;
  214. u64 vco_cfg4 = 0;
  215. u64 sdm_cfg0 = 0;
  216. u64 sdm_cfg1 = 0;
  217. u64 sdm_cfg2 = 0;
  218. u32 val1 = 0;
  219. u32 val2 = 0;
  220. u32 val3 = 0;
  221. void __iomem *pll_base = hdmi_pll_res->pll_base;
  222. multiplier_q = vco_rate;
  223. multiplier_r = do_div(multiplier_q, int_ref_clk_freq);
  224. lf_cfg0 = multiplier_q > 30 ? 0 : (multiplier_q > 16 ? 16 : 32);
  225. lf_cfg0 += integer_mode;
  226. lf_cfg1 = multiplier_q > 30 ? 0xc3 : (multiplier_q > 16 ? 0xbb : 0xf9);
  227. vco_cfg0 = vco_rate / ten_power_six;
  228. vco_cfg4 = ((ref_clk * 5) / ten_power_six) - 1;
  229. sdm_cfg0 = (integer_mode * 64) + multiplier_q - 1;
  230. sdm_cfg1 = 64 + multiplier_q - 1;
  231. sdm_cfg2 = (multiplier_r) * 65536;
  232. do_div(sdm_cfg2, int_ref_clk_freq);
  233. pr_debug("lf_cfg0 = 0x%x lf_cfg1 = 0x%x\n", lf_cfg0, lf_cfg1);
  234. pr_debug("vco_cfg0 = 0x%llx vco_cfg4 = 0x%llx\n", vco_cfg0, vco_cfg4);
  235. pr_debug("sdm_cfg0 = 0x%llx sdm_cfg1 = 0x%llx sdm_cfg2 = 0x%llx\n",
  236. sdm_cfg0, sdm_cfg1, sdm_cfg2);
  237. refclk_cfg = MDSS_PLL_REG_R(pll_base, HDMI_PHY_PLL_REFCLK_CFG);
  238. refclk_cfg &= ~0xf;
  239. refclk_cfg |= (ref_clk_multiplier == 2) ? 0x8
  240. : (ref_clk_multiplier == 1) ? 0 : 0x2;
  241. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_REFCLK_CFG, refclk_cfg);
  242. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_CHRG_PUMP_CFG, 0x02);
  243. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_LOOP_FLT_CFG0, lf_cfg0);
  244. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_LOOP_FLT_CFG1, lf_cfg1);
  245. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_IDAC_ADJ_CFG, 0x2c);
  246. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_I_VI_KVCO_CFG, 0x06);
  247. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_PWRDN_B, 0x0a);
  248. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_SDM_CFG0, sdm_cfg0);
  249. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_SDM_CFG1, sdm_cfg1);
  250. val1 = sdm_cfg2 & 0xff;
  251. val2 = (sdm_cfg2 >> 8) & 0xff;
  252. val3 = (sdm_cfg2 >> 16) & 0xff;
  253. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_SDM_CFG2, val1);
  254. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_SDM_CFG3, val2);
  255. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_SDM_CFG4, val3);
  256. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_SSC_CFG0, 0x9a);
  257. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_SSC_CFG1, 0x00);
  258. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_SSC_CFG2, 0x00);
  259. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_SSC_CFG3, 0x00);
  260. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_LOCKDET_CFG0, 0x10);
  261. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_LOCKDET_CFG1, 0x1a);
  262. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_LOCKDET_CFG2, 0x0d);
  263. val1 = vco_cfg0 & 0xff;
  264. val2 = (vco_cfg0 >> 8) & 0xff;
  265. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_VCOCAL_CFG0, val1);
  266. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_VCOCAL_CFG1, val2);
  267. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_VCOCAL_CFG2, 0x3b);
  268. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_VCOCAL_CFG3, 0x00);
  269. val1 = vco_cfg4 & 0xff;
  270. val2 = (vco_cfg4 >> 8) & 0xff;
  271. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_VCOCAL_CFG4, val1);
  272. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_VCOCAL_CFG5, val2);
  273. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_VCOCAL_CFG6, 0x33);
  274. MDSS_PLL_REG_W(pll_base, HDMI_PHY_PLL_VCOCAL_CFG7, 0x03);
  275. }
  276. int hdmi_vco_set_rate_28lpm(struct clk_hw *hw, unsigned long rate,
  277. unsigned long parent_rate)
  278. {
  279. struct hdmi_pll_vco_clk *vco = to_hdmi_vco_clk_hw(hw);
  280. struct mdss_pll_resources *hdmi_pll_res = vco->priv;
  281. void __iomem *pll_base;
  282. int rc;
  283. rc = mdss_pll_resource_enable(hdmi_pll_res, true);
  284. if (rc) {
  285. pr_err("pll resource can't be enabled\n");
  286. return rc;
  287. }
  288. if (hdmi_pll_res->pll_on)
  289. return 0;
  290. pll_base = hdmi_pll_res->pll_base;
  291. pr_debug("rate=%ld\n", rate);
  292. hdmi_phy_pll_calculator_28lpm(rate, hdmi_pll_res);
  293. /* Make sure writes complete before disabling iface clock */
  294. wmb();
  295. vco->rate = rate;
  296. hdmi_pll_res->vco_current_rate = rate;
  297. mdss_pll_resource_enable(hdmi_pll_res, false);
  298. return 0;
  299. } /* hdmi_pll_set_rate */
  300. static unsigned long hdmi_vco_get_rate(struct hdmi_pll_vco_clk *vco)
  301. {
  302. unsigned long freq = 0;
  303. int rc = 0;
  304. struct mdss_pll_resources *hdmi_pll_res = vco->priv;
  305. rc = mdss_pll_resource_enable(hdmi_pll_res, true);
  306. if (rc) {
  307. pr_err("Failed to enable hdmi pll resources\n");
  308. return 0;
  309. }
  310. freq = MDSS_PLL_REG_R(hdmi_pll_res->pll_base,
  311. HDMI_PHY_PLL_VCOCAL_CFG1) << 8 |
  312. MDSS_PLL_REG_R(hdmi_pll_res->pll_base,
  313. HDMI_PHY_PLL_VCOCAL_CFG0);
  314. switch (freq) {
  315. case 742:
  316. freq = 742500000;
  317. break;
  318. case 810:
  319. if (MDSS_PLL_REG_R(hdmi_pll_res->pll_base,
  320. HDMI_PHY_PLL_SDM_CFG3) == 0x18)
  321. freq = 810000000;
  322. else
  323. freq = 810900000;
  324. break;
  325. case 1342:
  326. freq = 1342500000;
  327. break;
  328. default:
  329. freq *= 1000000;
  330. }
  331. mdss_pll_resource_enable(hdmi_pll_res, false);
  332. return freq;
  333. }
  334. long hdmi_vco_round_rate_28lpm(struct clk_hw *hw, unsigned long rate,
  335. unsigned long *parent_rate)
  336. {
  337. unsigned long rrate = rate;
  338. struct hdmi_pll_vco_clk *vco = to_hdmi_vco_clk_hw(hw);
  339. if (rate < vco->min_rate)
  340. rrate = vco->min_rate;
  341. if (rate > vco->max_rate)
  342. rrate = vco->max_rate;
  343. *parent_rate = rrate;
  344. pr_debug("rrate=%ld\n", rrate);
  345. return rrate;
  346. }
  347. int hdmi_vco_prepare_28lpm(struct clk_hw *hw)
  348. {
  349. int rc = 0;
  350. struct hdmi_pll_vco_clk *vco = to_hdmi_vco_clk_hw(hw);
  351. struct mdss_pll_resources *hdmi_res = vco->priv;
  352. pr_debug("rate=%ld\n", clk_hw_get_rate(hw));
  353. rc = mdss_pll_resource_enable(hdmi_res, true);
  354. if (rc) {
  355. pr_err("Failed to enable mdss HDMI pll resources\n");
  356. goto error;
  357. }
  358. if ((hdmi_res->vco_cached_rate != 0)
  359. && (hdmi_res->vco_cached_rate == clk_hw_get_rate(hw))) {
  360. rc = vco->hw.init->ops->set_rate(hw,
  361. hdmi_res->vco_cached_rate, hdmi_res->vco_cached_rate);
  362. if (rc) {
  363. pr_err("index=%d vco_set_rate failed. rc=%d\n",
  364. rc, hdmi_res->index);
  365. mdss_pll_resource_enable(hdmi_res, false);
  366. goto error;
  367. }
  368. }
  369. rc = hdmi_pll_enable_28lpm(hw);
  370. if (rc) {
  371. mdss_pll_resource_enable(hdmi_res, false);
  372. pr_err("ndx=%d failed to enable hdmi pll\n",
  373. hdmi_res->index);
  374. goto error;
  375. }
  376. mdss_pll_resource_enable(hdmi_res, false);
  377. pr_debug("HDMI PLL enabled\n");
  378. error:
  379. return rc;
  380. }
  381. void hdmi_vco_unprepare_28lpm(struct clk_hw *hw)
  382. {
  383. struct hdmi_pll_vco_clk *vco = to_hdmi_vco_clk_hw(hw);
  384. struct mdss_pll_resources *hdmi_res = vco->priv;
  385. if (!hdmi_res) {
  386. pr_err("Invalid input parameter\n");
  387. return;
  388. }
  389. if (!hdmi_res->pll_on &&
  390. mdss_pll_resource_enable(hdmi_res, true)) {
  391. pr_err("pll resource can't be enabled\n");
  392. return;
  393. }
  394. hdmi_res->vco_cached_rate = clk_hw_get_rate(hw);
  395. hdmi_pll_disable_28lpm(hw);
  396. hdmi_res->handoff_resources = false;
  397. mdss_pll_resource_enable(hdmi_res, false);
  398. hdmi_res->pll_on = false;
  399. pr_debug("HDMI PLL disabled\n");
  400. }
  401. unsigned long hdmi_vco_recalc_rate_28lpm(struct clk_hw *hw,
  402. unsigned long parent_rate)
  403. {
  404. struct hdmi_pll_vco_clk *vco = to_hdmi_vco_clk_hw(hw);
  405. struct mdss_pll_resources *hdmi_pll_res = vco->priv;
  406. u64 vco_rate = 0;
  407. if (!hdmi_pll_res) {
  408. pr_err("dsi pll resources not available\n");
  409. return 0;
  410. }
  411. if (hdmi_pll_res->vco_current_rate) {
  412. vco_rate = (unsigned long)hdmi_pll_res->vco_current_rate;
  413. pr_debug("vco_rate=%lld\n", vco_rate);
  414. return vco_rate;
  415. }
  416. if (is_gdsc_disabled(hdmi_pll_res))
  417. return 0;
  418. if (mdss_pll_resource_enable(hdmi_pll_res, true)) {
  419. pr_err("Failed to enable hdmi pll resources\n");
  420. return 0;
  421. }
  422. if (hdmi_pll_lock_status(hdmi_pll_res)) {
  423. hdmi_pll_res->handoff_resources = true;
  424. hdmi_pll_res->pll_on = true;
  425. vco_rate = hdmi_vco_get_rate(vco);
  426. } else {
  427. hdmi_pll_res->handoff_resources = false;
  428. mdss_pll_resource_enable(hdmi_pll_res, false);
  429. }
  430. pr_debug("vco_rate = %lld\n", vco_rate);
  431. return (unsigned long)vco_rate;
  432. }
  433. static int hdmi_mux_set_parent(void *context, unsigned int reg,
  434. unsigned int mux_sel)
  435. {
  436. struct mdss_pll_resources *hdmi_pll_res = context;
  437. int rc = 0;
  438. u32 reg_val = 0;
  439. const u32 div_4 = 0x20;
  440. const u32 div_6 = 0x30;
  441. rc = mdss_pll_resource_enable(hdmi_pll_res, true);
  442. if (rc) {
  443. pr_err("Failed to enable hdmi pll resources\n");
  444. return rc;
  445. }
  446. /*
  447. * divsel_six is preferred over divsel_four to keep
  448. * vco range within goal limits to maintain margin.
  449. * To achieve this, its precedence order is toggled
  450. * at mux level. So reverse toggle the mux_sel value
  451. * here.
  452. */
  453. switch (mux_sel) {
  454. case 0x20: /* intended divider is divsel_six */
  455. mux_sel = div_6;
  456. break;
  457. case 0x30: /* intended divider is divsel_four */
  458. mux_sel = div_4;
  459. break;
  460. }
  461. pr_debug("mux_sel = %d\n", mux_sel);
  462. reg_val = MDSS_PLL_REG_R(hdmi_pll_res->pll_base,
  463. HDMI_PHY_PLL_REFCLK_CFG);
  464. reg_val &= ~0x70;
  465. reg_val |= (mux_sel & 0x70);
  466. pr_debug("pll_refclk_cfg = 0x%x\n", reg_val);
  467. MDSS_PLL_REG_W(hdmi_pll_res->pll_base,
  468. HDMI_PHY_PLL_REFCLK_CFG, reg_val);
  469. (void)mdss_pll_resource_enable(hdmi_pll_res, false);
  470. return 0;
  471. }
  472. static int hdmi_mux_get_parent(void *context, unsigned int reg,
  473. unsigned int *val)
  474. {
  475. int rc = 0;
  476. int mux_sel = 0;
  477. struct mdss_pll_resources *hdmi_pll_res = context;
  478. rc = mdss_pll_resource_enable(hdmi_pll_res, true);
  479. if (rc) {
  480. *val = 0;
  481. pr_err("Failed to enable hdmi pll resources\n");
  482. } else {
  483. mux_sel = MDSS_PLL_REG_R(hdmi_pll_res->pll_base,
  484. HDMI_PHY_PLL_REFCLK_CFG);
  485. mux_sel &= 0x70;
  486. *val = mux_sel;
  487. pr_debug("mux_sel = %d\n", *val);
  488. }
  489. (void)mdss_pll_resource_enable(hdmi_pll_res, false);
  490. return rc;
  491. }
  492. static struct regmap_config hdmi_pll_28lpm_cfg = {
  493. .reg_bits = 32,
  494. .reg_stride = 4,
  495. .val_bits = 32,
  496. .max_register = 0x49c,
  497. };
  498. static struct regmap_bus hdmi_pclk_src_mux_regmap_ops = {
  499. .reg_write = hdmi_mux_set_parent,
  500. .reg_read = hdmi_mux_get_parent,
  501. };
  502. /* Op structures */
  503. static const struct clk_ops hdmi_28lpm_vco_clk_ops = {
  504. .recalc_rate = hdmi_vco_recalc_rate_28lpm,
  505. .set_rate = hdmi_vco_set_rate_28lpm,
  506. .round_rate = hdmi_vco_round_rate_28lpm,
  507. .prepare = hdmi_vco_prepare_28lpm,
  508. .unprepare = hdmi_vco_unprepare_28lpm,
  509. };
  510. static struct hdmi_pll_vco_clk hdmi_vco_clk = {
  511. .min_rate = 540000000,
  512. .max_rate = 1125000000,
  513. .hw.init = &(struct clk_init_data){
  514. .name = "hdmi_vco_clk",
  515. .parent_names = (const char *[]){ "cxo" },
  516. .num_parents = 1,
  517. .ops = &hdmi_28lpm_vco_clk_ops,
  518. },
  519. };
  520. static struct clk_fixed_factor hdmi_vco_divsel_one_clk_src = {
  521. .div = 1,
  522. .mult = 1,
  523. .hw.init = &(struct clk_init_data){
  524. .name = "hdmi_vco_divsel_one_clk_src",
  525. .parent_names =
  526. (const char *[]){ "hdmi_vco_clk" },
  527. .num_parents = 1,
  528. .flags = CLK_SET_RATE_PARENT,
  529. .ops = &clk_fixed_factor_ops,
  530. },
  531. };
  532. static struct clk_fixed_factor hdmi_vco_divsel_two_clk_src = {
  533. .div = 2,
  534. .mult = 1,
  535. .hw.init = &(struct clk_init_data){
  536. .name = "hdmi_vco_divsel_two_clk_src",
  537. .parent_names =
  538. (const char *[]){ "hdmi_vco_clk" },
  539. .num_parents = 1,
  540. .flags = CLK_SET_RATE_PARENT,
  541. .ops = &clk_fixed_factor_ops,
  542. },
  543. };
  544. static struct clk_fixed_factor hdmi_vco_divsel_four_clk_src = {
  545. .div = 4,
  546. .mult = 1,
  547. .hw.init = &(struct clk_init_data){
  548. .name = "hdmi_vco_divsel_four_clk_src",
  549. .parent_names =
  550. (const char *[]){ "hdmi_vco_clk" },
  551. .num_parents = 1,
  552. .flags = CLK_SET_RATE_PARENT,
  553. .ops = &clk_fixed_factor_ops,
  554. },
  555. };
  556. static struct clk_fixed_factor hdmi_vco_divsel_six_clk_src = {
  557. .div = 6,
  558. .mult = 1,
  559. .hw.init = &(struct clk_init_data){
  560. .name = "hdmi_vco_divsel_six_clk_src",
  561. .parent_names =
  562. (const char *[]){ "hdmi_vco_clk" },
  563. .num_parents = 1,
  564. .flags = CLK_SET_RATE_PARENT,
  565. .ops = &clk_fixed_factor_ops,
  566. },
  567. };
  568. static struct clk_regmap_mux hdmi_pclk_src_mux = {
  569. .reg = HDMI_PHY_PLL_REFCLK_CFG,
  570. .shift = 4,
  571. .width = 2,
  572. .clkr = {
  573. .hw.init = &(struct clk_init_data){
  574. .name = "hdmi_pclk_src_mux",
  575. .parent_names =
  576. (const char *[]){"hdmi_vco_divsel_one_clk_src",
  577. "hdmi_vco_divsel_two_clk_src",
  578. "hdmi_vco_divsel_six_clk_src",
  579. "hdmi_vco_divsel_four_clk_src"},
  580. .num_parents = 4,
  581. .ops = &clk_regmap_mux_closest_ops,
  582. .flags = CLK_SET_RATE_PARENT,
  583. },
  584. },
  585. };
  586. static struct clk_fixed_factor hdmi_pclk_src = {
  587. .div = 5,
  588. .mult = 1,
  589. .hw.init = &(struct clk_init_data){
  590. .name = "hdmi_phy_pll_clk",
  591. .parent_names =
  592. (const char *[]){ "hdmi_pclk_src_mux" },
  593. .num_parents = 1,
  594. .flags = CLK_SET_RATE_PARENT,
  595. .ops = &clk_fixed_factor_ops,
  596. },
  597. };
  598. static struct clk_hw *mdss_hdmi_pllcc_28lpm[] = {
  599. [HDMI_VCO_CLK] = &hdmi_vco_clk.hw,
  600. [HDMI_VCO_DIVIDED_1_CLK_SRC] = &hdmi_vco_divsel_one_clk_src.hw,
  601. [HDMI_VCO_DIVIDED_TWO_CLK_SRC] = &hdmi_vco_divsel_two_clk_src.hw,
  602. [HDMI_VCO_DIVIDED_FOUR_CLK_SRC] = &hdmi_vco_divsel_four_clk_src.hw,
  603. [HDMI_VCO_DIVIDED_SIX_CLK_SRC] = &hdmi_vco_divsel_six_clk_src.hw,
  604. [HDMI_PCLK_SRC_MUX] = &hdmi_pclk_src_mux.clkr.hw,
  605. [HDMI_PCLK_SRC] = &hdmi_pclk_src.hw,
  606. };
  607. int hdmi_pll_clock_register_28lpm(struct platform_device *pdev,
  608. struct mdss_pll_resources *pll_res)
  609. {
  610. int rc = -ENOTSUPP, i;
  611. struct clk *clk;
  612. struct clk_onecell_data *clk_data;
  613. int num_clks = ARRAY_SIZE(mdss_hdmi_pllcc_28lpm);
  614. struct regmap *regmap;
  615. if (!pdev || !pdev->dev.of_node ||
  616. !pll_res || !pll_res->pll_base) {
  617. pr_err("Invalid input parameters\n");
  618. return -EPROBE_DEFER;
  619. }
  620. clk_data = devm_kzalloc(&pdev->dev, sizeof(struct clk_onecell_data),
  621. GFP_KERNEL);
  622. if (!clk_data)
  623. return -ENOMEM;
  624. clk_data->clks = devm_kzalloc(&pdev->dev, (num_clks *
  625. sizeof(struct clk *)), GFP_KERNEL);
  626. if (!clk_data->clks) {
  627. devm_kfree(&pdev->dev, clk_data);
  628. return -ENOMEM;
  629. }
  630. clk_data->clk_num = num_clks;
  631. /* Set client data for vco, mux and div clocks */
  632. regmap = devm_regmap_init(&pdev->dev, &hdmi_pclk_src_mux_regmap_ops,
  633. pll_res, &hdmi_pll_28lpm_cfg);
  634. hdmi_pclk_src_mux.clkr.regmap = regmap;
  635. hdmi_vco_clk.priv = pll_res;
  636. for (i = HDMI_VCO_CLK; i <= HDMI_PCLK_SRC; i++) {
  637. pr_debug("reg clk: %d index: %d\n", i, pll_res->index);
  638. clk = devm_clk_register(&pdev->dev,
  639. mdss_hdmi_pllcc_28lpm[i]);
  640. if (IS_ERR(clk)) {
  641. pr_err("clk registration failed for HDMI: %d\n",
  642. pll_res->index);
  643. rc = -EINVAL;
  644. goto clk_reg_fail;
  645. }
  646. clk_data->clks[i] = clk;
  647. }
  648. rc = of_clk_add_provider(pdev->dev.of_node,
  649. of_clk_src_onecell_get, clk_data);
  650. if (rc) {
  651. pr_err("%s: Clock register failed rc=%d\n", __func__, rc);
  652. rc = -EPROBE_DEFER;
  653. } else {
  654. pr_debug("%s SUCCESS\n", __func__);
  655. rc = 0;
  656. }
  657. return rc;
  658. clk_reg_fail:
  659. return rc;
  660. }