hdmi_pll.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 <linux/clk.h>
  17. #include <video/omapdss.h>
  18. #include "dss.h"
  19. #include "hdmi.h"
  20. void hdmi_pll_dump(struct hdmi_pll_data *pll, struct seq_file *s)
  21. {
  22. #define DUMPPLL(r) seq_printf(s, "%-35s %08x\n", #r,\
  23. hdmi_read_reg(pll->base, r))
  24. DUMPPLL(PLLCTRL_PLL_CONTROL);
  25. DUMPPLL(PLLCTRL_PLL_STATUS);
  26. DUMPPLL(PLLCTRL_PLL_GO);
  27. DUMPPLL(PLLCTRL_CFG1);
  28. DUMPPLL(PLLCTRL_CFG2);
  29. DUMPPLL(PLLCTRL_CFG3);
  30. DUMPPLL(PLLCTRL_SSC_CFG1);
  31. DUMPPLL(PLLCTRL_SSC_CFG2);
  32. DUMPPLL(PLLCTRL_CFG4);
  33. }
  34. void hdmi_pll_compute(struct hdmi_pll_data *pll,
  35. unsigned long target_tmds, struct dss_pll_clock_info *pi)
  36. {
  37. unsigned long fint, clkdco, clkout;
  38. unsigned long target_bitclk, target_clkdco;
  39. unsigned long min_dco;
  40. unsigned n, m, mf, m2, sd;
  41. unsigned long clkin;
  42. const struct dss_pll_hw *hw = pll->pll.hw;
  43. clkin = clk_get_rate(pll->pll.clkin);
  44. DSSDBG("clkin %lu, target tmds %lu\n", clkin, target_tmds);
  45. target_bitclk = target_tmds * 10;
  46. /* Fint */
  47. n = DIV_ROUND_UP(clkin, hw->fint_max);
  48. fint = clkin / n;
  49. /* adjust m2 so that the clkdco will be high enough */
  50. min_dco = roundup(hw->clkdco_min, fint);
  51. m2 = DIV_ROUND_UP(min_dco, target_bitclk);
  52. if (m2 == 0)
  53. m2 = 1;
  54. target_clkdco = target_bitclk * m2;
  55. m = target_clkdco / fint;
  56. clkdco = fint * m;
  57. /* adjust clkdco with fractional mf */
  58. if (WARN_ON(target_clkdco - clkdco > fint))
  59. mf = 0;
  60. else
  61. mf = (u32)div_u64(262144ull * (target_clkdco - clkdco), fint);
  62. if (mf > 0)
  63. clkdco += (u32)div_u64((u64)mf * fint, 262144);
  64. clkout = clkdco / m2;
  65. /* sigma-delta */
  66. sd = DIV_ROUND_UP(fint * m, 250000000);
  67. DSSDBG("N = %u, M = %u, M.f = %u, M2 = %u, SD = %u\n",
  68. n, m, mf, m2, sd);
  69. DSSDBG("Fint %lu, clkdco %lu, clkout %lu\n", fint, clkdco, clkout);
  70. pi->n = n;
  71. pi->m = m;
  72. pi->mf = mf;
  73. pi->mX[0] = m2;
  74. pi->sd = sd;
  75. pi->fint = fint;
  76. pi->clkdco = clkdco;
  77. pi->clkout[0] = clkout;
  78. }
  79. static int hdmi_pll_enable(struct dss_pll *dsspll)
  80. {
  81. struct hdmi_pll_data *pll = container_of(dsspll, struct hdmi_pll_data, pll);
  82. struct hdmi_wp_data *wp = pll->wp;
  83. u16 r = 0;
  84. r = hdmi_wp_set_pll_pwr(wp, HDMI_PLLPWRCMD_BOTHON_ALLCLKS);
  85. if (r)
  86. return r;
  87. return 0;
  88. }
  89. static void hdmi_pll_disable(struct dss_pll *dsspll)
  90. {
  91. struct hdmi_pll_data *pll = container_of(dsspll, struct hdmi_pll_data, pll);
  92. struct hdmi_wp_data *wp = pll->wp;
  93. hdmi_wp_set_pll_pwr(wp, HDMI_PLLPWRCMD_ALLOFF);
  94. }
  95. static const struct dss_pll_ops dsi_pll_ops = {
  96. .enable = hdmi_pll_enable,
  97. .disable = hdmi_pll_disable,
  98. .set_config = dss_pll_write_config_type_b,
  99. };
  100. static const struct dss_pll_hw dss_omap4_hdmi_pll_hw = {
  101. .n_max = 255,
  102. .m_min = 20,
  103. .m_max = 4095,
  104. .mX_max = 127,
  105. .fint_min = 500000,
  106. .fint_max = 2500000,
  107. .clkdco_min = 500000000,
  108. .clkdco_low = 1000000000,
  109. .clkdco_max = 2000000000,
  110. .n_msb = 8,
  111. .n_lsb = 1,
  112. .m_msb = 20,
  113. .m_lsb = 9,
  114. .mX_msb[0] = 24,
  115. .mX_lsb[0] = 18,
  116. .has_selfreqdco = true,
  117. };
  118. static const struct dss_pll_hw dss_omap5_hdmi_pll_hw = {
  119. .n_max = 255,
  120. .m_min = 20,
  121. .m_max = 2045,
  122. .mX_max = 127,
  123. .fint_min = 620000,
  124. .fint_max = 2500000,
  125. .clkdco_min = 750000000,
  126. .clkdco_low = 1500000000,
  127. .clkdco_max = 2500000000UL,
  128. .n_msb = 8,
  129. .n_lsb = 1,
  130. .m_msb = 20,
  131. .m_lsb = 9,
  132. .mX_msb[0] = 24,
  133. .mX_lsb[0] = 18,
  134. .has_selfreqdco = true,
  135. .has_refsel = true,
  136. };
  137. static int dsi_init_pll_data(struct platform_device *pdev, struct hdmi_pll_data *hpll)
  138. {
  139. struct dss_pll *pll = &hpll->pll;
  140. struct clk *clk;
  141. int r;
  142. clk = devm_clk_get(&pdev->dev, "sys_clk");
  143. if (IS_ERR(clk)) {
  144. DSSERR("can't get sys_clk\n");
  145. return PTR_ERR(clk);
  146. }
  147. pll->name = "hdmi";
  148. pll->base = hpll->base;
  149. pll->clkin = clk;
  150. switch (omapdss_get_version()) {
  151. case OMAPDSS_VER_OMAP4430_ES1:
  152. case OMAPDSS_VER_OMAP4430_ES2:
  153. case OMAPDSS_VER_OMAP4:
  154. pll->hw = &dss_omap4_hdmi_pll_hw;
  155. break;
  156. case OMAPDSS_VER_OMAP5:
  157. pll->hw = &dss_omap5_hdmi_pll_hw;
  158. break;
  159. default:
  160. return -ENODEV;
  161. }
  162. pll->ops = &dsi_pll_ops;
  163. r = dss_pll_register(pll);
  164. if (r)
  165. return r;
  166. return 0;
  167. }
  168. int hdmi_pll_init(struct platform_device *pdev, struct hdmi_pll_data *pll,
  169. struct hdmi_wp_data *wp)
  170. {
  171. int r;
  172. struct resource *res;
  173. pll->wp = wp;
  174. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pll");
  175. if (!res) {
  176. DSSERR("can't get PLL mem resource\n");
  177. return -EINVAL;
  178. }
  179. pll->base = devm_ioremap_resource(&pdev->dev, res);
  180. if (IS_ERR(pll->base)) {
  181. DSSERR("can't ioremap PLLCTRL\n");
  182. return PTR_ERR(pll->base);
  183. }
  184. r = dsi_init_pll_data(pdev, pll);
  185. if (r) {
  186. DSSERR("failed to init HDMI PLL\n");
  187. return r;
  188. }
  189. return 0;
  190. }
  191. void hdmi_pll_uninit(struct hdmi_pll_data *hpll)
  192. {
  193. struct dss_pll *pll = &hpll->pll;
  194. dss_pll_unregister(pll);
  195. }