hdmi_pll.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. dss_ctrl_pll_enable(DSS_PLL_HDMI, true);
  85. r = hdmi_wp_set_pll_pwr(wp, HDMI_PLLPWRCMD_BOTHON_ALLCLKS);
  86. if (r)
  87. return r;
  88. return 0;
  89. }
  90. static void hdmi_pll_disable(struct dss_pll *dsspll)
  91. {
  92. struct hdmi_pll_data *pll = container_of(dsspll, struct hdmi_pll_data, pll);
  93. struct hdmi_wp_data *wp = pll->wp;
  94. hdmi_wp_set_pll_pwr(wp, HDMI_PLLPWRCMD_ALLOFF);
  95. dss_ctrl_pll_enable(DSS_PLL_HDMI, false);
  96. }
  97. static const struct dss_pll_ops dsi_pll_ops = {
  98. .enable = hdmi_pll_enable,
  99. .disable = hdmi_pll_disable,
  100. .set_config = dss_pll_write_config_type_b,
  101. };
  102. static const struct dss_pll_hw dss_omap4_hdmi_pll_hw = {
  103. .n_max = 255,
  104. .m_min = 20,
  105. .m_max = 4095,
  106. .mX_max = 127,
  107. .fint_min = 500000,
  108. .fint_max = 2500000,
  109. .clkdco_min = 500000000,
  110. .clkdco_low = 1000000000,
  111. .clkdco_max = 2000000000,
  112. .n_msb = 8,
  113. .n_lsb = 1,
  114. .m_msb = 20,
  115. .m_lsb = 9,
  116. .mX_msb[0] = 24,
  117. .mX_lsb[0] = 18,
  118. .has_selfreqdco = true,
  119. };
  120. static const struct dss_pll_hw dss_omap5_hdmi_pll_hw = {
  121. .n_max = 255,
  122. .m_min = 20,
  123. .m_max = 2045,
  124. .mX_max = 127,
  125. .fint_min = 620000,
  126. .fint_max = 2500000,
  127. .clkdco_min = 750000000,
  128. .clkdco_low = 1500000000,
  129. .clkdco_max = 2500000000UL,
  130. .n_msb = 8,
  131. .n_lsb = 1,
  132. .m_msb = 20,
  133. .m_lsb = 9,
  134. .mX_msb[0] = 24,
  135. .mX_lsb[0] = 18,
  136. .has_selfreqdco = true,
  137. .has_refsel = true,
  138. };
  139. static int dsi_init_pll_data(struct platform_device *pdev, struct hdmi_pll_data *hpll)
  140. {
  141. struct dss_pll *pll = &hpll->pll;
  142. struct clk *clk;
  143. int r;
  144. clk = devm_clk_get(&pdev->dev, "sys_clk");
  145. if (IS_ERR(clk)) {
  146. DSSERR("can't get sys_clk\n");
  147. return PTR_ERR(clk);
  148. }
  149. pll->name = "hdmi";
  150. pll->id = DSS_PLL_HDMI;
  151. pll->base = hpll->base;
  152. pll->clkin = clk;
  153. switch (omapdss_get_version()) {
  154. case OMAPDSS_VER_OMAP4430_ES1:
  155. case OMAPDSS_VER_OMAP4430_ES2:
  156. case OMAPDSS_VER_OMAP4:
  157. pll->hw = &dss_omap4_hdmi_pll_hw;
  158. break;
  159. case OMAPDSS_VER_OMAP5:
  160. case OMAPDSS_VER_DRA7xx:
  161. pll->hw = &dss_omap5_hdmi_pll_hw;
  162. break;
  163. default:
  164. return -ENODEV;
  165. }
  166. pll->ops = &dsi_pll_ops;
  167. r = dss_pll_register(pll);
  168. if (r)
  169. return r;
  170. return 0;
  171. }
  172. int hdmi_pll_init(struct platform_device *pdev, struct hdmi_pll_data *pll,
  173. struct hdmi_wp_data *wp)
  174. {
  175. int r;
  176. struct resource *res;
  177. pll->wp = wp;
  178. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pll");
  179. if (!res) {
  180. DSSERR("can't get PLL mem resource\n");
  181. return -EINVAL;
  182. }
  183. pll->base = devm_ioremap_resource(&pdev->dev, res);
  184. if (IS_ERR(pll->base)) {
  185. DSSERR("can't ioremap PLLCTRL\n");
  186. return PTR_ERR(pll->base);
  187. }
  188. r = dsi_init_pll_data(pdev, pll);
  189. if (r) {
  190. DSSERR("failed to init HDMI PLL\n");
  191. return r;
  192. }
  193. return 0;
  194. }
  195. void hdmi_pll_uninit(struct hdmi_pll_data *hpll)
  196. {
  197. struct dss_pll *pll = &hpll->pll;
  198. dss_pll_unregister(pll);
  199. }