clk-icst.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Driver for the ICST307 VCO clock found in the ARM Reference designs.
  3. * We wrap the custom interface from <asm/hardware/icst.h> into the generic
  4. * clock framework.
  5. *
  6. * Copyright (C) 2012 Linus Walleij
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * TODO: when all ARM reference designs are migrated to generic clocks, the
  13. * ICST clock code from the ARM tree should probably be merged into this
  14. * file.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/clkdev.h>
  18. #include <linux/err.h>
  19. #include <linux/clk-provider.h>
  20. #include <linux/io.h>
  21. #include "clk-icst.h"
  22. /**
  23. * struct clk_icst - ICST VCO clock wrapper
  24. * @hw: corresponding clock hardware entry
  25. * @vcoreg: VCO register address
  26. * @lockreg: VCO lock register address
  27. * @params: parameters for this ICST instance
  28. * @rate: current rate
  29. */
  30. struct clk_icst {
  31. struct clk_hw hw;
  32. void __iomem *vcoreg;
  33. void __iomem *lockreg;
  34. struct icst_params *params;
  35. unsigned long rate;
  36. };
  37. #define to_icst(_hw) container_of(_hw, struct clk_icst, hw)
  38. /**
  39. * vco_get() - get ICST VCO settings from a certain register
  40. * @vcoreg: register containing the VCO settings
  41. */
  42. static struct icst_vco vco_get(void __iomem *vcoreg)
  43. {
  44. u32 val;
  45. struct icst_vco vco;
  46. val = readl(vcoreg);
  47. vco.v = val & 0x1ff;
  48. vco.r = (val >> 9) & 0x7f;
  49. vco.s = (val >> 16) & 03;
  50. return vco;
  51. }
  52. /**
  53. * vco_set() - commit changes to an ICST VCO
  54. * @locreg: register to poke to unlock the VCO for writing
  55. * @vcoreg: register containing the VCO settings
  56. * @vco: ICST VCO parameters to commit
  57. */
  58. static void vco_set(void __iomem *lockreg,
  59. void __iomem *vcoreg,
  60. struct icst_vco vco)
  61. {
  62. u32 val;
  63. val = readl(vcoreg) & ~0x7ffff;
  64. val |= vco.v | (vco.r << 9) | (vco.s << 16);
  65. /* This magic unlocks the VCO so it can be controlled */
  66. writel(0xa05f, lockreg);
  67. writel(val, vcoreg);
  68. /* This locks the VCO again */
  69. writel(0, lockreg);
  70. }
  71. static unsigned long icst_recalc_rate(struct clk_hw *hw,
  72. unsigned long parent_rate)
  73. {
  74. struct clk_icst *icst = to_icst(hw);
  75. struct icst_vco vco;
  76. if (parent_rate)
  77. icst->params->ref = parent_rate;
  78. vco = vco_get(icst->vcoreg);
  79. icst->rate = icst_hz(icst->params, vco);
  80. return icst->rate;
  81. }
  82. static long icst_round_rate(struct clk_hw *hw, unsigned long rate,
  83. unsigned long *prate)
  84. {
  85. struct clk_icst *icst = to_icst(hw);
  86. struct icst_vco vco;
  87. vco = icst_hz_to_vco(icst->params, rate);
  88. return icst_hz(icst->params, vco);
  89. }
  90. static int icst_set_rate(struct clk_hw *hw, unsigned long rate,
  91. unsigned long parent_rate)
  92. {
  93. struct clk_icst *icst = to_icst(hw);
  94. struct icst_vco vco;
  95. if (parent_rate)
  96. icst->params->ref = parent_rate;
  97. vco = icst_hz_to_vco(icst->params, rate);
  98. icst->rate = icst_hz(icst->params, vco);
  99. vco_set(icst->lockreg, icst->vcoreg, vco);
  100. return 0;
  101. }
  102. static const struct clk_ops icst_ops = {
  103. .recalc_rate = icst_recalc_rate,
  104. .round_rate = icst_round_rate,
  105. .set_rate = icst_set_rate,
  106. };
  107. struct clk *icst_clk_register(struct device *dev,
  108. const struct clk_icst_desc *desc,
  109. const char *name,
  110. const char *parent_name,
  111. void __iomem *base)
  112. {
  113. struct clk *clk;
  114. struct clk_icst *icst;
  115. struct clk_init_data init;
  116. struct icst_params *pclone;
  117. icst = kzalloc(sizeof(struct clk_icst), GFP_KERNEL);
  118. if (!icst) {
  119. pr_err("could not allocate ICST clock!\n");
  120. return ERR_PTR(-ENOMEM);
  121. }
  122. pclone = kmemdup(desc->params, sizeof(*pclone), GFP_KERNEL);
  123. if (!pclone) {
  124. kfree(icst);
  125. pr_err("could not clone ICST params\n");
  126. return ERR_PTR(-ENOMEM);
  127. }
  128. init.name = name;
  129. init.ops = &icst_ops;
  130. init.flags = CLK_IS_ROOT;
  131. init.parent_names = (parent_name ? &parent_name : NULL);
  132. init.num_parents = (parent_name ? 1 : 0);
  133. icst->hw.init = &init;
  134. icst->params = pclone;
  135. icst->vcoreg = base + desc->vco_offset;
  136. icst->lockreg = base + desc->lock_offset;
  137. clk = clk_register(dev, &icst->hw);
  138. if (IS_ERR(clk))
  139. kfree(icst);
  140. return clk;
  141. }
  142. EXPORT_SYMBOL_GPL(icst_clk_register);