clk-pll.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright 2014 Linaro Ltd.
  3. * Copyright (C) 2014 ZTE Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/clk-provider.h>
  10. #include <linux/err.h>
  11. #include <linux/io.h>
  12. #include <linux/iopoll.h>
  13. #include <linux/slab.h>
  14. #include <linux/spinlock.h>
  15. #include "clk.h"
  16. #define to_clk_zx_pll(_hw) container_of(_hw, struct clk_zx_pll, hw)
  17. #define CFG0_CFG1_OFFSET 4
  18. #define LOCK_FLAG BIT(30)
  19. #define POWER_DOWN BIT(31)
  20. static int rate_to_idx(struct clk_zx_pll *zx_pll, unsigned long rate)
  21. {
  22. const struct zx_pll_config *config = zx_pll->lookup_table;
  23. int i;
  24. for (i = 0; i < zx_pll->count; i++) {
  25. if (config[i].rate > rate)
  26. return i > 0 ? i - 1 : 0;
  27. if (config[i].rate == rate)
  28. return i;
  29. }
  30. return i - 1;
  31. }
  32. static int hw_to_idx(struct clk_zx_pll *zx_pll)
  33. {
  34. const struct zx_pll_config *config = zx_pll->lookup_table;
  35. u32 hw_cfg0, hw_cfg1;
  36. int i;
  37. hw_cfg0 = readl_relaxed(zx_pll->reg_base);
  38. hw_cfg1 = readl_relaxed(zx_pll->reg_base + CFG0_CFG1_OFFSET);
  39. /* For matching the value in lookup table */
  40. hw_cfg0 &= ~LOCK_FLAG;
  41. hw_cfg0 |= POWER_DOWN;
  42. for (i = 0; i < zx_pll->count; i++) {
  43. if (hw_cfg0 == config[i].cfg0 && hw_cfg1 == config[i].cfg1)
  44. return i;
  45. }
  46. return -EINVAL;
  47. }
  48. static unsigned long zx_pll_recalc_rate(struct clk_hw *hw,
  49. unsigned long parent_rate)
  50. {
  51. struct clk_zx_pll *zx_pll = to_clk_zx_pll(hw);
  52. int idx;
  53. idx = hw_to_idx(zx_pll);
  54. if (unlikely(idx == -EINVAL))
  55. return 0;
  56. return zx_pll->lookup_table[idx].rate;
  57. }
  58. static long zx_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  59. unsigned long *prate)
  60. {
  61. struct clk_zx_pll *zx_pll = to_clk_zx_pll(hw);
  62. int idx;
  63. idx = rate_to_idx(zx_pll, rate);
  64. return zx_pll->lookup_table[idx].rate;
  65. }
  66. static int zx_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  67. unsigned long parent_rate)
  68. {
  69. /* Assume current cpu is not running on current PLL */
  70. struct clk_zx_pll *zx_pll = to_clk_zx_pll(hw);
  71. const struct zx_pll_config *config;
  72. int idx;
  73. idx = rate_to_idx(zx_pll, rate);
  74. config = &zx_pll->lookup_table[idx];
  75. writel_relaxed(config->cfg0, zx_pll->reg_base);
  76. writel_relaxed(config->cfg1, zx_pll->reg_base + CFG0_CFG1_OFFSET);
  77. return 0;
  78. }
  79. static int zx_pll_enable(struct clk_hw *hw)
  80. {
  81. struct clk_zx_pll *zx_pll = to_clk_zx_pll(hw);
  82. u32 reg;
  83. reg = readl_relaxed(zx_pll->reg_base);
  84. writel_relaxed(reg & ~POWER_DOWN, zx_pll->reg_base);
  85. return readl_relaxed_poll_timeout(zx_pll->reg_base, reg,
  86. reg & LOCK_FLAG, 0, 100);
  87. }
  88. static void zx_pll_disable(struct clk_hw *hw)
  89. {
  90. struct clk_zx_pll *zx_pll = to_clk_zx_pll(hw);
  91. u32 reg;
  92. reg = readl_relaxed(zx_pll->reg_base);
  93. writel_relaxed(reg | POWER_DOWN, zx_pll->reg_base);
  94. }
  95. static int zx_pll_is_enabled(struct clk_hw *hw)
  96. {
  97. struct clk_zx_pll *zx_pll = to_clk_zx_pll(hw);
  98. u32 reg;
  99. reg = readl_relaxed(zx_pll->reg_base);
  100. return !(reg & POWER_DOWN);
  101. }
  102. static const struct clk_ops zx_pll_ops = {
  103. .recalc_rate = zx_pll_recalc_rate,
  104. .round_rate = zx_pll_round_rate,
  105. .set_rate = zx_pll_set_rate,
  106. .enable = zx_pll_enable,
  107. .disable = zx_pll_disable,
  108. .is_enabled = zx_pll_is_enabled,
  109. };
  110. struct clk *clk_register_zx_pll(const char *name, const char *parent_name,
  111. unsigned long flags, void __iomem *reg_base,
  112. const struct zx_pll_config *lookup_table, int count, spinlock_t *lock)
  113. {
  114. struct clk_zx_pll *zx_pll;
  115. struct clk *clk;
  116. struct clk_init_data init;
  117. zx_pll = kzalloc(sizeof(*zx_pll), GFP_KERNEL);
  118. if (!zx_pll)
  119. return ERR_PTR(-ENOMEM);
  120. init.name = name;
  121. init.ops = &zx_pll_ops;
  122. init.flags = flags;
  123. init.parent_names = parent_name ? &parent_name : NULL;
  124. init.num_parents = parent_name ? 1 : 0;
  125. zx_pll->reg_base = reg_base;
  126. zx_pll->lookup_table = lookup_table;
  127. zx_pll->count = count;
  128. zx_pll->lock = lock;
  129. zx_pll->hw.init = &init;
  130. clk = clk_register(NULL, &zx_pll->hw);
  131. if (IS_ERR(clk))
  132. kfree(zx_pll);
  133. return clk;
  134. }