video-pll.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (C) 2014 Texas Instruments Ltd
  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. * You should have received a copy of the GNU General Public License along with
  9. * this program. If not, see <http://www.gnu.org/licenses/>.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/err.h>
  14. #include <linux/io.h>
  15. #include <linux/kernel.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/sched.h>
  18. #include "omapdss.h"
  19. #include "dss.h"
  20. #include "dss_features.h"
  21. struct dss_video_pll {
  22. struct dss_pll pll;
  23. struct device *dev;
  24. void __iomem *clkctrl_base;
  25. };
  26. #define REG_MOD(reg, val, start, end) \
  27. writel_relaxed(FLD_MOD(readl_relaxed(reg), val, start, end), reg)
  28. static void dss_dpll_enable_scp_clk(struct dss_video_pll *vpll)
  29. {
  30. REG_MOD(vpll->clkctrl_base, 1, 14, 14); /* CIO_CLK_ICG */
  31. }
  32. static void dss_dpll_disable_scp_clk(struct dss_video_pll *vpll)
  33. {
  34. REG_MOD(vpll->clkctrl_base, 0, 14, 14); /* CIO_CLK_ICG */
  35. }
  36. static void dss_dpll_power_enable(struct dss_video_pll *vpll)
  37. {
  38. REG_MOD(vpll->clkctrl_base, 2, 31, 30); /* PLL_POWER_ON_ALL */
  39. /*
  40. * DRA7x PLL CTRL's PLL_PWR_STATUS seems to always return 0,
  41. * so we have to use fixed delay here.
  42. */
  43. msleep(1);
  44. }
  45. static void dss_dpll_power_disable(struct dss_video_pll *vpll)
  46. {
  47. REG_MOD(vpll->clkctrl_base, 0, 31, 30); /* PLL_POWER_OFF */
  48. }
  49. static int dss_video_pll_enable(struct dss_pll *pll)
  50. {
  51. struct dss_video_pll *vpll = container_of(pll, struct dss_video_pll, pll);
  52. int r;
  53. r = dss_runtime_get();
  54. if (r)
  55. return r;
  56. dss_ctrl_pll_enable(pll->id, true);
  57. dss_dpll_enable_scp_clk(vpll);
  58. r = dss_pll_wait_reset_done(pll);
  59. if (r)
  60. goto err_reset;
  61. dss_dpll_power_enable(vpll);
  62. return 0;
  63. err_reset:
  64. dss_dpll_disable_scp_clk(vpll);
  65. dss_ctrl_pll_enable(pll->id, false);
  66. dss_runtime_put();
  67. return r;
  68. }
  69. static void dss_video_pll_disable(struct dss_pll *pll)
  70. {
  71. struct dss_video_pll *vpll = container_of(pll, struct dss_video_pll, pll);
  72. dss_dpll_power_disable(vpll);
  73. dss_dpll_disable_scp_clk(vpll);
  74. dss_ctrl_pll_enable(pll->id, false);
  75. dss_runtime_put();
  76. }
  77. static const struct dss_pll_ops dss_pll_ops = {
  78. .enable = dss_video_pll_enable,
  79. .disable = dss_video_pll_disable,
  80. .set_config = dss_pll_write_config_type_a,
  81. };
  82. static const struct dss_pll_hw dss_dra7_video_pll_hw = {
  83. .type = DSS_PLL_TYPE_A,
  84. .n_max = (1 << 8) - 1,
  85. .m_max = (1 << 12) - 1,
  86. .mX_max = (1 << 5) - 1,
  87. .fint_min = 500000,
  88. .fint_max = 2500000,
  89. .clkdco_max = 1800000000,
  90. .n_msb = 8,
  91. .n_lsb = 1,
  92. .m_msb = 20,
  93. .m_lsb = 9,
  94. .mX_msb[0] = 25,
  95. .mX_lsb[0] = 21,
  96. .mX_msb[1] = 30,
  97. .mX_lsb[1] = 26,
  98. .mX_msb[2] = 4,
  99. .mX_lsb[2] = 0,
  100. .mX_msb[3] = 9,
  101. .mX_lsb[3] = 5,
  102. .has_refsel = true,
  103. };
  104. struct dss_pll *dss_video_pll_init(struct platform_device *pdev, int id,
  105. struct regulator *regulator)
  106. {
  107. const char * const reg_name[] = { "pll1", "pll2" };
  108. const char * const clkctrl_name[] = { "pll1_clkctrl", "pll2_clkctrl" };
  109. const char * const clkin_name[] = { "video1_clk", "video2_clk" };
  110. struct resource *res;
  111. struct dss_video_pll *vpll;
  112. void __iomem *pll_base, *clkctrl_base;
  113. struct clk *clk;
  114. struct dss_pll *pll;
  115. int r;
  116. /* PLL CONTROL */
  117. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, reg_name[id]);
  118. if (!res) {
  119. dev_err(&pdev->dev,
  120. "missing platform resource data for pll%d\n", id);
  121. return ERR_PTR(-ENODEV);
  122. }
  123. pll_base = devm_ioremap_resource(&pdev->dev, res);
  124. if (IS_ERR(pll_base)) {
  125. dev_err(&pdev->dev, "failed to ioremap pll%d reg_name\n", id);
  126. return ERR_CAST(pll_base);
  127. }
  128. /* CLOCK CONTROL */
  129. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  130. clkctrl_name[id]);
  131. if (!res) {
  132. dev_err(&pdev->dev,
  133. "missing platform resource data for pll%d\n", id);
  134. return ERR_PTR(-ENODEV);
  135. }
  136. clkctrl_base = devm_ioremap_resource(&pdev->dev, res);
  137. if (IS_ERR(clkctrl_base)) {
  138. dev_err(&pdev->dev, "failed to ioremap pll%d clkctrl\n", id);
  139. return ERR_CAST(clkctrl_base);
  140. }
  141. /* CLKIN */
  142. clk = devm_clk_get(&pdev->dev, clkin_name[id]);
  143. if (IS_ERR(clk)) {
  144. DSSERR("can't get video pll clkin\n");
  145. return ERR_CAST(clk);
  146. }
  147. vpll = devm_kzalloc(&pdev->dev, sizeof(*vpll), GFP_KERNEL);
  148. if (!vpll)
  149. return ERR_PTR(-ENOMEM);
  150. vpll->dev = &pdev->dev;
  151. vpll->clkctrl_base = clkctrl_base;
  152. pll = &vpll->pll;
  153. pll->name = id == 0 ? "video0" : "video1";
  154. pll->id = id == 0 ? DSS_PLL_VIDEO1 : DSS_PLL_VIDEO2;
  155. pll->clkin = clk;
  156. pll->regulator = regulator;
  157. pll->base = pll_base;
  158. pll->hw = &dss_dra7_video_pll_hw;
  159. pll->ops = &dss_pll_ops;
  160. r = dss_pll_register(pll);
  161. if (r)
  162. return ERR_PTR(r);
  163. return pll;
  164. }
  165. void dss_video_pll_uninit(struct dss_pll *pll)
  166. {
  167. dss_pll_unregister(pll);
  168. }