ccu_div.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2016 Maxime Ripard
  4. * Maxime Ripard <[email protected]>
  5. */
  6. #include <linux/clk-provider.h>
  7. #include <linux/io.h>
  8. #include "ccu_gate.h"
  9. #include "ccu_div.h"
  10. static unsigned long ccu_div_round_rate(struct ccu_mux_internal *mux,
  11. struct clk_hw *parent,
  12. unsigned long *parent_rate,
  13. unsigned long rate,
  14. void *data)
  15. {
  16. struct ccu_div *cd = data;
  17. if (cd->common.features & CCU_FEATURE_FIXED_POSTDIV)
  18. rate *= cd->fixed_post_div;
  19. rate = divider_round_rate_parent(&cd->common.hw, parent,
  20. rate, parent_rate,
  21. cd->div.table, cd->div.width,
  22. cd->div.flags);
  23. if (cd->common.features & CCU_FEATURE_FIXED_POSTDIV)
  24. rate /= cd->fixed_post_div;
  25. return rate;
  26. }
  27. static void ccu_div_disable(struct clk_hw *hw)
  28. {
  29. struct ccu_div *cd = hw_to_ccu_div(hw);
  30. return ccu_gate_helper_disable(&cd->common, cd->enable);
  31. }
  32. static int ccu_div_enable(struct clk_hw *hw)
  33. {
  34. struct ccu_div *cd = hw_to_ccu_div(hw);
  35. return ccu_gate_helper_enable(&cd->common, cd->enable);
  36. }
  37. static int ccu_div_is_enabled(struct clk_hw *hw)
  38. {
  39. struct ccu_div *cd = hw_to_ccu_div(hw);
  40. return ccu_gate_helper_is_enabled(&cd->common, cd->enable);
  41. }
  42. static unsigned long ccu_div_recalc_rate(struct clk_hw *hw,
  43. unsigned long parent_rate)
  44. {
  45. struct ccu_div *cd = hw_to_ccu_div(hw);
  46. unsigned long val;
  47. u32 reg;
  48. reg = readl(cd->common.base + cd->common.reg);
  49. val = reg >> cd->div.shift;
  50. val &= (1 << cd->div.width) - 1;
  51. parent_rate = ccu_mux_helper_apply_prediv(&cd->common, &cd->mux, -1,
  52. parent_rate);
  53. val = divider_recalc_rate(hw, parent_rate, val, cd->div.table,
  54. cd->div.flags, cd->div.width);
  55. if (cd->common.features & CCU_FEATURE_FIXED_POSTDIV)
  56. val /= cd->fixed_post_div;
  57. return val;
  58. }
  59. static int ccu_div_determine_rate(struct clk_hw *hw,
  60. struct clk_rate_request *req)
  61. {
  62. struct ccu_div *cd = hw_to_ccu_div(hw);
  63. return ccu_mux_helper_determine_rate(&cd->common, &cd->mux,
  64. req, ccu_div_round_rate, cd);
  65. }
  66. static int ccu_div_set_rate(struct clk_hw *hw, unsigned long rate,
  67. unsigned long parent_rate)
  68. {
  69. struct ccu_div *cd = hw_to_ccu_div(hw);
  70. unsigned long flags;
  71. unsigned long val;
  72. u32 reg;
  73. parent_rate = ccu_mux_helper_apply_prediv(&cd->common, &cd->mux, -1,
  74. parent_rate);
  75. if (cd->common.features & CCU_FEATURE_FIXED_POSTDIV)
  76. rate *= cd->fixed_post_div;
  77. val = divider_get_val(rate, parent_rate, cd->div.table, cd->div.width,
  78. cd->div.flags);
  79. spin_lock_irqsave(cd->common.lock, flags);
  80. reg = readl(cd->common.base + cd->common.reg);
  81. reg &= ~GENMASK(cd->div.width + cd->div.shift - 1, cd->div.shift);
  82. writel(reg | (val << cd->div.shift),
  83. cd->common.base + cd->common.reg);
  84. spin_unlock_irqrestore(cd->common.lock, flags);
  85. return 0;
  86. }
  87. static u8 ccu_div_get_parent(struct clk_hw *hw)
  88. {
  89. struct ccu_div *cd = hw_to_ccu_div(hw);
  90. return ccu_mux_helper_get_parent(&cd->common, &cd->mux);
  91. }
  92. static int ccu_div_set_parent(struct clk_hw *hw, u8 index)
  93. {
  94. struct ccu_div *cd = hw_to_ccu_div(hw);
  95. return ccu_mux_helper_set_parent(&cd->common, &cd->mux, index);
  96. }
  97. const struct clk_ops ccu_div_ops = {
  98. .disable = ccu_div_disable,
  99. .enable = ccu_div_enable,
  100. .is_enabled = ccu_div_is_enabled,
  101. .get_parent = ccu_div_get_parent,
  102. .set_parent = ccu_div_set_parent,
  103. .determine_rate = ccu_div_determine_rate,
  104. .recalc_rate = ccu_div_recalc_rate,
  105. .set_rate = ccu_div_set_rate,
  106. };
  107. EXPORT_SYMBOL_NS_GPL(ccu_div_ops, SUNXI_CCU);