video-pll.c 4.5 KB

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