clk-icst.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. const 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. vco = vco_get(icst->vcoreg);
  77. icst->rate = icst_hz(icst->params, vco);
  78. return icst->rate;
  79. }
  80. static long icst_round_rate(struct clk_hw *hw, unsigned long rate,
  81. unsigned long *prate)
  82. {
  83. struct clk_icst *icst = to_icst(hw);
  84. struct icst_vco vco;
  85. vco = icst_hz_to_vco(icst->params, rate);
  86. return icst_hz(icst->params, vco);
  87. }
  88. static int icst_set_rate(struct clk_hw *hw, unsigned long rate,
  89. unsigned long parent_rate)
  90. {
  91. struct clk_icst *icst = to_icst(hw);
  92. struct icst_vco vco;
  93. vco = icst_hz_to_vco(icst->params, rate);
  94. icst->rate = icst_hz(icst->params, vco);
  95. vco_set(icst->vcoreg, icst->lockreg, vco);
  96. return 0;
  97. }
  98. static const struct clk_ops icst_ops = {
  99. .recalc_rate = icst_recalc_rate,
  100. .round_rate = icst_round_rate,
  101. .set_rate = icst_set_rate,
  102. };
  103. struct clk *icst_clk_register(struct device *dev,
  104. const struct clk_icst_desc *desc,
  105. void __iomem *base)
  106. {
  107. struct clk *clk;
  108. struct clk_icst *icst;
  109. struct clk_init_data init;
  110. icst = kzalloc(sizeof(struct clk_icst), GFP_KERNEL);
  111. if (!icst) {
  112. pr_err("could not allocate ICST clock!\n");
  113. return ERR_PTR(-ENOMEM);
  114. }
  115. init.name = "icst";
  116. init.ops = &icst_ops;
  117. init.flags = CLK_IS_ROOT;
  118. init.parent_names = NULL;
  119. init.num_parents = 0;
  120. icst->hw.init = &init;
  121. icst->params = desc->params;
  122. icst->vcoreg = base + desc->vco_offset;
  123. icst->lockreg = base + desc->lock_offset;
  124. clk = clk_register(dev, &icst->hw);
  125. if (IS_ERR(clk))
  126. kfree(icst);
  127. return clk;
  128. }