ccu_mult.c 3.4 KB

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