ccu_nm.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 <linux/rational.h>
  12. #include "ccu_frac.h"
  13. #include "ccu_gate.h"
  14. #include "ccu_nm.h"
  15. static void ccu_nm_disable(struct clk_hw *hw)
  16. {
  17. struct ccu_nm *nm = hw_to_ccu_nm(hw);
  18. return ccu_gate_helper_disable(&nm->common, nm->enable);
  19. }
  20. static int ccu_nm_enable(struct clk_hw *hw)
  21. {
  22. struct ccu_nm *nm = hw_to_ccu_nm(hw);
  23. return ccu_gate_helper_enable(&nm->common, nm->enable);
  24. }
  25. static int ccu_nm_is_enabled(struct clk_hw *hw)
  26. {
  27. struct ccu_nm *nm = hw_to_ccu_nm(hw);
  28. return ccu_gate_helper_is_enabled(&nm->common, nm->enable);
  29. }
  30. static unsigned long ccu_nm_recalc_rate(struct clk_hw *hw,
  31. unsigned long parent_rate)
  32. {
  33. struct ccu_nm *nm = hw_to_ccu_nm(hw);
  34. unsigned long n, m;
  35. u32 reg;
  36. if (ccu_frac_helper_is_enabled(&nm->common, &nm->frac))
  37. return ccu_frac_helper_read_rate(&nm->common, &nm->frac);
  38. reg = readl(nm->common.base + nm->common.reg);
  39. n = reg >> nm->n.shift;
  40. n &= (1 << nm->n.width) - 1;
  41. m = reg >> nm->m.shift;
  42. m &= (1 << nm->m.width) - 1;
  43. return parent_rate * (n + 1) / (m + 1);
  44. }
  45. static long ccu_nm_round_rate(struct clk_hw *hw, unsigned long rate,
  46. unsigned long *parent_rate)
  47. {
  48. struct ccu_nm *nm = hw_to_ccu_nm(hw);
  49. unsigned long n, m;
  50. rational_best_approximation(rate, *parent_rate,
  51. 1 << nm->n.width, 1 << nm->m.width,
  52. &n, &m);
  53. return *parent_rate * n / m;
  54. }
  55. static int ccu_nm_set_rate(struct clk_hw *hw, unsigned long rate,
  56. unsigned long parent_rate)
  57. {
  58. struct ccu_nm *nm = hw_to_ccu_nm(hw);
  59. unsigned long flags;
  60. unsigned long n, m;
  61. u32 reg;
  62. if (ccu_frac_helper_has_rate(&nm->common, &nm->frac, rate))
  63. return ccu_frac_helper_set_rate(&nm->common, &nm->frac, rate);
  64. else
  65. ccu_frac_helper_disable(&nm->common, &nm->frac);
  66. rational_best_approximation(rate, parent_rate,
  67. 1 << nm->n.width, 1 << nm->m.width,
  68. &n, &m);
  69. spin_lock_irqsave(nm->common.lock, flags);
  70. reg = readl(nm->common.base + nm->common.reg);
  71. reg &= ~GENMASK(nm->n.width + nm->n.shift - 1, nm->n.shift);
  72. reg &= ~GENMASK(nm->m.width + nm->m.shift - 1, nm->m.shift);
  73. writel(reg | ((m - 1) << nm->m.shift) | ((n - 1) << nm->n.shift),
  74. nm->common.base + nm->common.reg);
  75. spin_unlock_irqrestore(nm->common.lock, flags);
  76. ccu_helper_wait_for_lock(&nm->common, nm->lock);
  77. return 0;
  78. }
  79. const struct clk_ops ccu_nm_ops = {
  80. .disable = ccu_nm_disable,
  81. .enable = ccu_nm_enable,
  82. .is_enabled = ccu_nm_is_enabled,
  83. .recalc_rate = ccu_nm_recalc_rate,
  84. .round_rate = ccu_nm_round_rate,
  85. .set_rate = ccu_nm_set_rate,
  86. };