clk-factors.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (C) 2013 Emilio López <emilio@elopez.com.ar>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Adjustable factor-based clock implementation
  9. */
  10. #include <linux/clk-provider.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/io.h>
  14. #include <linux/err.h>
  15. #include <linux/string.h>
  16. #include <linux/delay.h>
  17. #include "clk-factors.h"
  18. /*
  19. * DOC: basic adjustable factor-based clock that cannot gate
  20. *
  21. * Traits of this clock:
  22. * prepare - clk_prepare only ensures that parents are prepared
  23. * enable - clk_enable only ensures that parents are enabled
  24. * rate - rate is adjustable.
  25. * clk->rate = (parent->rate * N * (K + 1) >> P) / (M + 1)
  26. * parent - fixed parent. No clk_set_parent support
  27. */
  28. struct clk_factors {
  29. struct clk_hw hw;
  30. void __iomem *reg;
  31. struct clk_factors_config *config;
  32. void (*get_factors) (u32 *rate, u32 parent, u8 *n, u8 *k, u8 *m, u8 *p);
  33. spinlock_t *lock;
  34. };
  35. #define to_clk_factors(_hw) container_of(_hw, struct clk_factors, hw)
  36. #define SETMASK(len, pos) (((-1U) >> (31-len)) << (pos))
  37. #define CLRMASK(len, pos) (~(SETMASK(len, pos)))
  38. #define FACTOR_GET(bit, len, reg) (((reg) & SETMASK(len, bit)) >> (bit))
  39. #define FACTOR_SET(bit, len, reg, val) \
  40. (((reg) & CLRMASK(len, bit)) | (val << (bit)))
  41. static unsigned long clk_factors_recalc_rate(struct clk_hw *hw,
  42. unsigned long parent_rate)
  43. {
  44. u8 n = 1, k = 0, p = 0, m = 0;
  45. u32 reg;
  46. unsigned long rate;
  47. struct clk_factors *factors = to_clk_factors(hw);
  48. struct clk_factors_config *config = factors->config;
  49. /* Fetch the register value */
  50. reg = readl(factors->reg);
  51. /* Get each individual factor if applicable */
  52. if (config->nwidth != SUNXI_FACTORS_NOT_APPLICABLE)
  53. n = FACTOR_GET(config->nshift, config->nwidth, reg);
  54. if (config->kwidth != SUNXI_FACTORS_NOT_APPLICABLE)
  55. k = FACTOR_GET(config->kshift, config->kwidth, reg);
  56. if (config->mwidth != SUNXI_FACTORS_NOT_APPLICABLE)
  57. m = FACTOR_GET(config->mshift, config->mwidth, reg);
  58. if (config->pwidth != SUNXI_FACTORS_NOT_APPLICABLE)
  59. p = FACTOR_GET(config->pshift, config->pwidth, reg);
  60. /* Calculate the rate */
  61. rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
  62. return rate;
  63. }
  64. static long clk_factors_round_rate(struct clk_hw *hw, unsigned long rate,
  65. unsigned long *parent_rate)
  66. {
  67. struct clk_factors *factors = to_clk_factors(hw);
  68. factors->get_factors((u32 *)&rate, (u32)*parent_rate,
  69. NULL, NULL, NULL, NULL);
  70. return rate;
  71. }
  72. static int clk_factors_set_rate(struct clk_hw *hw, unsigned long rate,
  73. unsigned long parent_rate)
  74. {
  75. u8 n, k, m, p;
  76. u32 reg;
  77. struct clk_factors *factors = to_clk_factors(hw);
  78. struct clk_factors_config *config = factors->config;
  79. unsigned long flags = 0;
  80. factors->get_factors((u32 *)&rate, (u32)parent_rate, &n, &k, &m, &p);
  81. if (factors->lock)
  82. spin_lock_irqsave(factors->lock, flags);
  83. /* Fetch the register value */
  84. reg = readl(factors->reg);
  85. /* Set up the new factors - macros do not do anything if width is 0 */
  86. reg = FACTOR_SET(config->nshift, config->nwidth, reg, n);
  87. reg = FACTOR_SET(config->kshift, config->kwidth, reg, k);
  88. reg = FACTOR_SET(config->mshift, config->mwidth, reg, m);
  89. reg = FACTOR_SET(config->pshift, config->pwidth, reg, p);
  90. /* Apply them now */
  91. writel(reg, factors->reg);
  92. /* delay 500us so pll stabilizes */
  93. __delay((rate >> 20) * 500 / 2);
  94. if (factors->lock)
  95. spin_unlock_irqrestore(factors->lock, flags);
  96. return 0;
  97. }
  98. static const struct clk_ops clk_factors_ops = {
  99. .recalc_rate = clk_factors_recalc_rate,
  100. .round_rate = clk_factors_round_rate,
  101. .set_rate = clk_factors_set_rate,
  102. };
  103. /**
  104. * clk_register_factors - register a factors clock with
  105. * the clock framework
  106. * @dev: device registering this clock
  107. * @name: name of this clock
  108. * @parent_name: name of clock's parent
  109. * @flags: framework-specific flags
  110. * @reg: register address to adjust factors
  111. * @config: shift and width of factors n, k, m and p
  112. * @get_factors: function to calculate the factors for a given frequency
  113. * @lock: shared register lock for this clock
  114. */
  115. struct clk *clk_register_factors(struct device *dev, const char *name,
  116. const char *parent_name,
  117. unsigned long flags, void __iomem *reg,
  118. struct clk_factors_config *config,
  119. void (*get_factors)(u32 *rate, u32 parent,
  120. u8 *n, u8 *k, u8 *m, u8 *p),
  121. spinlock_t *lock)
  122. {
  123. struct clk_factors *factors;
  124. struct clk *clk;
  125. struct clk_init_data init;
  126. /* allocate the factors */
  127. factors = kzalloc(sizeof(struct clk_factors), GFP_KERNEL);
  128. if (!factors) {
  129. pr_err("%s: could not allocate factors clk\n", __func__);
  130. return ERR_PTR(-ENOMEM);
  131. }
  132. init.name = name;
  133. init.ops = &clk_factors_ops;
  134. init.flags = flags;
  135. init.parent_names = (parent_name ? &parent_name : NULL);
  136. init.num_parents = (parent_name ? 1 : 0);
  137. /* struct clk_factors assignments */
  138. factors->reg = reg;
  139. factors->config = config;
  140. factors->lock = lock;
  141. factors->hw.init = &init;
  142. factors->get_factors = get_factors;
  143. /* register the clock */
  144. clk = clk_register(dev, &factors->hw);
  145. if (IS_ERR(clk))
  146. kfree(factors);
  147. return clk;
  148. }