ccu_mult.c 3.5 KB

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