clk-pfd.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright 2012 Freescale Semiconductor, Inc.
  3. * Copyright 2012 Linaro Ltd.
  4. *
  5. * The code contained herein is licensed under the GNU General Public
  6. * License. You may obtain a copy of the GNU General Public License
  7. * Version 2 or later at the following locations:
  8. *
  9. * http://www.opensource.org/licenses/gpl-license.html
  10. * http://www.gnu.org/copyleft/gpl.html
  11. */
  12. #include <linux/clk-provider.h>
  13. #include <linux/io.h>
  14. #include <linux/slab.h>
  15. #include <linux/err.h>
  16. #include "clk.h"
  17. /**
  18. * struct clk_pfd - IMX PFD clock
  19. * @clk_hw: clock source
  20. * @reg: PFD register address
  21. * @idx: the index of PFD encoded in the register
  22. *
  23. * PFD clock found on i.MX6 series. Each register for PFD has 4 clk_pfd
  24. * data encoded, and member idx is used to specify the one. And each
  25. * register has SET, CLR and TOG registers at offset 0x4 0x8 and 0xc.
  26. */
  27. struct clk_pfd {
  28. struct clk_hw hw;
  29. void __iomem *reg;
  30. u8 idx;
  31. };
  32. #define to_clk_pfd(_hw) container_of(_hw, struct clk_pfd, hw)
  33. #define SET 0x4
  34. #define CLR 0x8
  35. #define OTG 0xc
  36. static int clk_pfd_enable(struct clk_hw *hw)
  37. {
  38. struct clk_pfd *pfd = to_clk_pfd(hw);
  39. writel_relaxed(1 << ((pfd->idx + 1) * 8 - 1), pfd->reg + CLR);
  40. return 0;
  41. }
  42. static void clk_pfd_disable(struct clk_hw *hw)
  43. {
  44. struct clk_pfd *pfd = to_clk_pfd(hw);
  45. writel_relaxed(1 << ((pfd->idx + 1) * 8 - 1), pfd->reg + SET);
  46. }
  47. static unsigned long clk_pfd_recalc_rate(struct clk_hw *hw,
  48. unsigned long parent_rate)
  49. {
  50. struct clk_pfd *pfd = to_clk_pfd(hw);
  51. u64 tmp = parent_rate;
  52. u8 frac = (readl_relaxed(pfd->reg) >> (pfd->idx * 8)) & 0x3f;
  53. tmp *= 18;
  54. do_div(tmp, frac);
  55. return tmp;
  56. }
  57. static long clk_pfd_round_rate(struct clk_hw *hw, unsigned long rate,
  58. unsigned long *prate)
  59. {
  60. u64 tmp = *prate;
  61. u8 frac;
  62. tmp = tmp * 18 + rate / 2;
  63. do_div(tmp, rate);
  64. frac = tmp;
  65. if (frac < 12)
  66. frac = 12;
  67. else if (frac > 35)
  68. frac = 35;
  69. tmp = *prate;
  70. tmp *= 18;
  71. do_div(tmp, frac);
  72. return tmp;
  73. }
  74. static int clk_pfd_set_rate(struct clk_hw *hw, unsigned long rate,
  75. unsigned long parent_rate)
  76. {
  77. struct clk_pfd *pfd = to_clk_pfd(hw);
  78. u64 tmp = parent_rate;
  79. u8 frac;
  80. tmp = tmp * 18 + rate / 2;
  81. do_div(tmp, rate);
  82. frac = tmp;
  83. if (frac < 12)
  84. frac = 12;
  85. else if (frac > 35)
  86. frac = 35;
  87. writel_relaxed(0x3f << (pfd->idx * 8), pfd->reg + CLR);
  88. writel_relaxed(frac << (pfd->idx * 8), pfd->reg + SET);
  89. return 0;
  90. }
  91. static int clk_pfd_is_enabled(struct clk_hw *hw)
  92. {
  93. struct clk_pfd *pfd = to_clk_pfd(hw);
  94. if (readl_relaxed(pfd->reg) & (1 << ((pfd->idx + 1) * 8 - 1)))
  95. return 0;
  96. return 1;
  97. }
  98. static const struct clk_ops clk_pfd_ops = {
  99. .enable = clk_pfd_enable,
  100. .disable = clk_pfd_disable,
  101. .recalc_rate = clk_pfd_recalc_rate,
  102. .round_rate = clk_pfd_round_rate,
  103. .set_rate = clk_pfd_set_rate,
  104. .is_enabled = clk_pfd_is_enabled,
  105. };
  106. struct clk *imx_clk_pfd(const char *name, const char *parent_name,
  107. void __iomem *reg, u8 idx)
  108. {
  109. struct clk_pfd *pfd;
  110. struct clk *clk;
  111. struct clk_init_data init;
  112. pfd = kzalloc(sizeof(*pfd), GFP_KERNEL);
  113. if (!pfd)
  114. return ERR_PTR(-ENOMEM);
  115. pfd->reg = reg;
  116. pfd->idx = idx;
  117. init.name = name;
  118. init.ops = &clk_pfd_ops;
  119. init.flags = 0;
  120. init.parent_names = &parent_name;
  121. init.num_parents = 1;
  122. pfd->hw.init = &init;
  123. clk = clk_register(NULL, &pfd->hw);
  124. if (IS_ERR(clk))
  125. kfree(pfd);
  126. return clk;
  127. }