ccu_mult.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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_mult.h"
  13. static void ccu_mult_find_best(unsigned long parent, unsigned long rate,
  14. unsigned int max_n, unsigned int *n)
  15. {
  16. *n = rate / parent;
  17. }
  18. static unsigned long ccu_mult_round_rate(struct ccu_mux_internal *mux,
  19. unsigned long parent_rate,
  20. unsigned long rate,
  21. void *data)
  22. {
  23. struct ccu_mult *cm = data;
  24. unsigned int n;
  25. ccu_mult_find_best(parent_rate, rate, 1 << cm->mult.width, &n);
  26. return parent_rate * n;
  27. }
  28. static void ccu_mult_disable(struct clk_hw *hw)
  29. {
  30. struct ccu_mult *cm = hw_to_ccu_mult(hw);
  31. return ccu_gate_helper_disable(&cm->common, cm->enable);
  32. }
  33. static int ccu_mult_enable(struct clk_hw *hw)
  34. {
  35. struct ccu_mult *cm = hw_to_ccu_mult(hw);
  36. return ccu_gate_helper_enable(&cm->common, cm->enable);
  37. }
  38. static int ccu_mult_is_enabled(struct clk_hw *hw)
  39. {
  40. struct ccu_mult *cm = hw_to_ccu_mult(hw);
  41. return ccu_gate_helper_is_enabled(&cm->common, cm->enable);
  42. }
  43. static unsigned long ccu_mult_recalc_rate(struct clk_hw *hw,
  44. unsigned long parent_rate)
  45. {
  46. struct ccu_mult *cm = hw_to_ccu_mult(hw);
  47. unsigned long val;
  48. u32 reg;
  49. reg = readl(cm->common.base + cm->common.reg);
  50. val = reg >> cm->mult.shift;
  51. val &= (1 << cm->mult.width) - 1;
  52. ccu_mux_helper_adjust_parent_for_prediv(&cm->common, &cm->mux, -1,
  53. &parent_rate);
  54. return parent_rate * (val + 1);
  55. }
  56. static int ccu_mult_determine_rate(struct clk_hw *hw,
  57. struct clk_rate_request *req)
  58. {
  59. struct ccu_mult *cm = hw_to_ccu_mult(hw);
  60. return ccu_mux_helper_determine_rate(&cm->common, &cm->mux,
  61. req, ccu_mult_round_rate, cm);
  62. }
  63. static int ccu_mult_set_rate(struct clk_hw *hw, unsigned long rate,
  64. unsigned long parent_rate)
  65. {
  66. struct ccu_mult *cm = hw_to_ccu_mult(hw);
  67. unsigned long flags;
  68. unsigned int n;
  69. u32 reg;
  70. ccu_mux_helper_adjust_parent_for_prediv(&cm->common, &cm->mux, -1,
  71. &parent_rate);
  72. ccu_mult_find_best(parent_rate, rate, 1 << cm->mult.width, &n);
  73. spin_lock_irqsave(cm->common.lock, flags);
  74. reg = readl(cm->common.base + cm->common.reg);
  75. reg &= ~GENMASK(cm->mult.width + cm->mult.shift - 1, cm->mult.shift);
  76. writel(reg | ((n - 1) << cm->mult.shift),
  77. cm->common.base + cm->common.reg);
  78. spin_unlock_irqrestore(cm->common.lock, flags);
  79. return 0;
  80. }
  81. static u8 ccu_mult_get_parent(struct clk_hw *hw)
  82. {
  83. struct ccu_mult *cm = hw_to_ccu_mult(hw);
  84. return ccu_mux_helper_get_parent(&cm->common, &cm->mux);
  85. }
  86. static int ccu_mult_set_parent(struct clk_hw *hw, u8 index)
  87. {
  88. struct ccu_mult *cm = hw_to_ccu_mult(hw);
  89. return ccu_mux_helper_set_parent(&cm->common, &cm->mux, index);
  90. }
  91. const struct clk_ops ccu_mult_ops = {
  92. .disable = ccu_mult_disable,
  93. .enable = ccu_mult_enable,
  94. .is_enabled = ccu_mult_is_enabled,
  95. .get_parent = ccu_mult_get_parent,
  96. .set_parent = ccu_mult_set_parent,
  97. .determine_rate = ccu_mult_determine_rate,
  98. .recalc_rate = ccu_mult_recalc_rate,
  99. .set_rate = ccu_mult_set_rate,
  100. };