mdp4_lvds_pll.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (C) 2014 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/clk.h>
  18. #include <linux/clk-provider.h>
  19. #include "mdp4_kms.h"
  20. struct mdp4_lvds_pll {
  21. struct clk_hw pll_hw;
  22. struct drm_device *dev;
  23. unsigned long pixclk;
  24. };
  25. #define to_mdp4_lvds_pll(x) container_of(x, struct mdp4_lvds_pll, pll_hw)
  26. static struct mdp4_kms *get_kms(struct mdp4_lvds_pll *lvds_pll)
  27. {
  28. struct msm_drm_private *priv = lvds_pll->dev->dev_private;
  29. return to_mdp4_kms(to_mdp_kms(priv->kms));
  30. }
  31. struct pll_rate {
  32. unsigned long rate;
  33. struct {
  34. uint32_t val;
  35. uint32_t reg;
  36. } conf[32];
  37. };
  38. /* NOTE: keep sorted highest freq to lowest: */
  39. static const struct pll_rate freqtbl[] = {
  40. { 72000000, {
  41. { 0x8f, REG_MDP4_LVDS_PHY_PLL_CTRL_1 },
  42. { 0x30, REG_MDP4_LVDS_PHY_PLL_CTRL_2 },
  43. { 0xc6, REG_MDP4_LVDS_PHY_PLL_CTRL_3 },
  44. { 0x10, REG_MDP4_LVDS_PHY_PLL_CTRL_5 },
  45. { 0x07, REG_MDP4_LVDS_PHY_PLL_CTRL_6 },
  46. { 0x62, REG_MDP4_LVDS_PHY_PLL_CTRL_7 },
  47. { 0x41, REG_MDP4_LVDS_PHY_PLL_CTRL_8 },
  48. { 0x0d, REG_MDP4_LVDS_PHY_PLL_CTRL_9 },
  49. { 0, 0 } }
  50. },
  51. };
  52. static const struct pll_rate *find_rate(unsigned long rate)
  53. {
  54. int i;
  55. for (i = 1; i < ARRAY_SIZE(freqtbl); i++)
  56. if (rate > freqtbl[i].rate)
  57. return &freqtbl[i-1];
  58. return &freqtbl[i-1];
  59. }
  60. static int mpd4_lvds_pll_enable(struct clk_hw *hw)
  61. {
  62. struct mdp4_lvds_pll *lvds_pll = to_mdp4_lvds_pll(hw);
  63. struct mdp4_kms *mdp4_kms = get_kms(lvds_pll);
  64. const struct pll_rate *pll_rate = find_rate(lvds_pll->pixclk);
  65. int i;
  66. DBG("pixclk=%lu (%lu)", lvds_pll->pixclk, pll_rate->rate);
  67. if (WARN_ON(!pll_rate))
  68. return -EINVAL;
  69. mdp4_write(mdp4_kms, REG_MDP4_LCDC_LVDS_PHY_RESET, 0x33);
  70. for (i = 0; pll_rate->conf[i].reg; i++)
  71. mdp4_write(mdp4_kms, pll_rate->conf[i].reg, pll_rate->conf[i].val);
  72. mdp4_write(mdp4_kms, REG_MDP4_LVDS_PHY_PLL_CTRL_0, 0x01);
  73. /* Wait until LVDS PLL is locked and ready */
  74. while (!mdp4_read(mdp4_kms, REG_MDP4_LVDS_PHY_PLL_LOCKED))
  75. cpu_relax();
  76. return 0;
  77. }
  78. static void mpd4_lvds_pll_disable(struct clk_hw *hw)
  79. {
  80. struct mdp4_lvds_pll *lvds_pll = to_mdp4_lvds_pll(hw);
  81. struct mdp4_kms *mdp4_kms = get_kms(lvds_pll);
  82. DBG("");
  83. mdp4_write(mdp4_kms, REG_MDP4_LVDS_PHY_CFG0, 0x0);
  84. mdp4_write(mdp4_kms, REG_MDP4_LVDS_PHY_PLL_CTRL_0, 0x0);
  85. }
  86. static unsigned long mpd4_lvds_pll_recalc_rate(struct clk_hw *hw,
  87. unsigned long parent_rate)
  88. {
  89. struct mdp4_lvds_pll *lvds_pll = to_mdp4_lvds_pll(hw);
  90. return lvds_pll->pixclk;
  91. }
  92. static long mpd4_lvds_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  93. unsigned long *parent_rate)
  94. {
  95. const struct pll_rate *pll_rate = find_rate(rate);
  96. return pll_rate->rate;
  97. }
  98. static int mpd4_lvds_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  99. unsigned long parent_rate)
  100. {
  101. struct mdp4_lvds_pll *lvds_pll = to_mdp4_lvds_pll(hw);
  102. lvds_pll->pixclk = rate;
  103. return 0;
  104. }
  105. static const struct clk_ops mpd4_lvds_pll_ops = {
  106. .enable = mpd4_lvds_pll_enable,
  107. .disable = mpd4_lvds_pll_disable,
  108. .recalc_rate = mpd4_lvds_pll_recalc_rate,
  109. .round_rate = mpd4_lvds_pll_round_rate,
  110. .set_rate = mpd4_lvds_pll_set_rate,
  111. };
  112. static const char *mpd4_lvds_pll_parents[] = {
  113. "pxo",
  114. };
  115. static struct clk_init_data pll_init = {
  116. .name = "mpd4_lvds_pll",
  117. .ops = &mpd4_lvds_pll_ops,
  118. .parent_names = mpd4_lvds_pll_parents,
  119. .num_parents = ARRAY_SIZE(mpd4_lvds_pll_parents),
  120. };
  121. struct clk *mpd4_lvds_pll_init(struct drm_device *dev)
  122. {
  123. struct mdp4_lvds_pll *lvds_pll;
  124. struct clk *clk;
  125. int ret;
  126. lvds_pll = devm_kzalloc(dev->dev, sizeof(*lvds_pll), GFP_KERNEL);
  127. if (!lvds_pll) {
  128. ret = -ENOMEM;
  129. goto fail;
  130. }
  131. lvds_pll->dev = dev;
  132. lvds_pll->pll_hw.init = &pll_init;
  133. clk = devm_clk_register(dev->dev, &lvds_pll->pll_hw);
  134. if (IS_ERR(clk)) {
  135. ret = PTR_ERR(clk);
  136. goto fail;
  137. }
  138. return clk;
  139. fail:
  140. return ERR_PTR(ret);
  141. }