clk-factors.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #define to_clk_factors(_hw) container_of(_hw, struct clk_factors, hw)
  29. #define SETMASK(len, pos) (((1U << (len)) - 1) << (pos))
  30. #define CLRMASK(len, pos) (~(SETMASK(len, pos)))
  31. #define FACTOR_GET(bit, len, reg) (((reg) & SETMASK(len, bit)) >> (bit))
  32. #define FACTOR_SET(bit, len, reg, val) \
  33. (((reg) & CLRMASK(len, bit)) | (val << (bit)))
  34. static unsigned long clk_factors_recalc_rate(struct clk_hw *hw,
  35. unsigned long parent_rate)
  36. {
  37. u8 n = 1, k = 0, p = 0, m = 0;
  38. u32 reg;
  39. unsigned long rate;
  40. struct clk_factors *factors = to_clk_factors(hw);
  41. struct clk_factors_config *config = factors->config;
  42. /* Fetch the register value */
  43. reg = readl(factors->reg);
  44. /* Get each individual factor if applicable */
  45. if (config->nwidth != SUNXI_FACTORS_NOT_APPLICABLE)
  46. n = FACTOR_GET(config->nshift, config->nwidth, reg);
  47. if (config->kwidth != SUNXI_FACTORS_NOT_APPLICABLE)
  48. k = FACTOR_GET(config->kshift, config->kwidth, reg);
  49. if (config->mwidth != SUNXI_FACTORS_NOT_APPLICABLE)
  50. m = FACTOR_GET(config->mshift, config->mwidth, reg);
  51. if (config->pwidth != SUNXI_FACTORS_NOT_APPLICABLE)
  52. p = FACTOR_GET(config->pshift, config->pwidth, reg);
  53. /* Calculate the rate */
  54. rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
  55. return rate;
  56. }
  57. static long clk_factors_round_rate(struct clk_hw *hw, unsigned long rate,
  58. unsigned long *parent_rate)
  59. {
  60. struct clk_factors *factors = to_clk_factors(hw);
  61. factors->get_factors((u32 *)&rate, (u32)*parent_rate,
  62. NULL, NULL, NULL, NULL);
  63. return rate;
  64. }
  65. static int clk_factors_set_rate(struct clk_hw *hw, unsigned long rate,
  66. unsigned long parent_rate)
  67. {
  68. u8 n = 0, k = 0, m = 0, p = 0;
  69. u32 reg;
  70. struct clk_factors *factors = to_clk_factors(hw);
  71. struct clk_factors_config *config = factors->config;
  72. unsigned long flags = 0;
  73. factors->get_factors((u32 *)&rate, (u32)parent_rate, &n, &k, &m, &p);
  74. if (factors->lock)
  75. spin_lock_irqsave(factors->lock, flags);
  76. /* Fetch the register value */
  77. reg = readl(factors->reg);
  78. /* Set up the new factors - macros do not do anything if width is 0 */
  79. reg = FACTOR_SET(config->nshift, config->nwidth, reg, n);
  80. reg = FACTOR_SET(config->kshift, config->kwidth, reg, k);
  81. reg = FACTOR_SET(config->mshift, config->mwidth, reg, m);
  82. reg = FACTOR_SET(config->pshift, config->pwidth, reg, p);
  83. /* Apply them now */
  84. writel(reg, factors->reg);
  85. /* delay 500us so pll stabilizes */
  86. __delay((rate >> 20) * 500 / 2);
  87. if (factors->lock)
  88. spin_unlock_irqrestore(factors->lock, flags);
  89. return 0;
  90. }
  91. const struct clk_ops clk_factors_ops = {
  92. .recalc_rate = clk_factors_recalc_rate,
  93. .round_rate = clk_factors_round_rate,
  94. .set_rate = clk_factors_set_rate,
  95. };