ccu_div.c 3.6 KB

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