ccu_nm.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 max_n, max_m;
  50. unsigned long n, m;
  51. max_n = 1 << nm->n.width;
  52. max_m = nm->m.max ?: 1 << nm->m.width;
  53. rational_best_approximation(rate, *parent_rate, max_n, max_m, &n, &m);
  54. return *parent_rate * n / m;
  55. }
  56. static int ccu_nm_set_rate(struct clk_hw *hw, unsigned long rate,
  57. unsigned long parent_rate)
  58. {
  59. struct ccu_nm *nm = hw_to_ccu_nm(hw);
  60. unsigned long flags;
  61. unsigned long max_n, max_m;
  62. unsigned long n, m;
  63. u32 reg;
  64. if (ccu_frac_helper_has_rate(&nm->common, &nm->frac, rate))
  65. return ccu_frac_helper_set_rate(&nm->common, &nm->frac, rate);
  66. else
  67. ccu_frac_helper_disable(&nm->common, &nm->frac);
  68. max_n = 1 << nm->n.width;
  69. max_m = nm->m.max ?: 1 << nm->m.width;
  70. rational_best_approximation(rate, parent_rate, max_n, max_m, &n, &m);
  71. spin_lock_irqsave(nm->common.lock, flags);
  72. reg = readl(nm->common.base + nm->common.reg);
  73. reg &= ~GENMASK(nm->n.width + nm->n.shift - 1, nm->n.shift);
  74. reg &= ~GENMASK(nm->m.width + nm->m.shift - 1, nm->m.shift);
  75. writel(reg | ((m - 1) << nm->m.shift) | ((n - 1) << nm->n.shift),
  76. nm->common.base + nm->common.reg);
  77. spin_unlock_irqrestore(nm->common.lock, flags);
  78. ccu_helper_wait_for_lock(&nm->common, nm->lock);
  79. return 0;
  80. }
  81. const struct clk_ops ccu_nm_ops = {
  82. .disable = ccu_nm_disable,
  83. .enable = ccu_nm_enable,
  84. .is_enabled = ccu_nm_is_enabled,
  85. .recalc_rate = ccu_nm_recalc_rate,
  86. .round_rate = ccu_nm_round_rate,
  87. .set_rate = ccu_nm_set_rate,
  88. };