clk-gpt-synth.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (C) 2012 ST Microelectronics
  3. * Viresh Kumar <vireshk@kernel.org>
  4. *
  5. * This file is licensed under the terms of the GNU General Public
  6. * License version 2. This program is licensed "as is" without any
  7. * warranty of any kind, whether express or implied.
  8. *
  9. * General Purpose Timer Synthesizer clock implementation
  10. */
  11. #define pr_fmt(fmt) "clk-gpt-synth: " fmt
  12. #include <linux/clk-provider.h>
  13. #include <linux/slab.h>
  14. #include <linux/io.h>
  15. #include <linux/err.h>
  16. #include "clk.h"
  17. #define GPT_MSCALE_MASK 0xFFF
  18. #define GPT_NSCALE_SHIFT 12
  19. #define GPT_NSCALE_MASK 0xF
  20. /*
  21. * DOC: General Purpose Timer Synthesizer clock
  22. *
  23. * Calculates gpt synth clk rate for different values of mscale and nscale
  24. *
  25. * Fout= Fin/((2 ^ (N+1)) * (M+1))
  26. */
  27. #define to_clk_gpt(_hw) container_of(_hw, struct clk_gpt, hw)
  28. static unsigned long gpt_calc_rate(struct clk_hw *hw, unsigned long prate,
  29. int index)
  30. {
  31. struct clk_gpt *gpt = to_clk_gpt(hw);
  32. struct gpt_rate_tbl *rtbl = gpt->rtbl;
  33. prate /= ((1 << (rtbl[index].nscale + 1)) * (rtbl[index].mscale + 1));
  34. return prate;
  35. }
  36. static long clk_gpt_round_rate(struct clk_hw *hw, unsigned long drate,
  37. unsigned long *prate)
  38. {
  39. struct clk_gpt *gpt = to_clk_gpt(hw);
  40. int unused;
  41. return clk_round_rate_index(hw, drate, *prate, gpt_calc_rate,
  42. gpt->rtbl_cnt, &unused);
  43. }
  44. static unsigned long clk_gpt_recalc_rate(struct clk_hw *hw,
  45. unsigned long parent_rate)
  46. {
  47. struct clk_gpt *gpt = to_clk_gpt(hw);
  48. unsigned long flags = 0;
  49. unsigned int div = 1, val;
  50. if (gpt->lock)
  51. spin_lock_irqsave(gpt->lock, flags);
  52. val = readl_relaxed(gpt->reg);
  53. if (gpt->lock)
  54. spin_unlock_irqrestore(gpt->lock, flags);
  55. div += val & GPT_MSCALE_MASK;
  56. div *= 1 << (((val >> GPT_NSCALE_SHIFT) & GPT_NSCALE_MASK) + 1);
  57. if (!div)
  58. return 0;
  59. return parent_rate / div;
  60. }
  61. /* Configures new clock rate of gpt */
  62. static int clk_gpt_set_rate(struct clk_hw *hw, unsigned long drate,
  63. unsigned long prate)
  64. {
  65. struct clk_gpt *gpt = to_clk_gpt(hw);
  66. struct gpt_rate_tbl *rtbl = gpt->rtbl;
  67. unsigned long flags = 0, val;
  68. int i;
  69. clk_round_rate_index(hw, drate, prate, gpt_calc_rate, gpt->rtbl_cnt,
  70. &i);
  71. if (gpt->lock)
  72. spin_lock_irqsave(gpt->lock, flags);
  73. val = readl(gpt->reg) & ~GPT_MSCALE_MASK;
  74. val &= ~(GPT_NSCALE_MASK << GPT_NSCALE_SHIFT);
  75. val |= rtbl[i].mscale & GPT_MSCALE_MASK;
  76. val |= (rtbl[i].nscale & GPT_NSCALE_MASK) << GPT_NSCALE_SHIFT;
  77. writel_relaxed(val, gpt->reg);
  78. if (gpt->lock)
  79. spin_unlock_irqrestore(gpt->lock, flags);
  80. return 0;
  81. }
  82. static struct clk_ops clk_gpt_ops = {
  83. .recalc_rate = clk_gpt_recalc_rate,
  84. .round_rate = clk_gpt_round_rate,
  85. .set_rate = clk_gpt_set_rate,
  86. };
  87. struct clk *clk_register_gpt(const char *name, const char *parent_name, unsigned
  88. long flags, void __iomem *reg, struct gpt_rate_tbl *rtbl, u8
  89. rtbl_cnt, spinlock_t *lock)
  90. {
  91. struct clk_init_data init;
  92. struct clk_gpt *gpt;
  93. struct clk *clk;
  94. if (!name || !parent_name || !reg || !rtbl || !rtbl_cnt) {
  95. pr_err("Invalid arguments passed");
  96. return ERR_PTR(-EINVAL);
  97. }
  98. gpt = kzalloc(sizeof(*gpt), GFP_KERNEL);
  99. if (!gpt) {
  100. pr_err("could not allocate gpt clk\n");
  101. return ERR_PTR(-ENOMEM);
  102. }
  103. /* struct clk_gpt assignments */
  104. gpt->reg = reg;
  105. gpt->rtbl = rtbl;
  106. gpt->rtbl_cnt = rtbl_cnt;
  107. gpt->lock = lock;
  108. gpt->hw.init = &init;
  109. init.name = name;
  110. init.ops = &clk_gpt_ops;
  111. init.flags = flags;
  112. init.parent_names = &parent_name;
  113. init.num_parents = 1;
  114. clk = clk_register(NULL, &gpt->hw);
  115. if (!IS_ERR_OR_NULL(clk))
  116. return clk;
  117. pr_err("clk register failed\n");
  118. kfree(gpt);
  119. return NULL;
  120. }