clk-icst.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/kernel.h>
  17. #include <linux/slab.h>
  18. #include <linux/export.h>
  19. #include <linux/err.h>
  20. #include <linux/clk-provider.h>
  21. #include <linux/io.h>
  22. #include "clk-icst.h"
  23. /**
  24. * struct clk_icst - ICST VCO clock wrapper
  25. * @hw: corresponding clock hardware entry
  26. * @vcoreg: VCO register address
  27. * @lockreg: VCO lock register address
  28. * @params: parameters for this ICST instance
  29. * @rate: current rate
  30. */
  31. struct clk_icst {
  32. struct clk_hw hw;
  33. void __iomem *vcoreg;
  34. void __iomem *lockreg;
  35. struct icst_params *params;
  36. unsigned long rate;
  37. };
  38. #define to_icst(_hw) container_of(_hw, struct clk_icst, hw)
  39. /**
  40. * vco_get() - get ICST VCO settings from a certain register
  41. * @vcoreg: register containing the VCO settings
  42. */
  43. static struct icst_vco vco_get(void __iomem *vcoreg)
  44. {
  45. u32 val;
  46. struct icst_vco vco;
  47. val = readl(vcoreg);
  48. vco.v = val & 0x1ff;
  49. vco.r = (val >> 9) & 0x7f;
  50. vco.s = (val >> 16) & 03;
  51. return vco;
  52. }
  53. /**
  54. * vco_set() - commit changes to an ICST VCO
  55. * @locreg: register to poke to unlock the VCO for writing
  56. * @vcoreg: register containing the VCO settings
  57. * @vco: ICST VCO parameters to commit
  58. */
  59. static void vco_set(void __iomem *lockreg,
  60. void __iomem *vcoreg,
  61. struct icst_vco vco)
  62. {
  63. u32 val;
  64. val = readl(vcoreg) & ~0x7ffff;
  65. val |= vco.v | (vco.r << 9) | (vco.s << 16);
  66. /* This magic unlocks the VCO so it can be controlled */
  67. writel(0xa05f, lockreg);
  68. writel(val, vcoreg);
  69. /* This locks the VCO again */
  70. writel(0, lockreg);
  71. }
  72. static unsigned long icst_recalc_rate(struct clk_hw *hw,
  73. unsigned long parent_rate)
  74. {
  75. struct clk_icst *icst = to_icst(hw);
  76. struct icst_vco vco;
  77. if (parent_rate)
  78. icst->params->ref = parent_rate;
  79. vco = vco_get(icst->vcoreg);
  80. icst->rate = icst_hz(icst->params, vco);
  81. return icst->rate;
  82. }
  83. static long icst_round_rate(struct clk_hw *hw, unsigned long rate,
  84. unsigned long *prate)
  85. {
  86. struct clk_icst *icst = to_icst(hw);
  87. struct icst_vco vco;
  88. vco = icst_hz_to_vco(icst->params, rate);
  89. return icst_hz(icst->params, vco);
  90. }
  91. static int icst_set_rate(struct clk_hw *hw, unsigned long rate,
  92. unsigned long parent_rate)
  93. {
  94. struct clk_icst *icst = to_icst(hw);
  95. struct icst_vco vco;
  96. if (parent_rate)
  97. icst->params->ref = parent_rate;
  98. vco = icst_hz_to_vco(icst->params, rate);
  99. icst->rate = icst_hz(icst->params, vco);
  100. vco_set(icst->lockreg, icst->vcoreg, vco);
  101. return 0;
  102. }
  103. static const struct clk_ops icst_ops = {
  104. .recalc_rate = icst_recalc_rate,
  105. .round_rate = icst_round_rate,
  106. .set_rate = icst_set_rate,
  107. };
  108. struct clk *icst_clk_register(struct device *dev,
  109. const struct clk_icst_desc *desc,
  110. const char *name,
  111. const char *parent_name,
  112. void __iomem *base)
  113. {
  114. struct clk *clk;
  115. struct clk_icst *icst;
  116. struct clk_init_data init;
  117. struct icst_params *pclone;
  118. icst = kzalloc(sizeof(struct clk_icst), GFP_KERNEL);
  119. if (!icst) {
  120. pr_err("could not allocate ICST clock!\n");
  121. return ERR_PTR(-ENOMEM);
  122. }
  123. pclone = kmemdup(desc->params, sizeof(*pclone), GFP_KERNEL);
  124. if (!pclone) {
  125. kfree(icst);
  126. pr_err("could not clone ICST params\n");
  127. return ERR_PTR(-ENOMEM);
  128. }
  129. init.name = name;
  130. init.ops = &icst_ops;
  131. init.flags = CLK_IS_ROOT;
  132. init.parent_names = (parent_name ? &parent_name : NULL);
  133. init.num_parents = (parent_name ? 1 : 0);
  134. icst->hw.init = &init;
  135. icst->params = pclone;
  136. icst->vcoreg = base + desc->vco_offset;
  137. icst->lockreg = base + desc->lock_offset;
  138. clk = clk_register(dev, &icst->hw);
  139. if (IS_ERR(clk)) {
  140. kfree(pclone);
  141. kfree(icst);
  142. }
  143. return clk;
  144. }
  145. EXPORT_SYMBOL_GPL(icst_clk_register);