ccu_mp.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_mp.h"
  13. static void ccu_mp_find_best(unsigned long parent, unsigned long rate,
  14. unsigned int max_m, unsigned int max_p,
  15. unsigned int *m, unsigned int *p)
  16. {
  17. unsigned long best_rate = 0;
  18. unsigned int best_m = 0, best_p = 0;
  19. unsigned int _m, _p;
  20. for (_p = 0; _p <= max_p; _p++) {
  21. for (_m = 1; _m <= max_m; _m++) {
  22. unsigned long tmp_rate = (parent >> _p) / _m;
  23. if (tmp_rate > rate)
  24. continue;
  25. if ((rate - tmp_rate) < (rate - best_rate)) {
  26. best_rate = tmp_rate;
  27. best_m = _m;
  28. best_p = _p;
  29. }
  30. }
  31. }
  32. *m = best_m;
  33. *p = best_p;
  34. }
  35. static unsigned long ccu_mp_round_rate(struct ccu_mux_internal *mux,
  36. unsigned long parent_rate,
  37. unsigned long rate,
  38. void *data)
  39. {
  40. struct ccu_mp *cmp = data;
  41. unsigned int m, p;
  42. ccu_mp_find_best(parent_rate, rate,
  43. 1 << cmp->m.width, (1 << cmp->p.width) - 1,
  44. &m, &p);
  45. return (parent_rate >> p) / m;
  46. }
  47. static void ccu_mp_disable(struct clk_hw *hw)
  48. {
  49. struct ccu_mp *cmp = hw_to_ccu_mp(hw);
  50. return ccu_gate_helper_disable(&cmp->common, cmp->enable);
  51. }
  52. static int ccu_mp_enable(struct clk_hw *hw)
  53. {
  54. struct ccu_mp *cmp = hw_to_ccu_mp(hw);
  55. return ccu_gate_helper_enable(&cmp->common, cmp->enable);
  56. }
  57. static int ccu_mp_is_enabled(struct clk_hw *hw)
  58. {
  59. struct ccu_mp *cmp = hw_to_ccu_mp(hw);
  60. return ccu_gate_helper_is_enabled(&cmp->common, cmp->enable);
  61. }
  62. static unsigned long ccu_mp_recalc_rate(struct clk_hw *hw,
  63. unsigned long parent_rate)
  64. {
  65. struct ccu_mp *cmp = hw_to_ccu_mp(hw);
  66. unsigned int m, p;
  67. u32 reg;
  68. reg = readl(cmp->common.base + cmp->common.reg);
  69. m = reg >> cmp->m.shift;
  70. m &= (1 << cmp->m.width) - 1;
  71. p = reg >> cmp->p.shift;
  72. p &= (1 << cmp->p.width) - 1;
  73. return (parent_rate >> p) / (m + 1);
  74. }
  75. static int ccu_mp_determine_rate(struct clk_hw *hw,
  76. struct clk_rate_request *req)
  77. {
  78. struct ccu_mp *cmp = hw_to_ccu_mp(hw);
  79. return ccu_mux_helper_determine_rate(&cmp->common, &cmp->mux,
  80. req, ccu_mp_round_rate, cmp);
  81. }
  82. static int ccu_mp_set_rate(struct clk_hw *hw, unsigned long rate,
  83. unsigned long parent_rate)
  84. {
  85. struct ccu_mp *cmp = hw_to_ccu_mp(hw);
  86. unsigned long flags;
  87. unsigned int m, p;
  88. u32 reg;
  89. ccu_mp_find_best(parent_rate, rate,
  90. 1 << cmp->m.width, (1 << cmp->p.width) - 1,
  91. &m, &p);
  92. spin_lock_irqsave(cmp->common.lock, flags);
  93. reg = readl(cmp->common.base + cmp->common.reg);
  94. reg &= ~GENMASK(cmp->m.width + cmp->m.shift - 1, cmp->m.shift);
  95. reg &= ~GENMASK(cmp->p.width + cmp->p.shift - 1, cmp->p.shift);
  96. writel(reg | (p << cmp->p.shift) | ((m - 1) << cmp->m.shift),
  97. cmp->common.base + cmp->common.reg);
  98. spin_unlock_irqrestore(cmp->common.lock, flags);
  99. return 0;
  100. }
  101. static u8 ccu_mp_get_parent(struct clk_hw *hw)
  102. {
  103. struct ccu_mp *cmp = hw_to_ccu_mp(hw);
  104. return ccu_mux_helper_get_parent(&cmp->common, &cmp->mux);
  105. }
  106. static int ccu_mp_set_parent(struct clk_hw *hw, u8 index)
  107. {
  108. struct ccu_mp *cmp = hw_to_ccu_mp(hw);
  109. return ccu_mux_helper_set_parent(&cmp->common, &cmp->mux, index);
  110. }
  111. const struct clk_ops ccu_mp_ops = {
  112. .disable = ccu_mp_disable,
  113. .enable = ccu_mp_enable,
  114. .is_enabled = ccu_mp_is_enabled,
  115. .get_parent = ccu_mp_get_parent,
  116. .set_parent = ccu_mp_set_parent,
  117. .determine_rate = ccu_mp_determine_rate,
  118. .recalc_rate = ccu_mp_recalc_rate,
  119. .set_rate = ccu_mp_set_rate,
  120. };