ccu_div.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. unsigned long parent_rate,
  15. unsigned long rate,
  16. void *data)
  17. {
  18. struct ccu_div *cd = data;
  19. unsigned long val;
  20. /*
  21. * We can't use divider_round_rate that assumes that there's
  22. * several parents, while we might be called to evaluate
  23. * several different parents.
  24. */
  25. val = divider_get_val(rate, parent_rate, cd->div.table, cd->div.width,
  26. cd->div.flags);
  27. return divider_recalc_rate(&cd->common.hw, parent_rate, val,
  28. cd->div.table, cd->div.flags);
  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. ccu_mux_helper_adjust_parent_for_prediv(&cd->common, &cd->mux, -1,
  55. &parent_rate);
  56. return divider_recalc_rate(hw, parent_rate, val, cd->div.table,
  57. cd->div.flags);
  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. ccu_mux_helper_adjust_parent_for_prediv(&cd->common, &cd->mux, -1,
  74. &parent_rate);
  75. val = divider_get_val(rate, parent_rate, cd->div.table, cd->div.width,
  76. cd->div.flags);
  77. spin_lock_irqsave(cd->common.lock, flags);
  78. reg = readl(cd->common.base + cd->common.reg);
  79. reg &= ~GENMASK(cd->div.width + cd->div.shift - 1, cd->div.shift);
  80. writel(reg | (val << cd->div.shift),
  81. cd->common.base + cd->common.reg);
  82. spin_unlock_irqrestore(cd->common.lock, flags);
  83. return 0;
  84. }
  85. static u8 ccu_div_get_parent(struct clk_hw *hw)
  86. {
  87. struct ccu_div *cd = hw_to_ccu_div(hw);
  88. return ccu_mux_helper_get_parent(&cd->common, &cd->mux);
  89. }
  90. static int ccu_div_set_parent(struct clk_hw *hw, u8 index)
  91. {
  92. struct ccu_div *cd = hw_to_ccu_div(hw);
  93. return ccu_mux_helper_set_parent(&cd->common, &cd->mux, index);
  94. }
  95. const struct clk_ops ccu_div_ops = {
  96. .disable = ccu_div_disable,
  97. .enable = ccu_div_enable,
  98. .is_enabled = ccu_div_is_enabled,
  99. .get_parent = ccu_div_get_parent,
  100. .set_parent = ccu_div_set_parent,
  101. .determine_rate = ccu_div_determine_rate,
  102. .recalc_rate = ccu_div_recalc_rate,
  103. .set_rate = ccu_div_set_rate,
  104. };