hdmi_pll.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * HDMI PLL
  3. *
  4. * Copyright (C) 2013 Texas Instruments Incorporated
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. */
  10. #define DSS_SUBSYS_NAME "HDMIPLL"
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/err.h>
  14. #include <linux/io.h>
  15. #include <linux/platform_device.h>
  16. #include <video/omapdss.h>
  17. #include "dss.h"
  18. #include "hdmi.h"
  19. #define HDMI_DEFAULT_REGN 16
  20. #define HDMI_DEFAULT_REGM2 1
  21. struct hdmi_pll_features {
  22. bool sys_reset;
  23. /* this is a hack, need to replace it with a better computation of M2 */
  24. bool bound_dcofreq;
  25. unsigned long fint_min, fint_max;
  26. u16 regm_max;
  27. unsigned long dcofreq_low_min, dcofreq_low_max;
  28. unsigned long dcofreq_high_min, dcofreq_high_max;
  29. };
  30. static const struct hdmi_pll_features *pll_feat;
  31. void hdmi_pll_dump(struct hdmi_pll_data *pll, struct seq_file *s)
  32. {
  33. #define DUMPPLL(r) seq_printf(s, "%-35s %08x\n", #r,\
  34. hdmi_read_reg(pll->base, r))
  35. DUMPPLL(PLLCTRL_PLL_CONTROL);
  36. DUMPPLL(PLLCTRL_PLL_STATUS);
  37. DUMPPLL(PLLCTRL_PLL_GO);
  38. DUMPPLL(PLLCTRL_CFG1);
  39. DUMPPLL(PLLCTRL_CFG2);
  40. DUMPPLL(PLLCTRL_CFG3);
  41. DUMPPLL(PLLCTRL_SSC_CFG1);
  42. DUMPPLL(PLLCTRL_SSC_CFG2);
  43. DUMPPLL(PLLCTRL_CFG4);
  44. }
  45. void hdmi_pll_compute(struct hdmi_pll_data *pll, unsigned long clkin, int phy)
  46. {
  47. struct hdmi_pll_info *pi = &pll->info;
  48. unsigned long refclk;
  49. u32 mf;
  50. /* use our funky units */
  51. clkin /= 10000;
  52. /*
  53. * Input clock is predivided by N + 1
  54. * out put of which is reference clk
  55. */
  56. pi->regn = HDMI_DEFAULT_REGN;
  57. refclk = clkin / pi->regn;
  58. /* temorary hack to make sure DCO freq isn't calculated too low */
  59. if (pll_feat->bound_dcofreq && phy <= 65000)
  60. pi->regm2 = 3;
  61. else
  62. pi->regm2 = HDMI_DEFAULT_REGM2;
  63. /*
  64. * multiplier is pixel_clk/ref_clk
  65. * Multiplying by 100 to avoid fractional part removal
  66. */
  67. pi->regm = phy * pi->regm2 / refclk;
  68. /*
  69. * fractional multiplier is remainder of the difference between
  70. * multiplier and actual phy(required pixel clock thus should be
  71. * multiplied by 2^18(262144) divided by the reference clock
  72. */
  73. mf = (phy - pi->regm / pi->regm2 * refclk) * 262144;
  74. pi->regmf = pi->regm2 * mf / refclk;
  75. /*
  76. * Dcofreq should be set to 1 if required pixel clock
  77. * is greater than 1000MHz
  78. */
  79. pi->dcofreq = phy > 1000 * 100;
  80. pi->regsd = ((pi->regm * clkin / 10) / (pi->regn * 250) + 5) / 10;
  81. /* Set the reference clock to sysclk reference */
  82. pi->refsel = HDMI_REFSEL_SYSCLK;
  83. DSSDBG("M = %d Mf = %d\n", pi->regm, pi->regmf);
  84. DSSDBG("range = %d sd = %d\n", pi->dcofreq, pi->regsd);
  85. }
  86. static int hdmi_pll_config(struct hdmi_pll_data *pll)
  87. {
  88. u32 r;
  89. struct hdmi_pll_info *fmt = &pll->info;
  90. /* PLL start always use manual mode */
  91. REG_FLD_MOD(pll->base, PLLCTRL_PLL_CONTROL, 0x0, 0, 0);
  92. r = hdmi_read_reg(pll->base, PLLCTRL_CFG1);
  93. r = FLD_MOD(r, fmt->regm, 20, 9); /* CFG1_PLL_REGM */
  94. r = FLD_MOD(r, fmt->regn - 1, 8, 1); /* CFG1_PLL_REGN */
  95. hdmi_write_reg(pll->base, PLLCTRL_CFG1, r);
  96. r = hdmi_read_reg(pll->base, PLLCTRL_CFG2);
  97. r = FLD_MOD(r, 0x0, 12, 12); /* PLL_HIGHFREQ divide by 2 */
  98. r = FLD_MOD(r, 0x1, 13, 13); /* PLL_REFEN */
  99. r = FLD_MOD(r, 0x0, 14, 14); /* PHY_CLKINEN de-assert during locking */
  100. r = FLD_MOD(r, fmt->refsel, 22, 21); /* REFSEL */
  101. if (fmt->dcofreq)
  102. r = FLD_MOD(r, 0x4, 3, 1); /* 1000MHz and 2000MHz */
  103. else
  104. r = FLD_MOD(r, 0x2, 3, 1); /* 500MHz and 1000MHz */
  105. hdmi_write_reg(pll->base, PLLCTRL_CFG2, r);
  106. REG_FLD_MOD(pll->base, PLLCTRL_CFG3, fmt->regsd, 17, 10);
  107. r = hdmi_read_reg(pll->base, PLLCTRL_CFG4);
  108. r = FLD_MOD(r, fmt->regm2, 24, 18);
  109. r = FLD_MOD(r, fmt->regmf, 17, 0);
  110. hdmi_write_reg(pll->base, PLLCTRL_CFG4, r);
  111. /* go now */
  112. REG_FLD_MOD(pll->base, PLLCTRL_PLL_GO, 0x1, 0, 0);
  113. /* wait for bit change */
  114. if (hdmi_wait_for_bit_change(pll->base, PLLCTRL_PLL_GO,
  115. 0, 0, 0) != 0) {
  116. DSSERR("PLL GO bit not clearing\n");
  117. return -ETIMEDOUT;
  118. }
  119. /* Wait till the lock bit is set in PLL status */
  120. if (hdmi_wait_for_bit_change(pll->base,
  121. PLLCTRL_PLL_STATUS, 1, 1, 1) != 1) {
  122. DSSERR("cannot lock PLL\n");
  123. DSSERR("CFG1 0x%x\n",
  124. hdmi_read_reg(pll->base, PLLCTRL_CFG1));
  125. DSSERR("CFG2 0x%x\n",
  126. hdmi_read_reg(pll->base, PLLCTRL_CFG2));
  127. DSSERR("CFG4 0x%x\n",
  128. hdmi_read_reg(pll->base, PLLCTRL_CFG4));
  129. return -ETIMEDOUT;
  130. }
  131. DSSDBG("PLL locked!\n");
  132. return 0;
  133. }
  134. static int hdmi_pll_reset(struct hdmi_pll_data *pll)
  135. {
  136. /* SYSRESET controlled by power FSM */
  137. REG_FLD_MOD(pll->base, PLLCTRL_PLL_CONTROL, pll_feat->sys_reset, 3, 3);
  138. /* READ 0x0 reset is in progress */
  139. if (hdmi_wait_for_bit_change(pll->base, PLLCTRL_PLL_STATUS, 0, 0, 1)
  140. != 1) {
  141. DSSERR("Failed to sysreset PLL\n");
  142. return -ETIMEDOUT;
  143. }
  144. return 0;
  145. }
  146. int hdmi_pll_enable(struct hdmi_pll_data *pll, struct hdmi_wp_data *wp)
  147. {
  148. u16 r = 0;
  149. r = hdmi_wp_set_pll_pwr(wp, HDMI_PLLPWRCMD_ALLOFF);
  150. if (r)
  151. return r;
  152. r = hdmi_wp_set_pll_pwr(wp, HDMI_PLLPWRCMD_BOTHON_ALLCLKS);
  153. if (r)
  154. return r;
  155. r = hdmi_pll_reset(pll);
  156. if (r)
  157. return r;
  158. r = hdmi_pll_config(pll);
  159. if (r)
  160. return r;
  161. return 0;
  162. }
  163. void hdmi_pll_disable(struct hdmi_pll_data *pll, struct hdmi_wp_data *wp)
  164. {
  165. hdmi_wp_set_pll_pwr(wp, HDMI_PLLPWRCMD_ALLOFF);
  166. }
  167. static const struct hdmi_pll_features omap44xx_pll_feats = {
  168. .sys_reset = false,
  169. .bound_dcofreq = false,
  170. .fint_min = 500000,
  171. .fint_max = 2500000,
  172. .regm_max = 4095,
  173. .dcofreq_low_min = 500000000,
  174. .dcofreq_low_max = 1000000000,
  175. .dcofreq_high_min = 1000000000,
  176. .dcofreq_high_max = 2000000000,
  177. };
  178. static const struct hdmi_pll_features omap54xx_pll_feats = {
  179. .sys_reset = true,
  180. .bound_dcofreq = true,
  181. .fint_min = 620000,
  182. .fint_max = 2500000,
  183. .regm_max = 2046,
  184. .dcofreq_low_min = 750000000,
  185. .dcofreq_low_max = 1500000000,
  186. .dcofreq_high_min = 1250000000,
  187. .dcofreq_high_max = 2500000000UL,
  188. };
  189. static int hdmi_pll_init_features(struct platform_device *pdev)
  190. {
  191. struct hdmi_pll_features *dst;
  192. const struct hdmi_pll_features *src;
  193. dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
  194. if (!dst) {
  195. dev_err(&pdev->dev, "Failed to allocate HDMI PHY Features\n");
  196. return -ENOMEM;
  197. }
  198. switch (omapdss_get_version()) {
  199. case OMAPDSS_VER_OMAP4430_ES1:
  200. case OMAPDSS_VER_OMAP4430_ES2:
  201. case OMAPDSS_VER_OMAP4:
  202. src = &omap44xx_pll_feats;
  203. break;
  204. case OMAPDSS_VER_OMAP5:
  205. src = &omap54xx_pll_feats;
  206. break;
  207. default:
  208. return -ENODEV;
  209. }
  210. memcpy(dst, src, sizeof(*dst));
  211. pll_feat = dst;
  212. return 0;
  213. }
  214. int hdmi_pll_init(struct platform_device *pdev, struct hdmi_pll_data *pll)
  215. {
  216. int r;
  217. struct resource *res;
  218. r = hdmi_pll_init_features(pdev);
  219. if (r)
  220. return r;
  221. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pll");
  222. if (!res) {
  223. DSSERR("can't get PLL mem resource\n");
  224. return -EINVAL;
  225. }
  226. pll->base = devm_ioremap_resource(&pdev->dev, res);
  227. if (IS_ERR(pll->base)) {
  228. DSSERR("can't ioremap PLLCTRL\n");
  229. return PTR_ERR(pll->base);
  230. }
  231. return 0;
  232. }