clk-pfd.c 3.1 KB

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