ccu_mult.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. unsigned long parent_rate,
  29. unsigned long rate,
  30. void *data)
  31. {
  32. struct ccu_mult *cm = data;
  33. struct _ccu_mult _cm;
  34. _cm.min = cm->mult.min;
  35. if (cm->mult.max)
  36. _cm.max = cm->mult.max;
  37. else
  38. _cm.max = (1 << cm->mult.width) + cm->mult.offset - 1;
  39. ccu_mult_find_best(parent_rate, rate, &_cm);
  40. return parent_rate * _cm.mult;
  41. }
  42. static void ccu_mult_disable(struct clk_hw *hw)
  43. {
  44. struct ccu_mult *cm = hw_to_ccu_mult(hw);
  45. return ccu_gate_helper_disable(&cm->common, cm->enable);
  46. }
  47. static int ccu_mult_enable(struct clk_hw *hw)
  48. {
  49. struct ccu_mult *cm = hw_to_ccu_mult(hw);
  50. return ccu_gate_helper_enable(&cm->common, cm->enable);
  51. }
  52. static int ccu_mult_is_enabled(struct clk_hw *hw)
  53. {
  54. struct ccu_mult *cm = hw_to_ccu_mult(hw);
  55. return ccu_gate_helper_is_enabled(&cm->common, cm->enable);
  56. }
  57. static unsigned long ccu_mult_recalc_rate(struct clk_hw *hw,
  58. unsigned long parent_rate)
  59. {
  60. struct ccu_mult *cm = hw_to_ccu_mult(hw);
  61. unsigned long val;
  62. u32 reg;
  63. if (ccu_frac_helper_is_enabled(&cm->common, &cm->frac))
  64. return ccu_frac_helper_read_rate(&cm->common, &cm->frac);
  65. reg = readl(cm->common.base + cm->common.reg);
  66. val = reg >> cm->mult.shift;
  67. val &= (1 << cm->mult.width) - 1;
  68. ccu_mux_helper_adjust_parent_for_prediv(&cm->common, &cm->mux, -1,
  69. &parent_rate);
  70. return parent_rate * (val + cm->mult.offset);
  71. }
  72. static int ccu_mult_determine_rate(struct clk_hw *hw,
  73. struct clk_rate_request *req)
  74. {
  75. struct ccu_mult *cm = hw_to_ccu_mult(hw);
  76. return ccu_mux_helper_determine_rate(&cm->common, &cm->mux,
  77. req, ccu_mult_round_rate, cm);
  78. }
  79. static int ccu_mult_set_rate(struct clk_hw *hw, unsigned long rate,
  80. unsigned long parent_rate)
  81. {
  82. struct ccu_mult *cm = hw_to_ccu_mult(hw);
  83. struct _ccu_mult _cm;
  84. unsigned long flags;
  85. u32 reg;
  86. if (ccu_frac_helper_has_rate(&cm->common, &cm->frac, rate))
  87. return ccu_frac_helper_set_rate(&cm->common, &cm->frac, rate);
  88. else
  89. ccu_frac_helper_disable(&cm->common, &cm->frac);
  90. ccu_mux_helper_adjust_parent_for_prediv(&cm->common, &cm->mux, -1,
  91. &parent_rate);
  92. _cm.min = cm->mult.min;
  93. if (cm->mult.max)
  94. _cm.max = cm->mult.max;
  95. else
  96. _cm.max = (1 << cm->mult.width) + cm->mult.offset - 1;
  97. ccu_mult_find_best(parent_rate, rate, &_cm);
  98. spin_lock_irqsave(cm->common.lock, flags);
  99. reg = readl(cm->common.base + cm->common.reg);
  100. reg &= ~GENMASK(cm->mult.width + cm->mult.shift - 1, cm->mult.shift);
  101. reg |= ((_cm.mult - cm->mult.offset) << cm->mult.shift);
  102. writel(reg, cm->common.base + cm->common.reg);
  103. spin_unlock_irqrestore(cm->common.lock, flags);
  104. ccu_helper_wait_for_lock(&cm->common, cm->lock);
  105. return 0;
  106. }
  107. static u8 ccu_mult_get_parent(struct clk_hw *hw)
  108. {
  109. struct ccu_mult *cm = hw_to_ccu_mult(hw);
  110. return ccu_mux_helper_get_parent(&cm->common, &cm->mux);
  111. }
  112. static int ccu_mult_set_parent(struct clk_hw *hw, u8 index)
  113. {
  114. struct ccu_mult *cm = hw_to_ccu_mult(hw);
  115. return ccu_mux_helper_set_parent(&cm->common, &cm->mux, index);
  116. }
  117. const struct clk_ops ccu_mult_ops = {
  118. .disable = ccu_mult_disable,
  119. .enable = ccu_mult_enable,
  120. .is_enabled = ccu_mult_is_enabled,
  121. .get_parent = ccu_mult_get_parent,
  122. .set_parent = ccu_mult_set_parent,
  123. .determine_rate = ccu_mult_determine_rate,
  124. .recalc_rate = ccu_mult_recalc_rate,
  125. .set_rate = ccu_mult_set_rate,
  126. };