ccu_mult.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (C) 2016 Maxime Ripard
  3. * Maxime Ripard <maxime.ripard@free-electrons.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. */
  10. #include <linux/clk-provider.h>
  11. #include "ccu_gate.h"
  12. #include "ccu_mult.h"
  13. struct _ccu_mult {
  14. unsigned long mult, min, max;
  15. };
  16. static void ccu_mult_find_best(unsigned long parent, unsigned long rate,
  17. struct _ccu_mult *mult)
  18. {
  19. int _mult;
  20. _mult = rate / parent;
  21. if (_mult < mult->min)
  22. _mult = mult->min;
  23. if (_mult > mult->max)
  24. _mult = mult->max;
  25. mult->mult = _mult;
  26. }
  27. static unsigned long ccu_mult_round_rate(struct ccu_mux_internal *mux,
  28. struct clk_hw *parent,
  29. unsigned long *parent_rate,
  30. unsigned long rate,
  31. void *data)
  32. {
  33. struct ccu_mult *cm = data;
  34. struct _ccu_mult _cm;
  35. _cm.min = cm->mult.min;
  36. if (cm->mult.max)
  37. _cm.max = cm->mult.max;
  38. else
  39. _cm.max = (1 << cm->mult.width) + cm->mult.offset - 1;
  40. ccu_mult_find_best(*parent_rate, rate, &_cm);
  41. return *parent_rate * _cm.mult;
  42. }
  43. static void ccu_mult_disable(struct clk_hw *hw)
  44. {
  45. struct ccu_mult *cm = hw_to_ccu_mult(hw);
  46. return ccu_gate_helper_disable(&cm->common, cm->enable);
  47. }
  48. static int ccu_mult_enable(struct clk_hw *hw)
  49. {
  50. struct ccu_mult *cm = hw_to_ccu_mult(hw);
  51. return ccu_gate_helper_enable(&cm->common, cm->enable);
  52. }
  53. static int ccu_mult_is_enabled(struct clk_hw *hw)
  54. {
  55. struct ccu_mult *cm = hw_to_ccu_mult(hw);
  56. return ccu_gate_helper_is_enabled(&cm->common, cm->enable);
  57. }
  58. static unsigned long ccu_mult_recalc_rate(struct clk_hw *hw,
  59. unsigned long parent_rate)
  60. {
  61. struct ccu_mult *cm = hw_to_ccu_mult(hw);
  62. unsigned long val;
  63. u32 reg;
  64. if (ccu_frac_helper_is_enabled(&cm->common, &cm->frac))
  65. return ccu_frac_helper_read_rate(&cm->common, &cm->frac);
  66. reg = readl(cm->common.base + cm->common.reg);
  67. val = reg >> cm->mult.shift;
  68. val &= (1 << cm->mult.width) - 1;
  69. parent_rate = ccu_mux_helper_apply_prediv(&cm->common, &cm->mux, -1,
  70. parent_rate);
  71. return parent_rate * (val + cm->mult.offset);
  72. }
  73. static int ccu_mult_determine_rate(struct clk_hw *hw,
  74. struct clk_rate_request *req)
  75. {
  76. struct ccu_mult *cm = hw_to_ccu_mult(hw);
  77. return ccu_mux_helper_determine_rate(&cm->common, &cm->mux,
  78. req, ccu_mult_round_rate, cm);
  79. }
  80. static int ccu_mult_set_rate(struct clk_hw *hw, unsigned long rate,
  81. unsigned long parent_rate)
  82. {
  83. struct ccu_mult *cm = hw_to_ccu_mult(hw);
  84. struct _ccu_mult _cm;
  85. unsigned long flags;
  86. u32 reg;
  87. if (ccu_frac_helper_has_rate(&cm->common, &cm->frac, rate)) {
  88. ccu_frac_helper_enable(&cm->common, &cm->frac);
  89. return ccu_frac_helper_set_rate(&cm->common, &cm->frac,
  90. rate, cm->lock);
  91. } else {
  92. ccu_frac_helper_disable(&cm->common, &cm->frac);
  93. }
  94. parent_rate = ccu_mux_helper_apply_prediv(&cm->common, &cm->mux, -1,
  95. parent_rate);
  96. _cm.min = cm->mult.min;
  97. if (cm->mult.max)
  98. _cm.max = cm->mult.max;
  99. else
  100. _cm.max = (1 << cm->mult.width) + cm->mult.offset - 1;
  101. ccu_mult_find_best(parent_rate, rate, &_cm);
  102. spin_lock_irqsave(cm->common.lock, flags);
  103. reg = readl(cm->common.base + cm->common.reg);
  104. reg &= ~GENMASK(cm->mult.width + cm->mult.shift - 1, cm->mult.shift);
  105. reg |= ((_cm.mult - cm->mult.offset) << cm->mult.shift);
  106. writel(reg, cm->common.base + cm->common.reg);
  107. spin_unlock_irqrestore(cm->common.lock, flags);
  108. ccu_helper_wait_for_lock(&cm->common, cm->lock);
  109. return 0;
  110. }
  111. static u8 ccu_mult_get_parent(struct clk_hw *hw)
  112. {
  113. struct ccu_mult *cm = hw_to_ccu_mult(hw);
  114. return ccu_mux_helper_get_parent(&cm->common, &cm->mux);
  115. }
  116. static int ccu_mult_set_parent(struct clk_hw *hw, u8 index)
  117. {
  118. struct ccu_mult *cm = hw_to_ccu_mult(hw);
  119. return ccu_mux_helper_set_parent(&cm->common, &cm->mux, index);
  120. }
  121. const struct clk_ops ccu_mult_ops = {
  122. .disable = ccu_mult_disable,
  123. .enable = ccu_mult_enable,
  124. .is_enabled = ccu_mult_is_enabled,
  125. .get_parent = ccu_mult_get_parent,
  126. .set_parent = ccu_mult_set_parent,
  127. .determine_rate = ccu_mult_determine_rate,
  128. .recalc_rate = ccu_mult_recalc_rate,
  129. .set_rate = ccu_mult_set_rate,
  130. };